1 """
2 什么时候用主键?主键的作用?
3 保证数据的唯一性
4 一张表只能有一个主键
5 一个主键只能是一列吗?错的 可以有多列
6
7 例子:
8 create table t1(
9 nid int(11) not null auto_increment primary key,
10 pid int(11) default null,
11 num int(11) default null
12 )engine=innodb default charset=utf8;
13
14 create table t1(
15 nid int(11) not null auto_increment,
16 pid int(11) not null,
17 num int(11),
18 primary key (nid,pid) # 主键也可以这样设置
19 )engine=innodb default charset=utf8;
20
21 create table t2(
22 id int auto_increment primary key,
23 name char(10),
24 id1 int,
25 id2 int,
26 constraint fk_t1_t2 foreign key (id1,id2) references t1(nid,pid)
27 )engine=innodb default charset=utf8;
28
29 # 注意:创建多个外键进行关联的时候,可以使用以上方法,主键有两列,
30
31 数据行:
32 insert into tb1(name,age) values ("alex",18);
33 delete from tb1;
34 truncate table tb1;
35
36 delete from tb1 where id > 8
37
38 update tb1 set name="root" where id > 8
39
40 select * from tb; # 注:一般不用*号 ,* 的效率低
41 select id,name from tb;
42
43 对于自增:
44 desc 表名;# 查看表里的字段类型 以及是否为空
45 show create table 表名;# 查看这个表是如何创建的。
46 show create table 表名 \G;展现出来的格式比较规范。
47 alter table 表名 AUTO_INCREMENT=2; # 修改自增起始值 两个两个增加
48 # 注 如果AUTO_INCREMENT=8 网小改就不行了,只能往大了改
49
50 MySQL:自增步长
51 基于会话级别:
52 一次登录就是一次会话
53
54 show session variables like 'auto_inc%'; # 查看会话的步长
55 set session auto_increment_increment=2;设置会话的步长为2
56
57
58 0,唯一索引
59 create table t1(
60 id int auto_increment primary key,
61 num int,
62 unique uq1 (num) # 唯一索引
63 )engine=innodb default charset=utf8;
64
65 create table t1(
66 id int auto_increment primary key,
67 num int,
68 xx int,
69 unique uq1 (num,xx) # 联合唯一索引
70 )engine=innodb default charset=utf8;
71
72 1,外键的变种
73 a, 用户表和部门表
74 用户:
75 1 alex 1
76 2 root 1
77 3 egon 2
78 4 laoyao 3
79
80 部门:
81 1 服务
82 2 保安
83 3 公关
84 ====》一对多
85 b, 用户表和博客表
86 用户:
87 1 alex
88 2 root
89 3 egon
90 4 laoyao
91 博客表: FK() + 唯一索引
92 1 /qazwsx/ 4 (laoyao的博客)
93 2 /qwewerf/ 1 (alex的博客)
94 3 /sadsads/ 3 (egon的博客)
95 4 /dsafdsd/ 2 (root的博客)
96 ====》一对一
97 用户表和用户登陆表 一对一
98 c, 用户表(百合网) 相亲记录表
99 多对多
100
101 一对一:
102 create table userinfo(
103 id int auto_increment primary key,
104 name varchar(32),
105 gender char(12),
106 email varchar(64)
107 )engine=innodb default charset=utf8;
108
109
110 create table admin(
111 id int not null auto_increment primary key,
112 username varchar(64) not null,
113 password varchar(64) not null,
114 user_id int not null,
115 unique uq_user (user_id), 唯一索引
116 constraint fk_admin_uinfo primary key (user_id) references userinfo(id)
117 )engine=innodb default charset=utf8;
118
119
120 多对多
121 create table userinfo(
122 id int not null auto_increment primary key,
123 name varchar(12),
124 gender varchar(12)
125 )engine=innodb default charset=utf8;
126
127 create table pri_host(
128 id int not null auto_increment primary key,
129 host_name varchar(32)
130 )engine=innodb default charset=utf8;
131
132
133 create table user_host_re(
134 id int not null auto_increment primary key,
135 userid int not null,
136 hostid int not null,
137 unique uq_user_host(userid,hostid),
138 constraint fk_user_re foreign key (userid) references userinfo(id),
139 constraint fk_user_host foreign key (hostid) references pri_host(id)
140 )engine=innodb default charset=utf8;
141
142
143 SQL语句数据行操作补充
144
145 create table tb11(
146 id int auto_increment primary key,
147 name varchar(32),
148 age int
149 )engine=innodb default charset=utf8;
150
151 create table tb12(
152 id int auto_increment primary key,
153 name varchar(32),
154 age int
155 )engine=innodb default charset=utf8;
156
157
158 增:
159 insert into tb11(name,age) values("alex",12);
160
161 insert into tb11(name,age) values("alex",13),("sam",15);
162
163 insert into tb12(name,age) select name,age from tb11;
164 # (把表tb11中的数据插入表tb12中)
165
166 删:
167 delete from tb12;
168 delete from tb12 where id = 2
169 delete from tb12 where id != 2
170 delete from tb12 where id >= 2
171 delete from tb12 where id < 2 or name="alex"
172
173 改:
174 update tb12 set name="alex" where id > 12 and name="xx"
175 # (把id大于12 且名字叫“xx”的,改成“alex”)
176 update tb12 set name="alex",age=19 where id > 12 and name="xx"
177
178 查:
179 select * from tb12; (展示tb12表中所有的数据)
180
181 select id,name from tb12; (展示id,和 name 列中的数据)
182
183 select id,name from tb12 where id > 10 or name ="xxx";
184
185 select name,age,11 from tb12;
186 #(会增加一个常数列,这一列都是11)
187
188 其他:
189 select * from tb12 where id != 1
190 select * from tb12 where id <> 1 != <> 都是不等于
191 select * from tb12 where id in (1,5,12);
192 # (展示tb12 中id在(1,5,12)中的数据)
193 select * from tb12 where id not in (1,5,12);
194 select * from tb12 where id between 5 and 12;
195 # (展示id在5~12之间的数据)
196
197 select * from tb12 where id in (select id from tb11)
198 # 先看括号里的
199
200
201 % _ 都是通配符
202 select * from tb12 where name like "%a" (以a结尾)
203 select * from tb12 where name like "a%" (以a开头)
204 select * from tb12 where name like "a_" (以a结尾)
205
206 select * from tb12 limit 10; # 查看前10条数据
207 select * from tb12 limit 1,1; 第一个1 代表起始位置,第二个
208 # 位置代表取几个
209
210
211 select * from tb12 limit 10 offset 20;
212 # offset 后面的数字表示从哪里开始,limit后面的表示取几个。
213
214 排序:
215 select * from tb12
216 # 默认顺序从小到大排。
217
218 select * from tb12 order by id desc;
219 # (按照id从大到小排)
220 select * from tb12 order by id asc;
221 # (按照id从小到大排)
222
223 select * from tb12 order by id desc limit 10;
224 # (取后10条数据)
225
226 select * from tb12 order by age desc,id desc;
227 # (先按照年龄从大到小排,如果年龄有相等的,就按照id的从大到小排)
228
229 create table department5(
230 id int auto_increment primary key,
231 title varchar(32)
232 )engine=innodb default charset=utf8;
233 insert into department5(title) values("公关"),("公共")
234 #,("公公"),("关关")
235
236
237 create table userinfo5(
238 id int auto_increment primary key,
239 name char(32),
240 part_id int,
241 constraint fk_user_depar foreign key (part_id) ref
242 # erences department5(id)
243 )engine=innodb default charset=utf8;
244 insert into userinfo5(name,part_id) values("杨涵",3),
245 # ("骆驼",2),("老虎",1),("蜘蛛",1);
246
247
248 分组:
249 group by 以...进行分组
250 select * from userinfo5 group by part_id;
251 # (以part_id进行分组) # 如果其中有两个重复,不知道
252 #该留谁,所以会报错
253
254 所以你得指定都拿谁,
255 select part_id from userinfo5 group by part_id;
256 可以把* 换成part_id ,因为part_id里面有重复的序号,聚合
257 # 后,序号就会变成一个。
258 select max(id),part_id from userinfo5 group by part_id;
259 但是如果把id 也写上,但是id里没有重复的序号,所
260 #以计算机不知道该选谁,这是就需要
261 MySQL里的函数了 max(id),会选两个中比较大的那一个
262 min(id) ,part_id重复的话,会选最小的那个
263
264 select count(id),max(id),part_id from userinfo5 gr
265 #oup by part_id;
266 # count(id) 计数 给part_id计数
267
268 count
269 max
270 min
271 avg
272 sum 都是聚合函数 帮我们把重复的弄成一个出来
273
274 如果对于聚合函数的结果进行二次筛选时,必须使用having
275 select count(id),max(id),part_id from userinfo5
276 # group by part_id having count(id) > 1;
277 select count(id) as count_id,max(id),part_id from
278 # userinfo5 group by part_id having count(id) > 1;
279 可以改名 通过as
280
281
282 连表操作:
283
284 select * from userinfo5,department5 where userinfo5.part_id =
285 department5.id;
286 # (让两个表连接起来,但也不能瞎连接,俩个的id是有关联的,用等号连起来)
287
288 select * from userinfo5 left join department5 on userinfo5.part_id =
289 department5.id;
290 # left join 把两个表连接起来 # on 就代表告诉两张表的关系
291
292 select * from userinfo5 left join department5 on userinfo5.part_id =
293 department5.id;
294 # left join 就是左边的表里所有的内容全部的显示,也就是userinfo5里的全部显示
295
296 select * from userinfo5 right join department5 on userinfo5.part_id =
297 department5.id;
298 # left join 就是右边的表里所有的内容全部的显示 也就是department5 里的全部显示
299
300 select * from userinfo5 inner join department5 on userinfo5.part_id =
301 department5.id;
302 # inner join 就是将出现null时的那一行隐藏
303
304 select score.sid from score
305 left join student on score.student_id = student.sid
306 left join course on score.corse_id = course.cid
307 left join class on score.class_id = class.cid
308 left join teacher on score.teacher_id = teacher.tid
309
310 # class_id 与 teacher_id 在score表里没有,但是score表已经与其他两张
311 都表关联了,
312 其它两张表里有这两列,所以,也就相当于score表里也有了这两列,所以就可
313 以使用score.teacher_id 与 score.class_id。
314 如果列名有重复的,可以在前面加上表名就可以 例如:score.sid
315
316 select count(name) from userinfo5; # 计算name这一列的个数
317
318 # 注:本文是根据老男孩课程内容整理而成的,本文仅供个人笔记使用,如果有侵犯,请联系我,我立即撤销。
319
320 """