MYSQL数据库-约束
时间:2021-07-01 10:21:17
帮助过:3人阅读
CREATE DATABASE mysql_shiyan;
2
3 use mysql_shiyan;
4
5 CREATE TABLE department
6 (
7 dpt_name
CHAR(
20)
NOT NULL,
8 people_num
INT(
10)
DEFAULT ‘10‘,
9 CONSTRAINT dpt_pk
PRIMARY KEY (dpt_name)
10 );
11
12 CREATE TABLE employee
13 (
14 id
INT(
10)
PRIMARY KEY,
15 name
CHAR(
20),
16 age
INT(
10),
17 salary
INT(
10)
NOT NULL,
18 phone
INT(
12)
NOT NULL,
19 in_dpt
CHAR(
20)
NOT NULL,
20 UNIQUE (phone),
21 CONSTRAINT emp_fk
FOREIGN KEY (in_dpt)
REFERENCES department(dpt_name)
22 );
23
24 CREATE TABLE project
25 (
26 proj_num
INT(
10)
NOT NULL,
27 proj_name
CHAR(
20)
NOT NULL,
28 start_date DATE
NOT NULL,
29 end_date DATE
DEFAULT ‘2015-04-01‘,
30 of_dpt
CHAR(
20)
REFERENCES department(dpt_name),
31 CONSTRAINT proj_pk
PRIMARY KEY (proj_num,proj_name)
32 );
MYSQL数据库-约束
标签: