先说好,一定要以管理员身份运行!!!!!!
例1 院系表(带列级约束)
没注释的是源代码
mysql> use db_2;
mysql> create table Department( Dno varchar(3) PRIMARY KEY, Dname varchar(30) UNIQUE, Office varchar(4) );
mysql> insert into Department values('D1','信工学院','C101'),('D2','地理学院','S201'),('D3','工学院','F301' ),('D4','医 学院','B206' );
mysql> SELECT * FROM Department;
例2 创建带列级约束的学生表
(不好意思,本人已经挂在找语法错误的路上了,等我明白怎么改才正确再来更新此篇) 谢谢马老师的指正,原来课本上有些SQL语句是根据Oracle优化的,不适合简单的MySQL。
然后以下是改好的代码:
create table Student(Sno varchar(3) PRIMARY KEY, Sname VARCHAR(10) NOT NULL, Sex Char(2) DEFAULT '男' CHECK(Sex IN ('男','女')), Birth DATE, Dno varchar(3) REFERENCES Department(Dno) );
mysql> insert into Student values('S1','赵刚','男','1995-09-02','D1'),
-> ('S2','周丽','女','1996-01-06','D3'),
-> ('S3','李强','男','1996-05-02','D3'),
-> ('S4','刘珊','女','1997-08-08','D1'),
-> ('S5','齐超','男','1997-06-08','D2'),
-> ('S6','宋佳','女','1997-08-02','D4');
mysql> SELECT * FROM Student;
|