标签:
约束:
1. 约束保证数据的完整性和一致性;
2. 约束分为表级约束和列级约束;
3. 约束类型包括:NOT NULL, PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREIGN KEY
CREATE TABLE USERS ( id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREAMENT, username VARCHAR(10) NOT NULL, pid SMALLINT UNSIGNED, FOREIGN KEY(pid) REFERENCES provinces (id) ON DELETE CASCADE );
4. 修改数据表
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
ALTER TABLE users ADD age SMALLINT UNSIGNED DEFAULT 10 AFTER username;
ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition, ...)
ALTER TABLE tbl_name DROP [COLUMN] col_name
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name, ...)
ALTER TABLE tbl_name DROP PRIMARY KEY;
//删除唯一约束 ALTER TABLE tbl_name DROP {INDEX | KEY} index_name //删除外键约束 ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol (通过SHOW CREATE TABLE tbl_name查看fk_symbol)
//修改列定义 ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name] //修改列名称 ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name] //修改数据表的名称 //法一: ALTER TABLE tbl_name RENAME [TO|AS] new_tbl_name //法二: RENAME TABLE tbl_name TO new_tbl_name [, tbl_name2 TO new_tbl_name2]...
标签:
原文地址:http://www.cnblogs.com/little-YTMM/p/5423115.html