這裏先建立一張表 t_stu;
DROP TABLE IF EXISTS t_stu;
CREATE TABLE t_stu(
id INT,
stu_name VARCHAR(20),
age INT,
majorid INT
)
ALTER TABLE t_stu MODIFY COLUMN stu_name VARCHAR(20) not NULL;
ALTER TABLE t_stu MODIFY COLUMN age INT DEFAULT 18;
1.列級約束
ALTER TABLE t_stu MODIFY COLUMN name INT PRIMARY KEY;
2.表級約束
ALTER TABLE t_stu ADD PRIMARY KEY(id);
1.列級約束
ALTER TABLE t_stu MODIFY COLUMN stu_name VARCHAR(20) UNIQUE;
2.表級約束
ALTER TABLE t_stu ADD UNIQUE(stu_name);
添加外鍵
ALTER TABLE t_stu ADD CONSTRAINT fk_major FOREIGN KEY(majorid)
REFERENCES t_major(id);
刪除約束
刪除惟一鍵
ALTER TABLE t_stu DROP INDEX name;
刪除外鍵
ALTER TABLE t_stu DROP FOREIGN KEY fk_major;
SHOW INDEX from t_stu;