>> alter table 表名 modify 列名 int not null; "添加非空约束" >> alter table 表名 modify 列名 int; "取消非空约束"
>> alter table 表名 add unique key(列); "添加唯一约束" >> alter table 表名 drop key 列; "取消唯一约束"
>> alter table 表名 add primary key(列); "添加主键约束" >> alter table 表名 drop primary key; "取消主键约束"
>> alter table 需要外键的表 add constraint 外键名 foreign key(需要外键表的字段名) referencnes 关联表名(关联字段名);
"添加外键约束"(注意:外键名不能重复)
>> alter table 存在外键的表 drop foreign key 关联字段名;
"删除外键约束"
>> alter table 表名 modify 列 int auto_increment;
"添加自增长约束"
>> alter table 表名 modify 列 int;
"删除自增长约束"
>> alter table 表名 modify 列 int default 默认值;
"添加默认约束" >> alter table 表名 modify 列 int;
"删除默认约束"
#建立学生信息表: create table student_details( id int primary key, sex varchar(20) not null, age int, address varchar(20) comment '家庭住址', parents varchar(20), home_num varchar(20), foreign key (id) references student(s_id) );
##创建学生表 create table student( s_id int primary key auto_increment, # 学生id s_name varchar(20) not null, # 学生名字 dept_id int not null, # 所属学院 id constraint sd_id foreign key(dept_id) references department(d_id) #外键 );
#创建学院表 create table department( d_id int primary key auto_increment, # 学院id d_name varchar(20) not null # 学院名 );
3.多对多关系
例:通过创建中间表进行关联 。 学生表:课程表=m:n
#建立课程表: create table cours( cours_id int primary key auto_increment, cours_name varchar(20) not null );
# 选课表 (中间表) create table `select`( s_id int, #用来记录学生id cours_id int, #用来记录 课程id primary key(s_id,cours_id), # 联合主键 foreign key(s_id) references student(s_id), # 关联学生id foreign key(cours_id) references cours(cours_id) # 关联课程id );
如对本文有疑问, 点击进行留言回复!!
mysql·update语句报错:Data truncation: Truncated incorrect DOUBLE value: ‘系统开小差啦~请稍后再试‘
MySQL Fix Product Name Format(trim去空格+upper/lower大小写)
mysql pxc LIMITATIONS 8.0.19-10
网友评论