数据库课程设计
数据库课程设计题目:影城网上售票系统
关系模式:
1.顾客(用户名,用户姓名,用户性别,身份证号,手机号)(用户名为主键)
2.电影院(影院名称,地点,电话号码)
3.影院名称(影厅编号,影厅类型,可容纳人数)
4.电影(电影名称,导演,主演,类型,图片)
5.电影票(用户姓名,影厅编号,放映编号,座位号)
6.放映关系(放映编号,影院名称,电影名称,放映日期,放映时间,价格,影厅编号)
1.数据含义:
CustomerID :用户名
CustomerName:用户姓名
CustomerSex:性别
CustomerIDNumber:身份证号
CustomerPhone:手机号
CinemaName:影院姓名
CinemaAddress:影院地点
CustomerPhone:影院电话号码
CinemaID :影厅编号 主键
CinemaType :影厅类型
CinemaNumber :可容纳人数
FilmID:电影名称
FilmDirector:导演
FilmStar:主演
FilmType:类型
FilmPicture:图片
FilmNumber:放映编号
SeatNumber:座位号
4.1.1 数据库创建
写出创建的SQL语句,文档中显示SQL语句创建成功的截图。
数据库的创建可分为两种模式:
一种为利用SQL Server Management Studio 创建数据库,另一种为用SQL语句创建数据库。
首先,先用第一种方法来创建数据
我是在学校机房里面整的,就没把文件保存到C盘,路径可以自己选择。
第二种方法:
用SQL语句创建数据库,
代码如下:
CREATE DATABASE XSCJ
ON RRIMARY
{
}
代码:1.
CREATE TABLE Customer (
CustomerID nvarchar(10) NOT NULL PRIMARY KEY,
CustomerName nvarchar(11) NOT NULL,
CustomerSex nvarchar(11) NOT NULL,
CustomerIDNumber nvarchar(11) NOT NULL,
CustomerPhone nvarchar(11) NOT NULL,
);
2.CREATE TABLE Cinema (
CinemaName nvarchar(10) NOT NULL PRIMARY KEY,
CinemaAddress nvarchar(11) NOT NULL,
CustomerPhone nvarchar(11) NOT NULL,
);
3.CREATE TABLE CinemaId (
CinemaID nvarchar(10) NOT NULL PRIMARY KEY,
CinemaType nvarchar(11) NOT NULL,
CinemaNumber nvarchar(11) NOT NULL,
);
4.CREATE TABLE Film(
FilmID nvarchar(10) PRIMARY KEY,
FilmDirector nvarchar(10) NOT NULL,
FilmStar nvarchar(10) NOT NULL,
FilmType nvarchar(10) NOT NULL,
FilmPicture nvarchar(10) NOT NULL,
);
5.CREATE TABLE FilmTicket (
CustomerName nvarchar(10) PRIMARY KEY,
CinemaID nvarchar(10) NOT NULL,
FilmNumber nvarchar(10) NOT NULL,
SeatNumber nvarchar(10) NOT NULL,
)
6.CREATE TABLE ShowRelationship(
FilmNumber nvarchar(10) NOT NULL,
CinemaName nvarchar(10) NOT NULL,
FilmID nvarchar(10) NOT NULL,
FilmDate nvarchar(10) NOT NULL,
FilmTime nvarchar(10) NOT NULL,
FilmPrice nvarchar(10) NOT NULL,
CinemaID nvarchar(10) NOT NULL,
)
视图的创建:
1.
CREATE VIEW CustomerView (
客户编号,
客户姓名,
客户性别,
身份证号,
手机号
)
AS
SELECT CustomerID, CustomerName, CustomerSex, CustomerIDNumber
, CustomerPhone
FROM Customer
2.
CREATE VIEW CinemaView (
影院名称,
地点,
电话号码
)
AS
SELECT CinemaName, CinemaAddress, CustomerPhone
FROM Cinema
3.
CREATE VIEW FilmTicketView (
用户姓名,
影厅编号,
放映编号,
座位号
)
AS
SELECT CustomerName, CinemaID , FilmNumber,SeatNumber
FROM FilmTicket
4.
CREATE VIEW ShowRelationshipTicketView (
放映编号,
影院名称,
电影名称,
放映日期,
放映时间,
价格,
影厅编号
)
AS
SELECT FilmNumber,CinemaName,FilmID,FilmDate,FilmTime,FilmPrice,CinemaID
FROM ShowRelationship
主键约束的创建:
USE File
GO
ALTER TABLE ShowRelationship
ADD CONSTRAINT PK_FileNumber PRIMARY KEY(FileNumber)
GO
1.删除Cinema表中的主键约束:
DEFAULT约束的创建:(默认值的约束)
1.为客户表Customer中的性别字段CustomerSex设置默认值为“男”:
2.然后通过SQL语句先使Customer表中的默认值取消
USE File
GO
ALTER TABLE CinemaId
ADD CONSTRAINT DF_CinemaNumber DEFAULT 100 FOR CinemaNumber
GO
外键约束的创建:
UNIQUE约束的创建:
USE Film
GO
ALTER TABLE Customer
ADD CONSTRAINT UN_CustomerPhone UNIQUE(CustomerPhone)
GO
CHECK约束的创建:
USE Film
GO
ALTER TABLE CinemaId
ADD CONSTRAINT CK_CinemaType CHECK(CinemaType='大' or CinemaType='小')
GO
索引的创建:
1.使用SQL语言在Customer表中的CustomerID列按升序创建索引IX_Customer_CustomerID。
2.
USE Film
GO
CREATE INDEX IX_Customer_CustomerID ON Customer(CustomerID)
GO
存储过程的创建:
1.在Film数据库中创建名为P_Customer的存储过程,要求查询每个顾客的姓名,性别,身份证号,手机号。
SQL语句:
USE Film
GO
CREATE PROCEDURE P_Customer
AS
SELECT Customer.CustomerName,Customer.CustomerSex,Customer.CustomerIDNumber,Customer.CustomerPhone
FROM Customer,FilmTicket
WHERE Customer.CustomerName = FilmTicket.CustomerName
2.
创建名称为P_Customer2的存储过程.要求输入某个电影的电影名称,能够查询到该电影的放映名称
USE Film
GO
CREATE PROCEDURE P_Customer2 @Film_FilmID nvarchar(20)
AS
SELECT ShowRelationship.FilmTime
FROM Film,ShowRelationship
WHERE Film.FilmID = ShowRelationship.FilmID
3.
创建名称为P_Customer4 的存储过程.要求输入某个日期,能得到该日期下,影院的放映电影名称
USE Film
GO
CREATE PROCEDURE P_Customer4 @FilmDate nvarchar(20)
AS
SELECT ShowRelationship.FilmID
FROM ShowRelationship
WHERE ShowRelationship.FilmDate = @FilmDate
触发器的创建:
1.在数据库的表ShowRelationshipTicketView上,创建触发器T1_insert,插入价格满足取值为30,40,50,60,70的售票记录
USE Film
GO
CREATE trigger T1_insert
ON ShowRelationship
FOR INSERT
AS
DECLARE @ShowRelationship_FilmPrice smallint
select @ShowRelationship_FilmPrice = ShowRelationship.FilmPrice
from ShowRelationship
if @ShowRelationship_FilmPrice not in(30,40,50,60,70)
rollback
go
2.创建数据库的insert触发器T2_insert,在向Customer表中输入一条记录的同时输入这个顾客的购票信息到FilmTicket 表
USE Film
GO
CREATE trigger T2_insert
ON Customer
FOR INSERT
AS
DECLARE @Customer_CustomerName char(10)
select @Customer_CustomerName = CustomerName from inserted
insert into FilmTicket(CustomerName,CinemaID) Values(@Customer_CustomerName,'2000000')
select *
from Customer
go
3.创建数据库的DELETE触发器T3_delete,在删除一个顾客的顾客信息的同时删除这个顾客的购票信息记录。
USE Film
GO
CREATE trigger T3_delete
ON Customer
FOR delete
AS
DECLARE @Customer_CustomerName nvarchar(10)
select @Customer_CustomerName = CustomerName from deleted
delete from Customer where CustomerName = @Customer_CustomerName
delete from FilmTicket where CustomerName = @Customer_CustomerName
select *
from Customer
go
五:数据库的操作
1.数据的操纵
1.1表结构的修改:
1.向FilmTicket表中添加Price字段
alter table FilmTicket
add Price nvarchar(10)
修改FilmTicket表中的Price字段,使之非空
alter table FilmTicket
alter column Price nvarchar(10) not null
2.数据插入:
1.向数据库的CinemaId 表中插入记录('01','大','50')
INSERT
INTO CinemaId
Values('01','大','50')
2.向数据库的Cinema 表中插入记录('万达','万达广场','17788887777')
INSERT
INTO Cinema
Values('万达','万达广场','17788887777')
3.数据更新:
1.将Film数据库中影厅编号为‘01’的房间,房间类型改成'小'
USE Film
GO
UPDATE CinemaId
set CinemaType ='小'
where CinemaID ='01'
go
2.将Film数据库中影院名称为万达的影院,电话号码改成15566664444
USE Film
GO
UPDATE Cinema
set CustomerPhone ='15566664444'
where CinemaName ='万达'
go
3.数据删除:
1.在Cinema表中,删除影院名称为‘万达’,且联系电话为‘15566664444’的记录
USE Film
GO
delete from Cinema
where CinemaName ='万达' and CustomerPhone ='15566664444'
go
2.在CinemaId表中,删除影厅编号为‘01’的记录
USE Film
GO
delete from CinemaId
where CinemaID ='01'
go
5.2数据查询:
5.2.1单表查询
1.查询Customer表中性别为男的姓名和手机号
select *
From CinemaId
Where CinemaType='大'
2.
select *
From CinemaId
Where CinemaNumber=30
3.
select *
From CinemaId
Where CinemaID=01
5.2.2多表查询
1.
Select Customer.CustomerName,CustomerPhone,SeatNumber,FilmTicket.Price
From Customer,FilmTicket
Where Customer.CustomerName = FilmTicket.CustomerName and Customer.CustomerName = (
Select Customer.CustomerName
From Customer,FilmTicket
Where Customer.CustomerName = '小李'
)
2.
Select Customer.CustomerName,CustomerSex,CustomerPhone,SeatNumber,FilmTicket.Price
From Customer,FilmTicket
Where Customer.CustomerName = FilmTicket.CustomerName and Customer.CustomerName = (
Select Customer.CustomerName
From Customer,FilmTicket
Where Customer.CustomerPhone='12233334444'
)
3.
Select Customer.CustomerName,CustomerSex,CustomerPhone,SeatNumber,CinemaID,FilmTicket.Price
From Customer,FilmTicket
Where Customer.CustomerName = FilmTicket.CustomerName and Customer.CustomerName = (
Select Customer.CustomerName
From Customer,FilmTicket
Where FilmTicket.SeatNumber='0722'
)