一、其它(共18题,100分)
1、创建数据库user_system
CREATE DATABASE user_system
2、在数据库user_system中创建数据表user及user_ext,其中user是用户表,user_ext是用户扩展表。表结构如下所示:
-- 创建user用户表
CREATE TABLE user(
id INT PRIMARY KEY auto_increment,
name VARCHAr(20) NOT NULL,
score DECIMAL(4,1),
sex VARCHAr(2)
);
-- 创建user_est用户扩展表
CREATE TABLE user_ext(
id INT PRIMARY KEY auto_increment,
age INT NOT NULL,
height DECIMAL(4,1),
weight DECIMAL(4.1)
);
3、在数据表user和user_ext中添加数据,添加的数据如下所示:
为方便大家快速插入数据,给出相应的值列表:
(1,'小明',100,'男');
(2,'小红',98,'女');
(3,'小聪',80,'男');
(4,'小亮',60,'男');
(5,'小伟',98,'女');
(6,'小琳',92,'女');
单条插入语法:
INSERT INTO user VALUES (1,'小明',100,'男');
INSERT INTO user VALUES (2,'小红',98,'女');
INSERT INTO user VALUES (3,'小聪',80,'男');
INSERT INTO user VALUES (4,'小亮',60,'男');
INSERT INTO user VALUES (5,'小伟',98,'女');
INSERT INTO user VALUES (6,'小琳',92,'女');
多条插入语法:
INSERT INTO user VALUES
(1,'小明',100,'男'),
(2,'小红',98,'女'),
(3,'小聪',80,'男'),
(4,'小亮',60,'男'),
(5,'小伟',98,'女'),
(6,'小琳',92,'女');
(1,11,135,40);
(2,9,145,30);
(3,10,134,38);
(4,11,142,45);
(5,12,139,32);
(6,11,140,33);
(7,14,160,45);
单条插入语法:
INSERT INTO user_ext VALUES (1,11,135,40);
INSERT INTO user_ext VALUES (2,9,145,30);
INSERT INTO user_ext VALUES (3,10,134,38);
INSERT INTO user_ext VALUES (4,11,142,45);
INSERT INTO user_ext VALUES (5,12,139,32);
INSERT INTO user_ext VALUES (6,11,140,33);
INSERT INTO user_ext VALUES (7,14,160,45);
多条插入语法:
INSERT INTO user_ext VALUES
(1,11,135,40),
(2,9,145,30),
(3,10,134,38),
(4,11,142,45),
(5,12,139,32),
(6,11,140,33),
(7,14,160,45);
在user表中添加外键约束:
ALTER TABLE user ADD CONSTRAINT id_fk FOREIGN KEY(id) REFERENCES user_ext(id)
4、在user表中修改“小聪”的成绩为“88”
UPDATe user SET score=88 WHERe name='小聪'
5、在user_ext表中删除编号为“7”的记录
DELETE FROM user_ext WHERe id=7
6、查询表user的所有内容
SELECt * FROM user;
7、查询出来user表中成绩大于80的所有数据
SELECt * FROM user WHERe score>80
8、查询表user中性别为'男'的所有内容
SELECt * FROM user WHERe sex='男'
9、查询表user中姓名开头不是为'小'字的内容
SELECt * FROM user WHERe name NOT LIKE '小%'
10、查询表user中姓名包含'聪'字的所有内容
SELECt * FROM user WHERe name LIKE '%聪%'
11、查询表user 中成绩为98,60,92的所有内容
SELECt * FROM user WHERe score IN(98,60,92)
12、查询表user中成绩大于95 或者性别为女性的所有内容
SELECt * FROM user WHERe score>95 OR sex='女'
13、获取表user中字段成绩大于60的内容数量
SELECt COUNT(*) FROM user WHERe score>60
14、获取表user中成绩的平均值
SELECt avg(score) 平均成绩 FROM user
15、获取表user中成绩的总分数
SELECt sum(score)总分数 FROM user
16、获取表user_ext中所有不同的字段 age ,并设置字段别名为'年龄'
SELECt DISTINCT(age)年龄 FROM user_ext;
17、获取表user_ext中的所有数据并且按照字段体重进行倒序排序
SELECt * FROM user_ext ORDER BY weight DESC
18、通过左连接 获取表 user(别名t1) 和表 user_ext(别名t2) 中字段 id 相同的数据,其中字段 age 大于9,并仅返回 id、students、age、weight 这几个字段的数据
SELECt t1.id,name,age,weight FROM user t1 LEFT JOIN user_ext t2 ON t1.id=t2.id