(小tips:此篇文章表均与上篇文章表相关联)
1、增加列
alter table student
-> add ssex char(2) not null(add为表添加新的字段)
-> ;
2、更改列的默认值
alter table student
-> alter ssex set default '男'(set default为表中字段设置默认值)
-> ;
3、给表重命名test改成test2
方法一:
rename table test to test2;(原表名to新表名)
方法二:
alter table test2(目前在学的在用的为alter table)
-> rename to test;(改表名)
4、更改student表中ssex的数据类型为枚举类型(‘male‘,’female’),默认值为‘male’
alter table student
-> modify ssex enum('male','female') not null;(更改已有字段的类型)
5、更改ssex的默认值为male
alter table student
-> alter ssex set default 'male';
6、添加主键
alter table test
-> add primary key(id)
-> ;
7、创建选修课程表
create table sc
-> (
-> sno char(11),
-> cno varchar(20),
-> grade decimal(6,2)
-> );
8、在sc表中添加student表中的sno为外键
alter table sc
-> add foreign key(sno) references student(sno);
9、在sc表中添加course表中的cno为外键
alter table sc
-> add foreign key(cno) references course(cno);
10、表sc设主键
alter table sc
-> add primary key(sno,cno);