1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* Author: Chenhui Wang
Date: 2018-05-27 */

-- 一种设置唯一性表格的方法
create table person(
first_name char(20) not null,
last_name char(20) not null,
sex char(10),
primary key (last_name, first_name) -- First approach
)

-- 这样如果插入两个相同的数据,就会报错

create table person(
first_name char(20) not null,
last_name char(20) not null,
sex char(10),
UNIQUE (last_name, first_name) -- Second approach
)
-- 相同的效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
-- 如果想统计重复的数据,用系统内置函数count(*)
/*
person table
+------------+-----------+--------+
| first_name | last_name | sex |
+------------+-----------+--------+
| Chenhui | Wang | Male |
| Chenhui | Wang | Male |
| Chenhui | Wang | Male |
| Huiqian | Ying | Female |
| Huiqian | Ying | Female |
| Jiahuan | He | Female |
| Luting | Sun | Female |
+------------+-----------+--------+
*/
select count(*) as repetitions, first_name, last_name from person group by last_name, first_name having repetitions > 1;
/*
+-------------+-----------+------------+
| repetitions | last_name | first_name |
+-------------+-----------+------------+
| 3 | Wang | Chenhui |
| 2 | Ying | Huiqian |
+-------------+-----------+------------+
*/
select first_name, count(*) from person group by first_name;
/*
+------------+----------+
| first_name | count(*) |
+------------+----------+
| Chenhui | 3 |
| Huiqian | 2 |
| Jiahuan | 1 |
| Luting | 1 |
+------------+----------+
*/
-- 去除重复元素,比如person列表中重复的
create table tmp select first_name, last_name, sex from person group by first_name, last_name, sex;
drop table person;
alter table tmp rename to person;
-- 或者
create table person_clone select distinct first_name, last_name, sex from person; --感觉这种最好
/*
+------------+-----------+--------+
| first_name | last_name | sex |
+------------+-----------+--------+
| Chenhui | Wang | Male |
| Huiqian | Ying | Female |
| Jiahuan | He | Female |
| Luting | Sun | Female |
+------------+-----------+--------+
*/

-- 导出table
select * from person into outfile 'C:/Users/Link/Desktop/ass.txt' -- 格式很乱
select * from person into outfile 'C:/Users/Link/Desktop/ass.txt' fields terminated by ',', enclosed by '"' lines terminated by '\r\n';
/*
结果会生成一个txt文件,内容如下
"Chenhui","Wang","Male"
"Chenhui","Wang","Male"
"Chenhui","Wang","Male"
"Huiqian","Ying","Female"
"Huiqian","Ying","Female"
"Jiahuan","He","Female"
"Luting","Sun","Female"
*/
-- 在多个表进行操作时,如果操作的是两个表共有的属性,要加.表示身份
/*
instructor
+-------+------------+-----------+----------+
| ID | name | dept_name | salary |
+-------+------------+-----------+----------+
| 10101 | Srinivasan | Comp.Sci. | 65000.00 |
| 12121 | Wu | Finance | 90000.00 |
| 15151 | Mozart | Music | 40000.00 |
| 22222 | Einstein | Physics | 95000.00 |
| 32343 | El Said | History | 60000.00 |
| 33456 | Gold | Physics | 87000.00 |
| 45565 | Katz | Comp.Sci. | 75000.00 |
| 58583 | Califieri | History | 62000.00 |
| 76543 | Singh | Finance | 80000.00 |
| 83821 | Brandt | Comp.Sci. | 92000.00 |
| 98345 | Kim | Elec.Eng. | 80000.00 |
+-------+------------+-----------+----------+

department
+-----------+----------+-----------+
| dept_name | building | budget |
+-----------+----------+-----------+
| Biology | Watson | 90000.00 |
| Comp.Sci. | Taylor | 100000.00 |
| Elec.Eng. | Taylor | 85000.00 |
| Finance | Painter | 120000.00 |
| History | Painter | 50000.00 |
| Music | Packard | 80000.00 |
| Physics | Watson | 70000.00 |
+-----------+----------+-----------+

teaches
+-------+-----------+--------+----------+------+
| ID | course_id | sec_id | semester | year |
+-------+-----------+--------+----------+------+
| 10101 | CS-101 | 1 | Fall | 2009 |
| 45565 | CS-101 | 1 | Spring | 2010 |
| 83821 | CS-190 | 1 | Spring | 2009 |
| 83821 | CS-190 | 2 | Spring | 2009 |
| 10101 | CS-315 | 1 | Spring | 2010 |
| 45565 | CS-319 | 1 | Spring | 2010 |
| 10101 | CS-347 | 1 | Fall | 2009 |
| 10101 | EE-181 | 1 | Spring | 2009 |
| 98345 | EE-181 | 1 | Spring | 2009 |
| 12121 | FIN-201 | 1 | Spring | 2010 |
| 32343 | HIS-351 | 1 | Spring | 2010 |
| 15151 | MU-119 | 1 | Spring | 2010 |
| 22222 | PHY-001 | 1 | Fall | 2009 |
+-------+-----------+--------+----------+------+
course
+-----------+---------------------------+-----------+---------+
| course_id | title | dept_name | credits |
+-----------+---------------------------+-----------+---------+
| BIO-101 | Intro.to Biology | Biology | 4 |
| BIO-301 | Genetics | Biology | 4 |
| BIO-399 | Computational Biology | Biology | 3 |
| CS-101 | Intro.to Computer Science | Comp.Sci. | 4 |
| CS-190 | Game Design | Comp.Sci. | 4 |
| CS-315 | Robotics | Comp.Sci. | 3 |
| CS-319 | Image Processing | Comp.Sci. | 3 |
| CS-347 | Database System Concepts | Comp.Sci. | 3 |
| EE-181 | Intro.to Digital Systems | Elec.Eng. | 3 |
| FIN-201 | Investment Banking | Finance | 3 |
| HIS-351 | World History | History | 3 |
| MU-119 | Music Video Production | Music | 3 |
| PHY-001 | Physical Principles | Physics | 4 |
+-----------+---------------------------+-----------+---------+
*/
select name, instructor.dept_name, building from instructor, department where instructor.dept_name = department.dept_name
/*

result
+------------+-----------+----------+
| name | dept_name | building |
+------------+-----------+----------+
| Srinivasan | Comp.Sci. | Taylor |
| Katz | Comp.Sci. | Taylor |
| Brandt | Comp.Sci. | Taylor |
| Kim | Elec.Eng. | Taylor |
| Wu | Finance | Painter |
| Singh | Finance | Painter |
| El Said | History | Painter |
| Califieri | History | Painter |
| Mozart | Music | Packard |
| Einstein | Physics | Watson |
| Gold | Physics | Watson |
+------------+-----------+----------+

*/
-- Natural join 自然连接
-- 对所有教师,找出他们的姓名以及所讲的课的代码
select name, course_id from instructor, teaches where instructor.ID = teaches.ID;
/*
result
+------------+-----------+
| name | course_id |
+------------+-----------+
| Srinivasan | CS-101 |
| Srinivasan | CS-315 |
| Srinivasan | CS-347 |
| Srinivasan | EE-181 |
| Wu | FIN-201 |
| Mozart | MU-119 |
| Einstein | PHY-001 |
| El Said | HIS-351 |
| Katz | CS-101 |
| Katz | CS-319 |
| Brandt | CS-190 |
| Brandt | CS-190 |
| Kim | EE-181 |
+------------+-----------+
*/
-- 或者用natural join,其本质是用第一个关系的每个元组去与第二个关系的元组进行连接,但是元组要在公共属性上取值一样才会被连接。
select name, course_id from instructor natural join teaches; -- 结果和上面一样
-- 下面展示一般的笛卡尔积和natural join有什么不同
select * from instructor join teaches; -- 普通的笛卡尔积,即用第一个关系的每个元组去与第二个关系的元组进行连接,会有mxn个结果,m,n分别是第一,二个元组个数
select * from instructor natural join teaches;
/*
result
+-------+------------+-----------+----------+-----------+--------+----------+------+
| ID | name | dept_name | salary | course_id | sec_id | semester | year |
+-------+------------+-----------+----------+-----------+--------+----------+------+
| 10101 | Srinivasan | Comp.Sci. | 65000.00 | CS-101 | 1 | Fall | 2009 |
| 10101 | Srinivasan | Comp.Sci. | 65000.00 | CS-315 | 1 | Spring | 2010 |
| 10101 | Srinivasan | Comp.Sci. | 65000.00 | CS-347 | 1 | Fall | 2009 |
| 10101 | Srinivasan | Comp.Sci. | 65000.00 | EE-181 | 1 | Spring | 2009 |
| 12121 | Wu | Finance | 90000.00 | FIN-201 | 1 | Spring | 2010 |
| 15151 | Mozart | Music | 40000.00 | MU-119 | 1 | Spring | 2010 |
| 22222 | Einstein | Physics | 95000.00 | PHY-001 | 1 | Fall | 2009 |
| 32343 | El Said | History | 60000.00 | HIS-351 | 1 | Spring | 2010 |
| 45565 | Katz | Comp.Sci. | 75000.00 | CS-101 | 1 | Spring | 2010 |
| 45565 | Katz | Comp.Sci. | 75000.00 | CS-319 | 1 | Spring | 2010 |
| 83821 | Brandt | Comp.Sci. | 92000.00 | CS-190 | 1 | Spring | 2009 |
| 83821 | Brandt | Comp.Sci. | 92000.00 | CS-190 | 2 | Spring | 2009 |
| 98345 | Kim | Elec.Eng. | 80000.00 | EE-181 | 1 | Spring | 2009 |
+-------+------------+-----------+----------+-----------+--------+----------+------+
*/
-- instructor 与 teaches公共属性为id,所以只进行id属性上的笛卡尔积

-- 列出教师名字和所教授课程的名字
select name, title from instructor, course where instructor.dept_name = course.dept_name; --这种方法不行,因为没有哪个教师教哪门课的信息
select name, title from instructor natural join teaches, course where teaches.course_id = course.course_id; -- instructor先与teaches做自然连接,再与course匹配。