mysql> create table db3.t2(
-> name char(10) not null default "", #不能为空值,当没有赋值时,默认赋值为“”;
-> sex enum("boy","girl") default "boy", #当没有赋值时,默认赋值为boy;
-> likes set("eat","game","money","it") not null default "eat,money")
mysql> desc db3.t2; #查看表结构
mysql> inset into db3.t2 values(); #都不赋值,查看赋的默认值
mysql> insert into db3.t2(name) values("mao"); #只赋值给name字段
mysql> alter table t2 add class char(7)default "nsd1911" first; #添加到第一列
mysql> alter table t2 add email varchar(50); #条件到最后一列
mysql> alter table db3.t2 add age int unsigned not null default "22" after name;
#把age字段添加在name字段之后
mysql> alter table db3.t2 modify email char(30) default "[email protected]"; #若t2表格里本身有email为空。则not null不能添加
mysql> alter table t2 modify age tinyint unsigned default 23 after class; #更改age的默认值,且放在class后面
mysql> alter table t2 change age ager tinyint unsigned not null default 30; #把age改为ager
mysql> alter table t2 drop class , drop email;
mysql> alter table t1 rename tt1;
mysql> create table db3.t4(name char(10),cj int,class char(10),
-> index(name),index(cjj)); #创建表格时,指定索引字段
mysql> create index xxss on db3.t4(class); #
mysql> desc db3.t4\G
mysql> show index from db3.t4\G #查看索引
mysql> drop index cj on db3.t4; #删除索引
mysql> create table t5(
-> name char(3),home char(9),tel char(10)
-> primary key(tel) #建表时创建主键
-> );
mysql> create table db3.t6(name char(10) primary key,age char(10)); #建表时创建主键
mysql> alter table db3.t1 add primary key(name); #在已有表里创建主键
mysql> alter table db3.t1 drop primary key; #删除主键
mysql> create table db3.t10(client_ip char(15),ser_port smallint,access enum("allow","deny"),primary key(client_ip,ser_port)); #创建复合主键 (表中多列一起做主键)
mysql> create table db3.t11(id int primary key auto_increment,name char(10),age tinyint);
#主键与auto_increment连用,字段值自增长 i++
mysql> alter table db3.t11 add id int primary key auto_increment first;
#增加id字段,有主键自增,当不加auto_increment时,会报错,因为int类型如果不允许为空那么会被默认为0,所以插入第二条数据时,数据库中已经有了主键为0的数据,所以冲突了,产生了错误!
mysql> alter table db3.t11 auto_increment=1; #只有在清空表格内容时,才能把初始值重置为1;
mysql> alter table dd1.t11 modify id int; #删除自增主键时,先修改其属性,去掉自增
mysql> alter table dd1.t11 drop primary key; #然后删除主键
create table db2.yg(
yg_id int primary key auto_increment , #依附的表格,最好与主键联用
name char(15)
)engine=innodb
create table db2.gz ( gz_id int , name char(15) , gz float(7,2),
foreign key(gz_id) references db2.yg (yg_id) #创建外键
on update cascade on delete cascade #同步更新,同步删除
)engine=innodb;
mysql> update db2.yg set yg_id=8 where name="bob" ; #测试同步更新
select * from db2.gz;
mysql> delete from db2.yg where yg_id=8; #测试同步删除
select * from db2.gz;
mysql> alter table db2.gz add primary key (gz_id);
mysql> show create table db2.gz \G #查看表的外键
mysql> alter table db2.gz drop foreign key gz_ibfk_1 ; #删除外键,gz_ibfk_1通过查看外键约束所得
mysql> alter table db2.gz add foreign key(gz_id) references db2.yg(yg_id) on update cascade on delete cascade; #在已有表添加外键字段(前是所关联的表格字段必须是主键)
默认引擎为innodb