?xml version="1.0" encoding="UTF-8"?
1.添加主键约束
ALTER TABLE tel_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,…);
2.添加唯一约束
ALTER TABLE able_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,…);
eg:ALTER TABLE user2 ADD UNIQUE (username);
3.添加外键约束
ALTER TABLE tel_name ADD
eg: ALTER TABLE user2 ADD FOREIGN KEY (pid) REFERENCES provinces(id);
查看数据表创建结构:
SHOW CREATE TABLE user2;
4.添加默认约束:ALTER TABLE users2 ALTER age SET DEFAULT 15;
删除默认约束:ALTER TABLE users2 ALTER age DROP DEFAULT;
删除约束就是把ADD 改成DROP;
查看约束的名字:
SHOW INDEXES FROM user2\G;
修改列定义:
ALTER TABLE user2 MODIFY id SMALLINT UNSIGNED NOT NULL FIRST;
修改列名称:
ALTER TABLE user2 CHANGE pid p_id TINYINT UNSIGNED NOT NULL;
数据表更名:
方法1:
ALTER TABLE tel_name RENAME [TO|AS] new_tbl_name;
方法2:
RENAME TABLE tel_name TO new_tbl_name [,tel_name2 TO new_tbl_name]...
网友评论