主键约束:
CREATE TABLE t( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY);
备注:一张表只能有一个。
唯一键约束:
CREATE TABLE t( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL UNIQUE KEY);
备注:一张表能够有多个。
默认键约束:create table t5(
id int unsigned auto_increment primary key,
username varchar(20) not null unique key,
sex ENUM('1','2','3') default '3');
备注:当执行insert t5(username) values('Tom');时系统会给sex字段默认赋值3。
外键约束:create table t5(
id int unsigned auto_increment primary key,
username varchar(20) not null unique key,
sex ENUM('1','2','3') default '3',
p_id tinyint unsigned,
foreign key(p_id) references t(pid)
);
备注:父表和子表的数据库引擎必须保持一致,当创建外键时外键列和参考列会自动创建索引。