1 delete 语句嵌套子查询:
2
3 delete from 表名1 where 列名 操作符 (select 列名 from 表名2 where 条件);
4
5 示例:
6 delete from customers_1 where salary > (select avg(salary) from customers_1);[删除一个之后还会在重新计算avg(salary)之后再重新执行执行该语句]
7 delete from customers_1 where age in (27,32);
8 ***delete from customers_2 where salary > (select avg(salary) from customers_1); 这个可以用
9
10 update customers_2 set salary=salary*2 where age > (select avg(age) from customers_1)
11
12 SQL函数
13 1.SQL aggregate 聚合函数
14 avg():average 平均值
15 count(): 行数
16 max(): 最大值
17 min(): 最小值
18 sum(): 求和
19
20 2.SQL Scalar 标量函数
21 ucase():upper case 以大写字母显示
22 lcase():lower case 以小写字母显示
23 length(): 求字符长度
24 round(): 对某个数字字段进行指定小数位数的四舍五入
25 now(): 返回当前的系统日期和时间
26 format(): 格式化某个字段的显示方式
27
28 思考题:
29 1.求出customers表中,地址为北京的顾客的总收入
30 select * from customers where address ="beijing" and (select sum(salary)from customers );
31 select sum(salary) from customrs where address="beijing";
32
33 2.选出customers表中,工资大于所有顾客平均工资的人,并且以大写字母显示,以小写字母显示住址
34 提示:select ucase(),lcase()where
35 select ucase(name),lase(address) from cuatomers where salary >(select avg(salary) from customers);
36
37 3.列出customers表中的工资(只取整数部分),工资的位数,以及当前系统的时间
38 select round(salary,0),length(salary),now() from customers;
39 select round(salary,0),length(salary)-3,now() from customers;
40
41 4.列出customers表中,每个顾客工资与所有顾客的平均工资
42 提示分两步做
43 select avg(salary)from customers;先求出平均工资的数然后在
44 select name,salary-[上面求出的数字] from customers;
45 select name,salary -(select avg(salary) from customers) as difference
46 [是把salary -(select avg(salary) from customers) 重命名为didderence] from customers;
47
48
49 mid(列名,起始位置,长度)
50 substring(列名,起始位置,长度)
51 以上两个语句在这个版本是没有啥区别的
52
53 select mid (name,1,3) from customers;
54 1.从起始位置为1开始
55 2.打印出三个字母的长度
56 注意:第一个字母的位置为1,不是0
57
58 ***as 别名;
59 select formate (列名,新的格式) from 表名;
60 select date_format(now(),"%Y-%M-%D") as date from customers;
61
62 Union:综合多个select语句,且返回不重复的行
63 如果要返回重复的行用union all
64 注意:
65 1.每个select语句中必须选中相同数量的列
66 2.列的数目要一致
67 3.列的数据类型要一致
68 4.列的顺序要一致
69
70 例如:
71 选中customers表中,年龄小于25岁和大于27岁的人的姓名和年龄
72 1.select * from customers where age <25 or age>27;
73
74 2.select * from customers where age <25 union select * from customers where age>27;
75
76 思考题:
77 选择出customers 表中,所有顾客的工资,并且最后一行打印出顾客的总工资
78 select salary from customers union select sum(salary) from customers;
79
80 alter table 向表中添加一个新的列
81
82 alter table 表名 add 列名 数据类型
83 alter table customers add gender char(20);
84 alter table customers add gender char(20) default "F";
85
86
87 向表中删除一个列
88 alter table 表名 drop column 列名;
89 alter table customers drop column gender;
90
91 修改表中某列的数据类型
92 alter table 表名 modify column 列名 新的数据类型
93
94 alter table customers modify column salary float;
95
96 微观修改表中的元素[改变行] 宏观修改表中的元素[改变列]
97 修改 update alter ..modify column..
98 删除 delete alter ..drop column..
99 添加 insert alter ..add column..
100
101 对表取消主键限制
102 alter table 表名 drop primary key;
103
104 show creat table courses;
105 alter table courses drop primary key;
106 show create table corses;
107
108 对表添加主键限制
109 alter table 表名 add primary key(列名);
110 alter table customers add primary key(ID);
111
112 对表取消外键限制:
113 alter table 表名 drop foreign key 列名;
114 alter table courses drop primary key instructor;
115
116 对表添加外键限制
117 alter table 表名 add foreign key (列名) references 表名(列名);
118
119 对表添加非空限制:
120 alter table 表名 modify 列名 数据类型 not null;
121 alter table courses modify name varchar(20) not null;
122
123 对表删除非空限制
124 alter table 表名 modify 列名 数据类型;
125 alter table 表名 modify 列名 数据类型 default null;
126 alter table courses modify name varchar(20) default null;
127
128 *********
129 truncate table:保留表头,也即保留表的格式,只删除表中存放的数据内容
130 truncate table 表名;
131 truncate table customers_1;
132 delete from customers_1;[和truncate table功能一样,一般常与where条件语句一起使用]
133
134 drop table customers_1: 连表头带表中的内容全部删除
135
136 思考题:
137 查询customers表中,城市的平均工资大于3000的城市名和工资信息
138 select address,salary,avg(salary) from customers having avg>3000 where (select address from customers);
139
140 学生疑问1;
141 删除外键必须用show create table 查看系统自动生成的外键名(如:courses_ibfk_1,而不是instructor),用该生成的新名字来删除外键,例如
142 alter table courses drop foreign key courses_ibfk_1;
143 alter table courses add foreign key(instructor) references teachers(name);
144
145 学生疑问2
146 alter table 表名 rename 新表名;
147 alter table customers rename customers_update;
148
149 学生疑问3
150 alter table 表名 change 旧名字 新名字 数据类型;
151 alter table courses change ID c_id int;
152
153 执行事务:
154 事务机制:确保数据一致性
155 事务的4个属性:
156 1.原子性:一个事务是一个不可分割的工作单位,事务中包括的各个操作要么都做,要么都不做
157 2.一致性:事务必须是数据库从一个一致性状态变到另一个一致性状态
158 3.隔离性:一个事务的执行不能被其他并发的事务打扰
159 4.持久性:一个事务一旦提交,它对数据库中数据的改变就应该是永久性的
160 注意:
161 1.MYSQL中默认aql语句的结果自动commit到数据库
162 2.开始需要写start transaction,这样自动commit就会被禁用,知道我们使用commit或rollback终止这个transaction
163 start transaction
164 sql1
165 sql2
166 ...
167 sqln
168 commit;
169 示例:
170 start transction;
171 insert into teachers values("wei","m","instructor");
172 insert into teachers values("wang","f","instructor");
173 select * from teachers;
174 commit;
175
176 回滚的演示:
177 start transction;
178 insert into teachers values("han","m","instructor");
179 rollback;[删除上面的数据]
180
181 python3与mysql
182 pysql库:
183
184 import pymysql
185 #打开数据库链接(commect链接)
186 db = pymysql.connect("localhost","root","123456","testDB")
187 #使用cursor()方法创建一个游标对象cursor
188 cursor =db.cursor()
189 #使用execute()方法执行sql查询返回值为表中所有记录的总数
190 SQL="select * from customers;"
191 cursor.execute(SQL)
192 #等价于cursor.execute("select * from customers;")
193 #使用fetchone()方法获得单条数据
194 data =cursor.fetchone()#[相当于迭代器只打印一行,超出行数不会报错也啥都不显示]
195 #注意,返回的data是保存在元组tuple中,用()包起来
196 print ("The first row in the table is:",data)
197
198 sql = "drop table customers_bkp;"#是将生成的表格进行删除,如果不删除再次运行表格已经存在就会报错
199
200 cursor.execute(sql)
201 #写sql语句,创建一个新的表格,叫customers_bkp包括id,name,age,address,salary,gender
202 SQL_CREATE="create table customers_bkp(\
203 Id int,\
204 name varchar(20),\
205 age int,\
206 address char(25),\
207 salary float\
208 );"
209
210 #使用execute()方法执行上面的sql语句
211 cursor.execute(SQL_CREATE)
212
213 #写sql语句,像customers_bkp表中插入行,完成对customers表内容的复制
214 #提示insert into ...select * from .....
215 SQL_INSERT="insert into customers_bkp\
216 (id,name,age,address,salary)\
217 select * from customers;"
218
219 cursor.execute(SQL_INSERT)#[插入表格之后还的执行之后才会出现上面语句的结果]
220
221
222 #使用execute()方法执行上面的sql语句
223 cursor.execute("select * from customers_bkp;")
224
225 #fetchall()接收全部的返回结果,即表中所有的行,返回结果存在元组中
226 data2 = cursor.fetchall()#[把上面的表格数据以元组形式返回]
227 #rowcount是一个只读属性,返回执行execute()方法影响的行数
228 print(cursor.rowcount)#[返回执行execute()的行数]
229 #打印
230 for row in data2:
231 id=row[0]
232 name=row[1]
233 age=row[2]
234 address=row[3]
235 salary=row[4]
236 print(str(id),name,str(age),address,str(salary))
237 #提交到数据库内执行
238 db.commit()
239 #最后关闭数据库
240 db.close()
241
242
243 思考题:
244 把customers表中工资大于所有顾客的平均工资的人的姓名和工资还有地址打印出来
245 要求利用nysql库完成
246 select name,address,salary from customers where salary>(select avg(salary) from customers);
247
248 import pymysql
249 db=pymysql.connect("localhost","root","123456","testDB")
250 c =db.cursor()
251 sql="select name,address,salary from customers where salary>(select avg(salary) from customers);"
252 c.execute(sql)
253 d=c.fetchall()
254 # print(d)
255 for x in d:
256 name=x[0]
257 address=x[1]
258 salary=x[2]
259 print(name,address,salary)
260 db.commit
261 db.close()
262
263
264 ******select address,sum(salary) from customers group by address having address in("beijing","shenzhen");