赞
踩
题目:自己设计一个小型数据库。
个人比较喜欢看电影,于是用Python爬了豆瓣Top250的电影,做了一个关于电影的小型数据库,这里只举一小部分作为例子。
关系模式:
电影名称关系模式 | TITLE(MNO,MNAME) |
电影评分关系模式 | RATING(MNO,MSCORE) |
电影标签关系模式 | TAG(MNO,MTAG) |
演员关系模式 | ACTOR(NAME,BIRTH,STAR,BLOOD) |
创建数据表:
- CREATE TABLE TITLE(
- MNO CHAR(10) PRIMARY KEY,
- MNAME CHAR(50) UNIQUE,
- );
-
- CREATE TABLE RATING(
- MNO CHAR(10) PRIMARY KEY,
- MSCORE FLOAT(10),
- );
-
- CREATE TABLE TAG(
- MNO CHAR(10) PRIMARY KEY,
- MTAG CHAR(20),
- );
插入数据:
- INSERT INTO TITLE(MNO,MNAME)
- VALUES
- (1,'肖申克的救赎'),
- (2,'霸王别姬'),
- (3,'这个杀手不太冷'),
- (4,'阿甘正传'),
- (5,'美丽人生'),
- (6,'千与千寻'),
- (7,'泰坦尼克号'),
- (8,'辛德勒的名单'),
- (9,'盗梦空间'),
- (10,'机器人总动员');
- SELECT * FROM TITLE
-
- INSERT INTO RATING(MNO,MSCORE)
- VALUES
- (1,9.6),
- (2,9.5),
- (3,9.4),
- (4,9.4),
- (5,9.5),
- (6,9.3),
- (7,9.3),
- (8,9.4),
- (9,9.3),
- (10,9.3);
-
- INSERT INTO TAG(MNO,MTAG)
- VALUES
- (1,'犯罪'),
- (2,'同性'),
- (3,'犯罪'),
- (4,'剧情'),
- (5,'剧情'),
- (6,'动画'),
- (7,'灾难'),
- (8,'剧情'),
- (9,'科幻'),
- (10,'动画');
(1)选择评分大于等于9.4的电影;
- SELECT TITLE.MNAME,RATING.MSCORE
- FROM TITLE,RATING
- WHERE TITLE.MNO=RATING.MNO AND MSCORE>=9.4;
(2)选择分类为'剧情'的电影;
- SELECT TITLE.MNAME,TAG.MTAG
- FROM TITLE,TAG
- WHERE TITLE.MNO=TAG.MNO AND MTAG='剧情';
(3)更新第十部电影的分类为'科幻',评分为9.1;
- UPDATE TAG
- SET MTAG='科幻'
- WHERE MNO=10;
-
- UPDATE RATING
- SET MSCORE=9.1
- WHERE MNO=10;
(4)选择排名第10的电影;
- SELECT MNAME,MTAG,MSCORE
- FROM TITLE,TAG,RATING
- WHERE TITLE.MNO=10 AND TAG.MNO=10 AND RATING.MNO=10;
(5)自然连接;
- SELECT TITLE.MNO,TITLE.MNAME,RATING.MSCORE,TAG.MTAG
- FROM TITLE,RATING,TAG
- WHERE TITLE.MNO=RATING.MNO AND RATING.MNO=TAG.MNO;
(6)删除排名第十的电影;
- DELETE
- FROM TITLE
- WHERE TITLE.MNO=10;
-
- DELETE
- FROM RATING
- WHERE RATING.MNO=10;
-
- DELETE
- FROM TAG
- WHERE TAG.MNO=10;
-
- SELECT TITLE.MNO,TITLE.MNAME,RATING.MSCORE,TAG.MTAG
- FROM TITLE,RATING,TAG
- WHERE TITLE.MNO=RATING.MNO AND RATING.MNO=TAG.MNO;
完整程序:
- CREATE DATABASE ***;
-
- CREATE TABLE TITLE(
- MNO CHAR(10) PRIMARY KEY,
- MNAME CHAR(50) UNIQUE,
- );
-
- CREATE TABLE RATING(
- MNO CHAR(10) PRIMARY KEY,
- MSCORE FLOAT(10),
- );
-
- CREATE TABLE TAG(
- MNO CHAR(10) PRIMARY KEY,
- MTAG CHAR(20),
- );
-
- INSERT INTO TITLE(MNO,MNAME)
- VALUES
- (1,'肖申克的救赎'),
- (2,'霸王别姬'),
- (3,'这个杀手不太冷'),
- (4,'阿甘正传'),
- (5,'美丽人生'),
- (6,'千与千寻'),
- (7,'泰坦尼克号'),
- (8,'辛德勒的名单'),
- (9,'盗梦空间'),
- (10,'机器人总动员');
- SELECT * FROM TITLE
-
- INSERT INTO RATING(MNO,MSCORE)
- VALUES
- (1,9.6),
- (2,9.5),
- (3,9.4),
- (4,9.4),
- (5,9.5),
- (6,9.3),
- (7,9.3),
- (8,9.4),
- (9,9.3),
- (10,9.3);
-
- INSERT INTO TAG(MNO,MTAG)
- VALUES
- (1,'犯罪'),
- (2,'同性'),
- (3,'犯罪'),
- (4,'剧情'),
- (5,'剧情'),
- (6,'动画'),
- (7,'灾难'),
- (8,'剧情'),
- (9,'科幻'),
- (10,'动画');
-
- --选择评分大于等于9.4的电影
- SELECT TITLE.MNAME,RATING.MSCORE
- FROM TITLE,RATING
- WHERE TITLE.MNO=RATING.MNO AND MSCORE>=9.4;
-
- --选择分类为'剧情'的电影
- SELECT TITLE.MNAME,TAG.MTAG
- FROM TITLE,TAG
- WHERE TITLE.MNO=TAG.MNO AND MTAG='剧情';
-
- --更新第十部电影的分类为'科幻',评分为9.1
- UPDATE TAG
- SET MTAG='科幻'
- WHERE MNO=10;
-
- UPDATE RATING
- SET MSCORE=9.1
- WHERE MNO=10;
-
- --选择排名第10的电影
- SELECT MNAME,MTAG,MSCORE
- FROM TITLE,TAG,RATING
- WHERE TITLE.MNO=10 AND TAG.MNO=10 AND RATING.MNO=10;
-
- --自然连接
- SELECT TITLE.MNO,TITLE.MNAME,RATING.MSCORE,TAG.MTAG
- FROM TITLE,RATING,TAG
- WHERE TITLE.MNO=RATING.MNO AND RATING.MNO=TAG.MNO;
-
- --删除排名第十的电影
- DELETE
- FROM TITLE
- WHERE TITLE.MNO=10;
-
- DELETE
- FROM RATING
- WHERE RATING.MNO=10;
-
- DELETE
- FROM TAG
- WHERE TAG.MNO=10;
-
- SELECT TITLE.MNO,TITLE.MNAME,RATING.MSCORE,TAG.MTAG
- FROM TITLE,RATING,TAG
- WHERE TITLE.MNO=RATING.MNO AND RATING.MNO=TAG.MNO;
版权声明:本文为博主原创文章,未经博主允许不得转载。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。