mysql——前面内容——前期整理笔记00
create table student( sid varchar(50),
sname varchar(50),
sage varchar(50),
ssex varchar(50)
); insert into student( sid,sname,sage,ssex ) values('','zhaolei','1990-01-01','nan'); insert into student values('','qiandian','1990-12-21','nan');
insert into student values('','sunfeng','1990-05-20','nan');
insert into student values('','liyun','1990-08-06','nan'); insert into student values('','zhoumei','1991-12-01','nv'),
('','wulan','1992-03-01','nv'),
('','zhenzu','1989-07-01','nv'),
('','wangju','1990-01-20','nv'); select * from student; select sid,sname,sage,ssex from student; select sid,sname,ssex from student; ============================================================================================= 1、修改表名 语法格式:alter table 旧表名 rename [to] 新表名; 注释:修改后example1表就不存在了,只存在名为user的新表,但是其内容是一致的,只是换了个名称. alter table student rename to sww; select * from student; select * from sww; alter table sww rename to student; =====================================================================================================
2、修改字段名 语法格式:alter table 表名 change 旧属性名 新属性名 新数据类型; 注释:新数据类型指修改后的数据类型,如不需要修改,则将新数据类型设置成与原来一样 alter table student change sname sww varchar(50); select * from student; alter table student change sww sname varchar(50); ============================================================================================== 3、修改字段的数据类型 语法格式:alter table 表名 modify 属性名 数据类型; 注释:表名指所要修改数据类型的字段的表的名称; 属性名指:所要修改数据类型字段的名称; 数据类型指:修改后的新的数据类型 =========================================================================================================
4、修改字段的排列位置 语法格式:alter table 表名 modify 属性名1 数据类型 first|after 属性名2; alter table student modify sage varchar(50) first; select * from student; alter table student modify sage varchar(50) after sname; ====================================================================================== 5、增加字段 语法格式:alter table 表名 add 属性名1 数据类型 [完整性约束条件] [first | after 属性名2]; 完整性约束条件:是可选参数,用来设置新增字段的完整性约束条件 first:是可选参数,其作用是将新增字段设置为表的第一个字的 after:是可选参数,其作用是将新增字段添加到“属性名2”所指的字段后 如果执行的SQL语句中没有“first”或者“after 属性名2”参数指定新增字段的位置,则新增字段默认为表的最后一个字段 alter table student add saddress varchar(50) after ssex; select * from student; ============================================================================================================== 6、删除字段 删除字段是删除表中已经定义好的表中的某个字段,删除后其字段所属的数据都会被删除 语法格式:alter table 表名 drop 属性名; alter table student drop saddress; select * from student; ========================================================================================================== select * from student; select * from student where sid in('','','',''); select * from student where sid not in('','','',''); select * from student where sid > 4; select * from student where sid != 4; select * from student where sid between 4 and 6; select * from student where sid not between 4 and 6; select * from student where sname like '%n%'; select * from student where sname not like '%n%'; ===================================================================================================== select * from student; select * from student order by sid desc; select count(*) from student; select sum(sid) from student; select avg(sid) from student; select max(sid) from student; select min(sid) from student; select count(*) from student group by ssex; select sid as a, sname as b,sage as c, ssex as d from student; select * from student limit 3; select * from student limit 1,2; ====================================================================================
增加一个字段,插入数据,并更改数据 alter table student add saddress varchar(50) after ssex; select * from student; update student set saddress = 'jiangsu' where sid = '';
update student set saddress = 'jiangsu' where sid = ''; update student set saddress = 'shanghai' where sid = '';
update student set saddress = 'shanghai' where sid = '';
update student set saddress = 'shanghai' where sid = ''; update student set saddress = 'beijing' where sid = '';
update student set saddress = 'beijing' where sid = ''; update student set saddress = 'anhui' where sid = ''; insert into student ( sid,sname,sage,ssex ) values ('','shenweiwei','1989-10-18','nan'); update student set saddress = 'jiangsu' where sid = ''; 一、将查询结果插入到表中 insert语句可以将一个表中查询到的数据插入到另外一个表中 语法格式: insert into 表名1 (属性列表1) select 属性列表2 from 表名2 where 条件表达式; 表名1说明记录插入到哪个表中; 表名2表示记录是从哪个表中查询出来的; 属性列表1参数表示为哪些字段赋值; 属性列表2表示从表中查询出哪些字段的数据; 条件表达式参数设置了select语句的查询条件; 注意:使用这种方法时,必须保证属性列表1和属性列表2中的字段个数是一样的,而且每个对应字段的数据类型必须是一样的。 create table student2( sid varchar(50),
sname varchar(50),
sage varchar(50),
ssex varchar(50),
saddress varchar(50)
); select * from student2;
delete from student2;
insert into student2 ( sid,sname,sage,ssex,saddress ) values ('','fenglili2','1988-09-18','nv','jiangsu');
insert into student2 ( sid,sname,sage,ssex,saddress ) values ('','fenglili3','1988-08-18','nv','jiangsu');
insert into student2 ( sid,sname,sage,ssex,saddress ) values ('','fenglili4','1988-07-18','nv','shanghai'); insert into student ( sid,sname,sage,ssex,saddress ) values ('','fenglili2','1988-09-18','nv','jiangsu');
insert into student ( sid,sname,sage,ssex,saddress ) values ('','fenglili3','1988-08-18','nv','jiangsu');
insert into student ( sid,sname,sage,ssex,saddress ) values ('','fenglili4','1988-07-18','nv','shanghai');
delete from student where sid in('','','');
select * from student; insert into student(sid,sname,sage,ssex) select sid,sname,sage,ssex from student2 where sid = ''; select * from student; insert into student(sid,sname,sage,ssex,saddress) select sid,sname,sage,ssex,saddress from student2 where sid in ('',''); select * from student; update student set saddress = 'jiangsu' where saddress is null; select * from student; ================================================================================================================================
一、基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ] ] [ order by 属性名2 [ asc | desc ] ] 属性列表参数表示需要查询的字段名; 表名和视图列表参数表示从此处指定的表或者视图中查询数据,表和视图可以有多个; 条件表达式1参数指定查询条件; 属性名1参数指按照该字段的数据进行分组; 条件表达式2参数满足该表达式的数据才能输出; 属性名2参数指按照该字段中的数据进行排序;排序方式由asc和desc这两个参数指出; asc参数表示升序,这是默认参数,desc表示降序;(升序表示从小到大) 对记录没有指定是asc或者desc,默认情况下是asc; 如果有where子句,就按照“条件表达式1”指定的条件进行查询;如果没有where子句,就查询所有记录; 如果有group by子句,就按照“属性名1”指定的字段进行分组,如果group by后面带having关键字,那么只有 满足“条件表达式2”中知道的条件才能输出。group by子句通常和count()、sum()等聚合函数一起使用; 如果有order by子句,就按照“属性名2”指定的字段进行排序,排序方式由asc和desc两个参数指出;默认情况下是asc; 查询结果不重复:distinct 关键字 select * from student; select distinct ssex from student; select distinct saddress from student; 分组查询: select * from student group by ssex; select * from student group by saddress; group by 关键字与group_concat()函数一起使用,每个分组中指定字段值都显示出来: select ssex,GROUP_CONCAT(ssex) from student group by ssex;
select ssex,GROUP_CONCAT(sname) from student group by ssex;
select saddress,GROUP_CONCAT(sname) from student group by saddress; group by 关键字与集合函数一起使用: select ssex,count(ssex) from student group by ssex; select saddress,count(saddress) from student group by saddress; group by 关键字与"having 条件表达式"一起使用,可以限制输出结果,只有满足条件表达式的结果才会显示: select ssex,count(ssex) from student group by ssex;
select ssex,count(ssex) from student group by ssex having count(ssex) >6; select saddress,count(saddress) from student group by saddress;
select saddress,count(saddress) from student group by saddress having count(saddress) >3; 注意:“having 表达式” 与 "where 表达式"都是用来限制显示的,但是两者起作用的地方不一样; "where 表达式"用于表或者视图,是表和视图的查询条件; “having 表达式”作用于分组后的记录,用于选择满足条件的分组。 group by 关键字与 with rollup 一起使用,会在所有记录的最后加上一条记录,这条记录是上面所有记录的总和。 select ssex,count(ssex) from student group by ssex;
select ssex,count(ssex) from student group by ssex with rollup; select saddress,count(saddress) from student group by saddress;
select saddress,count(saddress) from student group by saddress with rollup; select * from student; select * from student limit 4;
select * from student limit 1,5; ======================================================================================================= create table score ( xh int(50),
km varchar(50),
cj int(50)
); select * from score; insert into score values(1,'shuxue',80);
insert into score values(1,'yuwen',70);
insert into score values(1,'yingyu',40); insert into score values(2,'shuxue',40);
insert into score values(2,'yuwen',60);
insert into score values(2,'yingyu',50); insert into score values(3,'shuxue',60);
insert into score values(3,'yuwen',20);
insert into score values(3,'yingyu',90); insert into score values(4,'shuxue',50);
insert into score values(4,'yuwen',60);
insert into score values(4,'yingyu',70); select xh,sum(cj) from score where xh = 1; 查询此同学的总成绩;
select xh,sum(cj) from score where xh = 4; select xh,sum(cj) from score group by xh; 查询每一个同学的各科总和成绩; select km,max(cj) from score group by km; 查询各个科目的最高成绩; select km,avg(cj) from score group by km; 查询每一科目的平均成绩; select km,max(cj) from score group by km; 查询每一科目的最高成绩; select km,min(cj) from score group by km; 查询每一科目的最低成绩;
======================================================================================================================= create table employee ( num int(50),
d_id int(50),
name varchar(50),
age int(50),
sex varchar(50),
homeadd varchar(50)
); insert into employee values(1,1001,'zhangsan',26,'nan','beijing');
insert into employee values(2,1001,'lisi',24,'nv','hunan');
insert into employee values(3,1002,'wangwu',25,'nan','jiangsu');
insert into employee values(4,1004,'aric',15,'nan','yingguo'); select * from employee; create table department ( d_id int(50),
d_name varchar(50),
functione varchar(50),
address varchar(50)
); insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao');
insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng');
insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating'); select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id; 内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表; 当该字段的值相等时,就查询出该记录。 ====================================================================================================================== 外连接查询:可以查询两个或者两个以上的表,外连接查询也需要通过指定字段来进行连。 当该字段取值相等时,可以查询出该记。 而且该字段取值不相等的记录也可以查询出来。 外连接包括:左连接、右连接 语法: select 属性列表 from 表名1 left | right join 表名2 on 表名1.属性名1 = 表名2.属性名2; 属性列表表示要查询的字段的名称,这些字段可以来自不同的表; Left表示左连接查询; rigth表示右连接查询; on后面接的是连接的条件; 1、左连接查询 进行左连接查询时,可以查询出表名1所指的表中的所有记录。而表名2所指的表中,只能查询出匹配的记录 select * from employee;
select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id = department.d_id; 2、右连接查询 进行右连接查询时,可以查询出表名2所指的表中的所有记录。而表名1所指的表中,只能查询出匹配的记录 select * from employee;
select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee right join department on employee.d_id = department.d_id; 复合条件查询 在连接查询时,通过增加其他的限制条件,可以使查询结果更加准确 select * from employee;
select * from department; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id and age > 24; select num,name,employee.d_id,age,sex,d_name,functione from employee,department where employee.d_id = department.d_id order by age asc; select 语句先按照内连接的方式从employee和department表中查询出数据。然后查询结果按照age字段从小到大的顺序进行排序。 ================================================================================================================================================= 子查询 子查询是将一个查询语句嵌套在另外一个查询语句中,内层查询语句的查询结果,可以作为外来层查询语句提供查询条件。 1、带in关键词的子查询 select * from employee;
select * from department; select * from employee where d_id in (select d_id from department ); select * from employee where d_id not in (select d_id from department ); 2、带exists关键字的子查询 exists关键字表示存在,使用exists关键字时,内层查询语句不用返回查询的记录。而是返回一个真假值。 如果内层查询语句查询到满足条件的记录,就返回一个真值(true);否则,返回一个假值(false); 当返回值为true时,外层查询语句将进行查询;而返回false时,外层查询语句不进行查询或者查询不出任何记录。 select * from employee;
select * from department; select * from employee where exists (select d_name from department where d_id = 1003); select * from employee where exists (select d_name from department where d_id = 1004); exists关键字可以与其他查询条件一起使用。条件表达式与exists关键字之间用and或者or来连接。 select * from employee where age > 24 and exists (select d_name from department where d_id = 1003); select * from employee where age > 24 and exists (select d_name from department where d_id = 1004); not exists与exists相反。 select * from employee where age > 24 and not exists (select d_name from department where d_id = 1003); select * from employee where age > 24 and not exists (select d_name from department where d_id = 1004); 3、带比较运算符的子查询 create table schoarship ( levela int(50),
score int(50)
); insert into schoarship(levela,score) values(1,90);
insert into schoarship values(2,80);
insert into schoarship values(3,70); select * from schoarship; create table computer_stu ( id int(50),
name varchar(50),
score int(50)
); insert into computer_stu(id,name,score) values (1001,'lily',85); insert into computer_stu(id,name,score) values (1002,'tom',91),
(1003,'jim',87),
(1004,'aric',77),
(1005,'lucy',65),
(1006,'andy',99),
(1007,'ada',85),
(1008,'jeck',70); select * from computer_stu; select id,name,score from computer_stu where score >= (select score from schoarship where levela = 1); /* 查询获得一等奖学金的学生有哪些,第一个表为奖学金等级和最低分数*/ select d_id,d_name from department where d_id != (select d_id from employee where age = 24); /*只有生产部和销售部没有年龄等于24岁的员工*/ 4、带any关键字的子查询 any关键字表示满足其中任何一个条件。使用any关键字时,只要满足内查询语句返回的结果中的任何一个,就可以通过该条件来执行外层查询语句 select * from computer_stu where score >= any ( select score from schoarship ); /*结果显示,有7个人获得奖学金,只有id为1005的人没有,因为分数为65,不高于奖学金指定最低分数的任何一个*/ 5、带all关键字的子查询 all关键字表示满足所有条件。使用all关键字时,只有满足内层查询语句返回的所有结果,才可以执行外层查询语句。 select * from computer_stu where score >= all ( select score from schoarship ); /*结果显示,只有两个人获得奖学金。因为这两个人的分数比所有奖学金要求的分数都高*/ ============================================================================================================================
合并查询结果 合并查询结果 是将多个select语句的查询结果合并到一起 union关键字,数据库会将所有的查询结果合并到一起,然后除掉相同的记录; union all关键字,只是简单的合并到一起 select d_id from department; select d_id from employee; select d_id from department union select d_id from employee; select d_id from department union all select d_id from employee; ===================================================================================================================== 为表和字段取别名 1、语法:表名 表的别名 select * from department d where d.d_id = 1001; 2、语法:属性名 [as] 别名 select d_id as department_id,d_name department_name from department; 数据库中,可以同时为表和字段取别名 select d_id as department_id,d_name department_name,d.functione,d.address from department d where d.d_id = 1001; =================================================================================================================================
mysql——前面内容——前期整理笔记00的更多相关文章
- mysql——触发器——前期整理笔记00
一.触发器 触发器是由事件来出发某个动作.这些事件包括insert语句.update语句和delete语句. 当数据库系统执行这些事件时,就会激活触发器执行相应得动作. 触发器是有insert.upd ...
- Mysql又一次整理笔记--woods备忘
==============================SQL备忘 CRUD 查询 多表 事件等=============================== ------------------ ...
- mysql 索引优化知识整理笔记
http://blog.csdn.net/zhxp_870516/article/details/8434539 http://www.jb51.net/article/49346.htm https ...
- mysql 5.7压缩包安装笔记
转载请注明出处http://www.cnblogs.com/havedream/p/5075263.html 重装系统之后准备安装mysql,看到官网上有mysql 5.7.10可以下载就点了,然后就 ...
- (转)Mysql数据库主从心得整理
Mysql数据库主从心得整理 原文:http://blog.sae.sina.com.cn/archives/4666 管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本 ...
- MySql命令,吐血整理的Mysql,实话,真的吐血
MySql命令,吐血整理的Mysql,实话,真的吐血 1.基本操作 2.数据库操作 3.表的操作 4.数据操作 5.字符集编码 6.数据类型(列类型) 7.列属性(列约束) 8.建表规范 9.SELE ...
- canvas学习之API整理笔记(二)
前面我整理过一篇文章canvas学习之API整理笔记(一),从这篇文章我们已经可以基本了解到常用绘图的API.简单的变换和动画.而本篇文章的主要内容包括高级动画.像素操作.性能优化等知识点,讲解每个知 ...
- MySQL隐式转化整理
MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
- xmpp整理笔记:发送图片信息和声音信息
图片和音频文件发送的基本思路就是: 先将图片转化成二进制文件,然后将二进制文件进行base64编码,编码后成字符串.在即将发送的message内添加一个子节点,节点的stringValue(节点的值) ...
随机推荐
- Apache工作流程
一个经典的Apache处理php页面的流程 需要连接mysql数据库并处理的流程 网站是一系列网页的组合 从用户角度看就是访问诸如 hhtp://www.baidu.com -----url 这是互联 ...
- matplotlib 模块
目录 matplotlib 模块 1.条形图 2.直方图 3.折线图 4.散点图+直线图 5.饼图 6. plot 函数参数 7.图像标注参数 matplotlib 模块 1.条形图 import m ...
- qt5--文件操作
文本文件的读写操作: #include "win.h" #include "ui_win.h" #include <QDebug> #include ...
- vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)
一.安装echarts: cnpm i echarts -D 二.在vue-cli的main.js文件中引用echarts: import charts from 'echarts' Vue.prot ...
- Confluence 6 多媒体文件和在页面中显示文件列表
多媒体文件 文件的预览同时也支持 MP3 音频和 MP4 视频文件.Confluence 使用 HTML 5 来播放附加的音频和视频文件.这个意味着这些文件类型的文件格式,用户可以在支持的浏览器中直接 ...
- Confluence 6 插入一个文件到你的页面
文件可以在页面中以缩略图或者链接的方式显示.我们有多种办法能够上传文件,请参考 Upload Files 页面. 你可以控制文件如何在你的页面中显示.文件在页面中显示的可用方法与你的文件类型有关. 插 ...
- Qtcreator 之中文目录
由于编码格式问题, qtcreator 所有关于目录的引用和存放均不出现中文,否则编译可以通过,运行时 crashed !!!
- H5自定义video功能与样式处理
H5的video非常简单,方便,有时我们可能需要自己来设置样式来自定义的video,自定义的话我们需要对功能进行一些处理,这里常用的功能几乎是都用到了,第一次练习代码很累赘,之后会慢慢改进. 常用的一 ...
- 如何在一个页面使用多个router-view显示不同的内容
todo https://www.jianshu.com/p/92f61bf9db81
- C++入门经典-例3.14-使用while循环计算从1到10的累加
1:代码如下: // 3.14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...