1 -- 创建数据库
2 create database OneDB
3
4 -- 使用数据库
5 use OneDB
6
7 -- 创建表 : 分类表
8 create table classify
9 (
10 id int primary key identity(1,1), -- 主键标识,从1开始,步长1增长
11 name nvarchar(20) not null -- not null 非空
12 )
13
14 -- 向classify表中插入数据
15 insert into classify(name)values('图书') -- 主键标识是自增的
16 insert into classify(name)values('家电') -- 所以在这里只需要向
17 insert into classify(name)values('服饰') -- name中插入数据即可!
18
19
20 -- 创建表 : 产品表
21 create table product
22 (
23 id int primary key identity(1,1), -- 主键标识,从1开始,步长1增长
24 name nvarchar(20) not null,
25 price decimal default 0.00, -- price的值默认为0.00
26 number int default 0,
27 c_id int foreign key references classify(id) -- 外键引用
28 )
29
30
31 -- 删除表
32 drop table classify
33 drop table product
34
35
36 -- 查询表中的数据
37 select * from classify
38 select * from product
1 -- 创建分类表
2 create table classify
3 (
4 id int primary key identity(1,1),
5 name nvarchar(20) not null
6 )
7
8 -- 添加分类表测试数据
9 insert into classify(name) values('图书');
10 insert into classify(name) values('家电');
11 insert into classify(name) values('服饰');
12
13 -- 创建商品表
14 create table product
15 (
16 id int primary key identity(1,1),
17 name nvarchar(20) not null,
18 price decimal default 0.00,
19 number int default 0,
20 c_id int foreign key references classify(id)
21 )
22
23 -- 添加商品表测试数据
24 insert into product(name, price, number, c_id) values('PHP入门到精通', 53, 50, 1);
25 insert into product(name, price, number, c_id) values('数据库入门到精通', 72, 55, 1);
26 insert into product(name, price, number, c_id) values('比基尼', 89, 90, 3);
27 insert into product(name, price, number, c_id) values('虎皮裙', 50, 90, 3);
28 insert into product(name, price, number, c_id) values('长靴', 89, 90, 3);
29 insert into product(name, price, number, c_id) values('ASP.NET入门到精通', 56, 80, 1);
30 insert into product(name, price, number, c_id) values('电冰箱', 36, 40, 2);
31 insert into product(name, price, number, c_id) values('烘干机', 86, 35, 2);
32
33 select * from classify
34 select * from product