2016.9.23小程序---数据库
时间:2021-07-01 10:21:17
帮助过:2人阅读
CREATE TABLE t_student (
2 s_id
INT PRIMARY KEY AUTO_INCREMENT,
3 s_name
VARCHAR(
20)
UNIQUE,
4 s_sex
VARCHAR(
2)
DEFAULT ‘男‘ CHECK (s_sex
IN (
‘男‘,
‘女‘) ) ,
5 s_age
INT,
6 s_class
VARCHAR(
10)
7
8 );
9 SELECT * FROM t_student;
10 DROP TABLE t_student;
11
12 INSERT INTO t_student(s_name,s_sex,s_age,s_class)
13 VALUES (
‘张三‘,
‘男‘,
21,
‘T130‘),
14 (
‘刘伯伯‘,
‘男‘,
47,
‘T130‘),
15 (
‘刘基‘,
‘男‘,
11,
‘T130‘),
16 (
‘张玲‘,
‘女‘,
23,
‘T130‘),
17 (
‘张伯山‘,
‘男‘,
35,
‘T130‘),
18 (
‘李二虎‘,
‘男‘,
79,
‘T130‘),
19 (
‘唐梅‘,
‘女‘,
31,
‘T130‘),
20 (
‘李四‘,
‘男‘,
16,
‘T130‘),
21 (
‘李菲‘,
‘女‘,
19,
‘T130‘),
22 (
‘张亚亚‘,
‘女‘,
18,
‘T130‘),
23 (
‘王五‘,
‘男‘,
26,
‘T130‘),
24 (
‘王一伯‘,
‘男‘,
8,
‘T130‘),
25 (
‘刘伯承‘,
‘男‘,
27,
‘T130‘),
26 (
‘张俊‘,
‘男‘,
19,
‘T130‘),
27 (
‘刘珊珊‘,
‘女‘,
25,
‘T130‘),
28 (
‘钱仲‘,
‘男‘,
66,
‘T130‘),
29 (
‘王君妹‘,
‘女‘,
27,
‘T130‘),
30 (
‘周伯珊‘,
‘女‘,
25,
‘T130‘),
31 (
‘吴糖‘,
‘女‘,
18,
‘T130‘);
32 -- 1.把刘基的名字修改为刘伯温
33 UPDATE t_student
SET s_name
=‘刘伯温‘ WHERE s_name
=‘刘基‘;
34 -- 2.唐伯虎年龄20,性别男,班级为T130,添加到表中
35 INSERT INTO t_student(s_name,s_sex,s_age,s_class)
VALUES (
‘唐伯虎‘,
‘男‘,
20,
‘T130‘);
36 -- 3.查询出所有姓名包括伯的所有的人员的信息
37 SELECT * FROM t_student
WHERE s_name
LIKE ‘%伯%‘;
38 -- 4,查询年龄在10-20之间的所有人员的信息
39 SELECT * FROM t_student
WHERE s_age
>10 AND s_age
<20;
40 -- 5,查询前5条年龄在10-20之间的所有人员的信息
41 SELECT * FROM t_student
WHERE s_age
>10 AND s_age
<20 LIMIT
0,
5
42 -- 6,将年龄在10-20之间的所有人员,每个人的年龄加10
43 UPDATE t_student
SET s_age
=s_age
+10 WHERE s_age
>10 AND s_age
<20
44 -- 7、查询T130班所有的男生。
45 SELECT * FROM t_student
WHERE s_sex
=‘男‘ AND s_class
=‘T130‘
46 -- 8、删除id为3的记录
47 DELETE FROM t_student
WHERE s_id
=3
48 -- 9、显示所有人员信息,并显示所处年龄等级:18岁以下为未成年,18-30为青年,30-50为壮年,50以上为老年
49 SELECT u.
*,(
CASE WHEN s_age
<18 THEN ‘未成年‘
50 WHEN s_age
>=18 AND s_age
<30 THEN ‘青年‘
51 WHEN s_age
>=30 AND s_age
<50 THEN ‘壮年‘
52 ELSE ‘老年‘ END
53
54 ) ageGrade
FROM t_student u;
2016.9.23小程序---数据库
标签: