添加主键约束
ALTER TABLE tb_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…)
如:
ALTER TABLE user2 ADD CONSTRAINT PK_user2_id PRIMARY KEY (id);
添加唯一约束
ALTER TABLE tb_name ADD [CONSTRAINT[SYMBOL]] UNIQUE [INDEX|KEY] [INDEX_NAME] [INDEX_TYPE] (index_col_name,…)
唯一约束能多个,主键约束只能有一个。
如:
ALTER TABLE user2 ADD UNIQUE (username);
添加外键约束
ALTER TABLE tb_name ADD [CONSTRAINT[symbol]] FOREIGN KEY [index_name] (index_col_name,…) reference_definition
如:
ALTER TABLE user2 ADD FOREIGN KEY (pid) REFERENCES pvovinces (id);
添加/删除默认约束
ALTER TABLE tb_name ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
set如:
ALTER TABLE user2 ALTER age SET DEFAULT 15;
drop如:
ALTER TABLE user2 ALTER age DROP DEFAULT;