create table student( id int, name char(6), born_year year, birth_date date, class_time time, reg_time datetime ); insert into student values (1,‘eddie‘,now(),now(),now(),now());
字符类型
1 char: 定长
2 varchar: 变长
#宽度指的是字符的个数
create table t1(name char(5));
create table t2(name varchar(5));
insert into t1 values(‘李杰 ‘);
insert into t2 values(‘李杰‘);
select char_length(name) from t1;
select char_length(name) from t2;
枚举类型/集合类型
create table consumer( id int, name char(16), sex enum(‘male‘,‘female‘,‘other‘), level enum(‘vip1‘,‘vip2‘,‘vip3‘), hobbies set(‘play‘,‘music‘,‘read‘,‘run‘) ); insert into consumer values (1,‘eddie‘,‘male‘,‘vip2‘,‘music,read‘);
约束条件not null 和 default
create table t15( id int(11) unsigned zerofill );
create table t16( id int, name char(10), sex enum(‘male‘,‘female‘) not null default ‘male‘ ); insert into t16(id, name) values(1,‘eddie‘);
unique key
create table department( id int unique, name char(10) unique ); insert into department values (1,‘IT‘), (2,‘SALE‘);
联合唯一
create table services( id int, ip char(15), port int, unique(id), unique(ip,port) ); insert into services values (1,‘192.168.1.10‘,80), (2,‘192.168.1.10‘,81), (3,‘192.168.1.13‘,80);
primary key
约束: not null unique
存储引擎(innodb): 对于innodb存储引擎来说,一张表内必须有一个主键
单列主键
create table t18( id int not null unique, name char(10) );
复合主键
create table t19( ip char(10), port int, primary key(ip,port) ); insert into t19 values (‘1.1.1‘,80), (‘1.1.1‘,81);
auto_increment 自增长
create table t20( id int primary key auto_increment, name char(16) ); insert into t20(name) values (‘eddie‘), (‘david‘), (‘wxx‘); insert into t20(id,name)values (7,‘xxxx‘); show variables like ‘auto_inc%‘; #步长 auto_increment_increment 默认为1 #起始偏移量 auto_increment_offset 默认为1 #设置步长 set session auto_increment_increment=5; set global auto_increment_increment=5 #设置起始偏移量 set global auto_increment_offset=3; 起始偏移量小于等于步长
清空表
delete from t20; #auto_increment 没有删除 用delete 删除固定的id truncate t20; 建议用这个清空表
原文地址:https://www.cnblogs.com/zhujing666/p/12311889.html
时间: 2024-11-07 23:42:29