-- 修改表名 ALTER TABLE 旧表名 RENAME 新表名;
alter table student rename t_student
-- ALTER TABLE 表名 CHANGE COLUMN 旧列名 新列名 类型;
-- CHANGE COLUMN修改列的关键字
alter table t_student change column stu_scord stu_class VARCHAR(10)
-- ALTER TABLE 表名 MODIFY 列名 新类型; MODIFY修改列的类型关键字
alter table t_student MODIFY stu_name VARCHAR(20)
-- ALTER TABLE 表名 ADD COLUMN 列名 类型;向表中添加新列
alter table t_student add COLUMN stu_AdTime datetime default null
alter table t_student drop COLUMN stu_entryTime
-- 修改表添加主键约束 ALTER TABLE 表名 ADD PRIMARY KEY(列名)
alter table t_student add primary key(stu_id)
select * from t_student
-- alter table 表名 modify 主键 类型 auto_increment;将id设置为自动增长
alter table t_student modify stu_id int auto_increment
-- 将字段stu_id移动到stu_no的前面
alter table t_student modify stu_no int AFTER stu_id
-- ALTER TABLE 表名 DROP PRIMARY KEY;删除主键约束先去掉自动增长再删除
alter table t_student modify stu_id int
alter table t_student drop primary key
-- 添加外键约束;ALTER TABLE 表名 ADD CONSTRAINT 约束名 FOREIGN KEY( 列 名 ) REFERENCES 参照的表名(参照的列名);
CREATE table t_subject(sub_id int,sub_subject varchar(20),sub_credit float(8,2))
alter table t_subject add primary key(sub_id)
alter table t_subject modify sub_id int auto_increment
alter table t_student add column sub_id int
alter table t_student add constraint stu_fk foreign key(sub_id) REFERENCES t_subject(sub_id)
select * from t_subject
-- 删除外键约束;ALTER TABLE 表名 DROP FOREIGN KEY 约束名;
alter table t_student drop foreign key stu_fk
-- 添加唯一性约束;ALTER TABLE 表名 ADD CONSTRAINT 约束名 UNIQUE(列名);
-- 被约束的字段不允许出现重复值
alter table t_student add constraint stu_uk unique(stu_id)
-- ALTER TABLE 表名 DROP KEY 约束名;删除唯一性约束
alter table t_student drop key stu_uk
-- ALTER TABLE 表名 MODIFY 列名 类型 NOT NULL;添加非空约束
alter table t_student modify stu_class VARCHAR(20) not null
select * from t_student
-- ALTER TABLE 表名 MODIFY 列名 类型 NULL;删除非空约束
alter table t_student modify stu_class varchar(20) not null
-- 查询表中约束信息:SHOW KEYS FROM 表名;
show keys from t_student
-- 创建表添加约束
create table t_teacher(tea_id int primary key auto_increment,tea_math int default 0,tea_english int default 0,tea_chinese int default 0,tea_position varchar(20) UNique)
select * from t_teacher