Sql语句学习笔记(2)-创建数据表

use RetalDB--表示在数据库RetalDB中进行的操作
go
if exists (select * from sysobjects where name='tb_user')
 drop table tb_user
go

--创建客户表tb_user
create table tb_user
(
 user_id int primary key,--指定为主键时,此列默认为非空,指定过多个限制条件时不用“'”隔开
 user_name varchar(20) not null,
 user_point int not null,
)

--创建影碟类型表tb_MovieType
if exists (select *from sysobjects where name='tb_movie_type')
 drop table tb_movie_type
go

create table tb_movie_type
(
 type_id int primary key,--指定主键
 type_name varchar(20) not null,
 type_english_name varchar(20) not null
)

--创建影碟表
if exists (select * from sysobjects where name='tb_movie')
 drop table tb_movie
go

create table tb_movie
(
 movie_id int primary key,
 movie_name varchar(20) not null,
 movie_price float,
 movie_type_id int not null,
 movie_detail varchar(255)
 --添加外键约束
 constraint FK_movie_id foreign key(movie_type_id) references tb_movie_type(type_id)--引用影片类型表中的type_id字段
)

--创建租赁信息表
if exists (select * from sysobjects where name='tb_retal')
 drop table tb_retal
go

create table tb_retal
(
 id int identity(1,1) primary key,--信息编号
 user_id int not null,--外键,引用客户表中的主键user_id
 movie_id int not null, --外键,引用影片表中的movie_id 
 rent_time datetime not null  default(getdate()),--租出时间,以插入数据时的时间为默认值
 return_time datetime,--归还时间
 rent_fee float default(0)--租金
 
 constraint FK_user_id foreign key(user_id) references tb_user(user_id),
 constraint FK_movie_id foreign key(movie_id) references tb_movie(movie_id)
)

 

 

 

创建好的表如图所示:(后来为了保证tb_retal中数据的唯一性,将user_id,movie_id,rent_time这3个键设置成了联合主键,即同一个客户不可能在同一时间租了同一张影碟多次)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值