标签:
设置主键约束:
CREATE TABLE t_student( s_id INT PRIMARY KEY, -- 指定主键约束 s_name VARCHAR(50) NOT NULL)
--增加主键 ALTER TABLE t_class CHANGE c_id c_id INT,ADD PRIMARY KEY(c_id);
设置非空约束:
CREATE TABLE t_student( s_id INT PRIMARY KEY, s_name VARCHAR(50) UNIQUE NOT NULL, s_age INT NOT NULL
设置外键约束:
REATE TABLE t_class( c_id INT PRIMARY KEY, c_name VARCHAR(50) UNIQUE NOT NULL )ENGINE=INNODB --再创建从表(外键所在的表) CREATE TABLE t_student( s_id INT PRIMARY KEY, s_c_id INT REFERENCES t_class(c_id), s_name VARCHAR(50) UNIQUE NOT NULL, s_age INT, s_gendar CHAR(2) DEFAULT ‘男‘, CONSTRAINT FOREIGN KEY(s_c_id) REFERENCES t_class(c_id) )ENGINE=INNODB
CREATE TABLE t_class( c_id INT PRIMARY KEY AUTO_INCREMENT,--定义自增长
标签:
原文地址:http://www.cnblogs.com/yigegushi/p/5618756.html