MySQl之最全且必会的sql语句

  1. 创建一个名称为mydb1的数据库,如果有mydb1数据库则直接使用,如果无则创建mydb1数据库  
  2. create database if not exists mydb1;  
  3. create database if not exists mydb1;  
  4.   
  5. 创建一个使用UTF8字符集的mydb2数据库,注意这里不是UTF-8  
  6. create database if not exists mydb2 character set UTF8;  
  7. create database if not exists mydb2 character set UTF8;  
  8.   
  9. 创建一个使用UTF8字符集,并带校对规则的mydb3数据库  
  10. create database if not exists mydb3 character set UTF8 collate utf8_general_ci;  
  11.   
  12. 校对规则:是数据库表中的所有记录按什么方式存储数据的先后顺序,例如:a在前,z在后  
  13. 字符集与校对规则是一一对应,不能乱改  
  14. 如果不写校对规则的话,默认是[对应字符集]默认的校对规则。参考<<MySQL5手册--10.10.10>>  
  15.   
  16. 查看当前数据库服务器中的所有数据库  
  17. show databases;  
  18.   
  19. 查看前面创建的mydb1数据库的定义信息  
  20. show create database mydb1;  
  21. show create database mydb1;  
  22.   
  23. 删除前面创建的mydb1数据库,如果有mydb1则删除  
  24. drop database if exists mydb1;  
  25. drop database if exists mydb1;  
  26.   
  27. 使用mydb2数据库  
  28. use mydb2;  
  29. use mydb2;  
  30.   
  31. 查看数据库服务器中的数据库,并把其中mydb3库的字符集修改为GBK  
  32. alter database mydb3 character set GBK;  
  33. alter database mydb3 character set GBK;  
  34.   
  35. 以下代码是在mydb2数据库中创建users表,并插入几条记录,学员们暂时不用理会  
  36. create table if not exists users(  
  37.     name varchar(10)  
  38. );  
  39. insert into users values('XX');  
  40. insert into users values('YY');  
  41. insert into users values('ZZ');  
  42.   
  43. 备份mydb1库中的数据到e:/xx.sql的文件中,以便将来恢复之用  
  44. window7(先exit到windows环境)备份:mysqldump -uroot -p mydb1 > e:\mydb1.sql回车  
  45.                                     mysqldump -uroot -p mydb1 > d:/myydb1.sql  
  46. mysqldump是mysql提供的用于备份数据库的命令  
  47. mysqldump命令必须window环境运行  
  48. source命令中mysql环境运行  
  49.   
  50. mysql恢复:source e:\mydb1.sql回车  
  51. 注意:恢复时,[先创建数据库]并使用该数据库,[再]通过source命令,因为sql文件中[只有]表信息,[无]数据库信息  
  52.   
  53. 创建一个users表,包含id/username/password/birthday/salary  
  54. create table if not exists users(  
  55.     id int(4),  
  56.     username varchar(10),  
  57.     password varchar(6),  
  58.     birthday datetime,  
  59.     salary float(6,2)  
  60. );  
  61.   
  62.   
  63. float(6,2)表示:2表示显示时小数点后最多几位,超过2位,四舍五入;  
  64.                 6表示整数和小数最多几位,整数部份最多(6-2)位  
  65. 以上表中的所有字段都允许为NULL,且默认NULL  
  66. 如果数据显示出来是乱码,按如下步骤:  
  67.   
  68. 向users表中插入一条记录,先英后中(无法插入中文)  
  69. insert into users values(1,'jack','123456','2015-8-8 8:8:8',9999.99);  
  70. insert into users values(2,'哈哈','123456','2015-9-9 9:9:9',9999.99);  
  71. insert into users values(3,'呵呵','123456','2015-7-7 7:7:7',7777.77);  
  72.   
  73. 查询users表的结构  
  74. desc users;  
  75.   
  76. 创建employees表------------------------------用employees.sql表  
  77.   
  78. 在上面员工表的基本上增加一个image列  
  79. alter table employees  
  80. add image blob;  
  81. alter table employees add image blob;  
  82.   
  83. 企业中,不是真真正正存照片本身,而是存照片路径,即E:/zgl.jpg  
  84.   
  85. 修改name列,使其长度为60  
  86. alter table employees  
  87. modify name varchar(60);  
  88.   
  89. alter table employees  
  90. modify name varchar(60);  
  91.   
  92. 删除image列  
  93. alter table employees  
  94. drop column image;  
  95.   
  96. alter table employees  
  97. drop column image;  
  98. 如果表中有内容的话,删除或添加某列,不影响其它列的信息  
  99.   
  100. 表名employees改为staff  
  101. rename table employees to staff;  
  102. rename table employees to staff;  
  103.   
  104. 修改表的字符集为GBK  
  105. alter table staff  
  106. character set UTF8/GBK/GB2312;  
  107.   
  108. 列名name修改为username  
  109. alter table staff  
  110. change column name username varchar(60);  
  111. alter table staff  
  112. change column name username varchar(60);  
  113.   
  114. 向staff表中插入数据  
  115. insert into staff values(3,'赵君','男','2005-1-1',3333.33,'2005-7-1','这是备注信息');  
  116.   
  117. 显示插入NULL  
  118. insert into employees values(1,'哈哈','男','2015-1-1',1111.11,'2015-5-5','这是备注信息',NULL);  
  119.   
  120. 隐式插入NULL  
  121. insert into employees(id,name,sex,birthday,salary,resume)   
  122. values(2,'呵呵','男','2015-1-1',2222.22,'这是备注信息');  
  123.   
  124. 如果存在表就删除表  
  125. drop table if exists staff;//表不在了  
  126. drop table if exists staff;  
  127.   
  128. truncate table users;//表还在,只不过没有内容了  
  129.   
  130. 再重新创建表  
  131. create table staff(  
  132.   id int(5),  
  133.   name varchar(6),  
  134.   sal int(5)  
  135. );  
  136. insert into staff values(1,'哈哈',7000);  
  137. insert into staff values(2,'呵呵',8000);  
  138. insert into staff values(3,'嘻嘻',9000);  
  139. insert into staff values(4,'明明',10000);  
  140. insert into staff(id,name,sal) values(4,'星星',6000);  
  141. insert into staff(name,id,sal) values('月月',5,6000);  
  142. ----------------------------------------------------------------------------------------------------------------  
  143. 将所有员工薪水修改为10000元  
  144. update staff set sal=10000;  
  145. update staff set sal=10000;  
  146.   
  147. 以上就是SQL语句的威力,如果发送命令,MySQL数据库服务器自行会解析其命令,并做相对应的过程处理,这个  
  148. 过程处理对程序员来讲,是封闭的,看不见。  
  149. SQL的全称【结构化查询语句】,第四代计算机语言  
  150. 第一代:机器语言,即01010010100100101  
  151. 第二代:汇编语言,即用一个代号去表示10101010这些数字  
  152. 第三代:高级语言,即c/c++/vb/java/c#/...  
  153. 第四代:命令语言,即SQL  
  154. 第五代:智能语言,。。。  
  155.           
  156. 将姓名为'哈哈'的员工薪水修改为11000元  
  157. update staff set sal=11000 where name = '哈哈';  
  158. update staff set sal=11000 where name = '哈哈';  
  159.   
  160. 将月月的薪水在原有基础上增加1000元  
  161. update staff set sal = sal + 1000 where name = '月月';  
  162. update staff set sal = sal+1000 where name = '月月';  
  163.   
  164. 删除表中3号的记录  
  165. delete from staff where id = 3;  
  166. delete from staff where id = 3;  
  167.   
  168. 删除表中所有记录  
  169. delete from staff;  
  170. delete它是一行一行删除,速慢较【慢 】          drop table staff;整个表都被删除了  
  171.   
  172. 使用truncate删除表中记录  
  173. truncate它是整表删除后再重构,速慢较【快】。但是它的表结构还在,知识表内容没了  
  174. ---------------------------------------------------------------------------------------------------------------  
  175. students表  
  176. use mydb1;  
  177. drop table if exists students;  
  178. create table if not exists students(  
  179.     id int(5),  
  180.     name varchar(20),  
  181.     chinese int(5),  
  182.     english int(5),  
  183.     math int(5)  
  184. );  
  185.   
  186. insert into students(id,name,chinese,english,math) values(1,'张小明',89,78,90);  
  187. insert into students(id,name,chinese,english,math) values(2,'李进',67,98,56);  
  188. insert into students(id,name,chinese,english,math) values(3,'王五',87,78,77);  
  189. insert into students(id,name,chinese,english,math) values(4,'李一',88,98,90);  
  190. insert into students(id,name,chinese,english,math) values(5,'李来财',82,84,67);  
  191. insert into students(id,name,chinese,english,math) values(6,'张进宝',55,85,45);  
  192. insert into students(id,name,chinese,english,math) values(7,'黄蓉',85,75,80);  
  193. insert into students(id,name,chinese,english,math) values(8,'张一李',75,65,30);  
  194. insert into students(id,name,chinese,english,math) values(9,'何李',75,65,30);  
  195. insert into students(id,name,chinese,english,math) values(10,'单',75,65,30);  
  196. insert into students(id,name,chinese,english,math) values(11,'李',75,65,NULL);  
  197. insert into students(id,name,chinese,english,math) values(12,'jack',75,65,40);  
  198. insert into students(id,name,chinese,english,math) values(13,'marry',75,65,60);  
  199.   
  200. 查询表中所有学生的信息,*表示所有字段,顺序与表结构相同------------------------------用students.sql表  
  201. select id,name,chinese,math,english from students;  
  202. select name,id,chinese,math,english from students;  
  203. select name,chinese,math,english,id from students;  
  204. select * from students;  
  205. *号虽然写好起来方便,但充满不确定因素,要慎用  
  206.   
  207. 查询表中所有学生的姓名和对应的英语成绩  
  208. select name,english from students;  
  209. select name,english from students;  
  210.   
  211. 过滤表中重复语文成绩distinct(区别的)放在单列名前面,可以对该列名重复你元素进行筛选,仅仅保留一个  
  212. select distinct chinese from students;  
  213. select distinct chinese from students;  
  214.   
  215. 在所有学生分数上加10分特长分  
  216. select name,chinese+10,math+10,english+10 from students;  
  217. select name,chinese+10,math+10,english+10 from students;  
  218. NULL与任何数值进行运算,结果为NULL  
  219.   
  220. 统计每个学生的总分  
  221. select name,chinese+math+english from students;  
  222. select name,chinese+math+english from students;  
  223.   
  224. **使用(别名)表示学生分数,注意使用别名加上双引号  
  225. select name "姓名",chinese+math+english "总分" from students;  
  226. select name "姓名",chinese+math+english "总分" from students;  
  227.   
  228. 查询姓名为'张小明'的学生成绩    注意字符串用单引号'' 条件用where  
  229. select id,name,chinese,math,english from students where name = '张小明';  
  230. select * from students where name = '张小明';  
  231.   
  232. 查询英语成绩大于90分的同学  
  233. select id,name,chinese,math,english   
  234. from students   
  235. where english > 90;  
  236. select * from students where english>90;  
  237.   
  238. 查询总分大于200分的所有同学  
  239. select id,name,chinese,math,english   
  240. from students   
  241. where chinese+math+english > 200;  
  242.   
  243. 查询数学分数为89或者90或者91的同学  
  244. 方式一:  
  245. select name,math  
  246. from students  
  247. where (math=89) or (math=91) or (math=90);  
  248.   
  249. 方式二:(推荐)  
  250. select name,math  
  251. from students  
  252. where math in (91,89,90,100,10000);//即使加上一些不存在的值也没问题  
  253.   
  254. 查询英语分数在 80-90之间的同学,包含80和90  
  255. 方式一:  
  256. select name,english  
  257. from students  
  258. where (english>=80) and (english<=90);  
  259.   
  260. 方式二:  
  261. select name,english  
  262. from students  
  263. where english between 80 and 90;  
  264. select name,english from students where english between 80 and 90;  
  265.   
  266. 查询所有姓'李'的学生成绩,%表示0或多个字符(模糊查询)  
  267. select name,english,math,english  
  268. from students  
  269. where name like '李%';  
  270. select * from students where name like '李%';  
  271. =表示精确比较  
  272. 模糊比较,like关键字  
  273.   
  274. 查询所有名'李'的学生成绩  
  275. select name,english,math,english  
  276. from students  
  277. where name like '%李';  
  278.   
  279. 查询所有姓名中包含’李’的学生成绩  
  280. select name,english,math,english  
  281. from students  
  282. where name like '%李%';  
  283.   
  284. 以下三条SQL都表示查询表中的[所有]记录  
  285. select name,english,math,english  
  286. from students  
  287. where name like '%';  
  288.   
  289. select name,english,math,english  
  290. from students  
  291. where name like '%%';  
  292.   
  293. select name,english,math,english  
  294. from students  
  295. where name like '%%%';  
  296.   
  297. 查询所有姓'李'的学生成绩,但姓名必须是三个字符,_表示1个字符  
  298. select *  
  299. from students  
  300. where name like '李__';  
  301. select * from students where name like '李__';  
  302.   
  303. 查询数学分>80且语文分>80的同学  
  304. select *  
  305. from students   
  306. where 1=1 and   
  307. (math>80) and   
  308. (chinese>80);  
  309.   
  310. select * from students where 1=1 and (math>80) and (chinese>80);  
  311. ---------------------------------------------  
  312. 对数学成绩排序(降序)后输出  
  313. select id,name,math  
  314. from students  
  315. order by math desc;  
  316. select id,name,math from students order by math desc;  
  317. desc表示降序,排序字段用数值型  
  318.   
  319. select id,name,math  
  320. from students  
  321. order by math asc;  
  322. asc表示升序,不写asc和desc默认是升序  
  323.   
  324. **对总分排序(降序)后输出  
  325. SELECT name "姓名",math+chinese+english "总分"  
  326. From students  
  327. order by (math+chinese+english) desc;  
  328. select name "姓名",math+chinese+english "总分" from students order by (math+chinese+english) desc;  
  329.   
  330. 扩展:通常<order by>后面可以跟如下内容:  
  331. 1)字段  
  332.    order by math desc  
  333. 2)表达式  
  334.    order by (math+chinese+english) desc  
  335. 3)别名(这个别名可以不加引号)  
  336.    select name "姓名",math+chinese+english "总分"  
  337.    from students  
  338.    order by 总分 desc;  
  339. 4)列号,即列在select中出现的位置,从1开始排序  
  340.    select name "姓名",math+chinese+english "总分"  
  341.    from students  
  342.    order by 2 desc;  
  343.   
  344. 对姓'李'的学生总分排序(降序)输出  
  345. select name "姓名",(chinese+math+english) "总分"  
  346. from students  
  347. where name like '李%'  
  348. order by 2 desc;  
  349. --  
  350. select name "姓名",(chinese+math+english) "总分" from students where name like "李%" order by 2 desc;  
  351.   
  352. 查询数学分为NULL的学生  
  353. select name,math  
  354. from students  
  355. where math is null;  
  356. select name,math from students where math is null;  
  357.   
  358. select name,math  
  359. from students  
  360. where math is not null;  
  361.   
  362. 还有not in,not between and,is not  
  363. -------------------------------------------------------------------------------------------------------  
  364. 统计函数:统计函数会把null值得排除掉  
  365. 统计一个班级共有多少学生  
  366. select count(*) "总人数"  
  367. from students;  
  368. select count(*) "总人数" from students;  
  369.   
  370. 不提倡用*号,而用非NULL唯一列,即id(主健)  
  371. select count(id)  
  372. from students;  
  373. select count(id) from students;  
  374.   
  375. select count(math)  
  376. from students;  
  377. 建议不要统计含有NULL的列值  
  378.   
  379. 统计数学成绩大于80的学生有多少个  
  380. select count(id)  
  381. from students  
  382. where math>80;  
  383. select count(id) from students where math>80;  
  384.   
  385. 统计总分大于250的人数有多少  
  386. select count(id)  
  387. from students  
  388. where (math+chinese+english)>250;  
  389. -----  
  390. 统计一个班级数学总成绩  
  391. select sum(math) "数学总成绩"  
  392. from students;  
  393. select sum(math) "数学总成绩" from students;  
  394.   
  395. 统计一个班级语文、英语、数学各科的总成绩  
  396. select sum(math) "数学总成绩",sum(english) "英语总成绩",sum(chinese) "语文总成绩"  
  397. from students;  
  398.   
  399. 统计一个班级语文、英语、数学的成绩总和  
  400. select sum(chinese+math+english) "总成绩"  
  401. from students;  
  402. ------  
  403. 统计一个班级语文成绩平均分  
  404. select avg(chinese) "语文平均分"  
  405. from students;  
  406. select avg(chinese) "语文平均分"  
  407. from students;  
  408.   
  409. 求一个班级数学平均分  
  410. select avg(math) "数学平均分"  
  411. from students;  
  412.   
  413. 求一个班级总分平均分  
  414. select avg(math+chinese+english) "班级总分平均分"  
  415. from students;  
  416.   
  417. 求班级最高分和最低数学分数  
  418. select max(math) "数学最高分",min(math) "数学最低分"  
  419. from students;  
  420.   
  421. 回顾:  
  422. count()   统计总数  
  423. sum()     求和  
  424. avg()     平均  
  425. max()     最值  
  426. min()  
  427.   
  428. -------------------------------------------------------------------------------------------------------  
  429.   
  430. orders表:  
  431. drop table if exists orders;  
  432. create table if not exists orders(  
  433.     o_id int,  
  434.     o_product varchar(20),  
  435.     o_price int  
  436. );  
  437.   
  438. insert into orders values(1,'电视',900);  
  439. insert into orders values(2,'洗衣机',100);  
  440. insert into orders values(3,'洗衣粉',90);  
  441. insert into orders values(4,'桔子',10);  
  442. insert into orders values(5,'洗衣粉',80);  
  443.   
  444.   
  445.   
  446. 对订单表中商品归类后,显示每一类商品的总价------------------------------用orders.sql表  
  447. select o_product "商品",sum(o_price) "总价"  
  448. from orders   
  449. group by o_product;  
  450.   
  451. 查询购买了几类商品,并且每类总价大于100的商品,即分类后,还要过滤  
  452. select o_product "商品",sum(o_price) "总价"  
  453. from orders   
  454. group by o_product  
  455. having sum(o_price) > 100;  
  456.    
  457. 小结:  
  458. where:  
  459. 行过滤器  
  460. 针对原始记录,即没有分组的记录  
  461. 可以不出现  
  462. 通常出现在where后面  
  463. 先执行where  
  464.   
  465. having:  
  466. 组过滤器  
  467. 针对分组后的记录  
  468. 可以不出现  
  469. 通过出现在group by 后面  
  470. 后执行having  
  471.   
  472. 查询购买了几类商品,并且每类总价大于100的商品,同时按照总价的降序排列  
  473. select o_product "商品",sum(o_price) "总价"  
  474. from orders   
  475. group by o_product  
  476. having sum(o_price) > 100  
  477. order by 2 desc;  
  478.   
  479. 小结:  
  480. select子句  
  481. from 子句  
  482. where子句  
  483. group by子句  
  484. having子句  
  485. order by子句  
  486.   
  487. 在MySQL数据库服务器中,上面的所有子句,哪个是必须写的呢?  
  488. 答:select  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值