#Createcreate database hzyc;create database ifnot exists hzyc98 character set gbk;#Retrieveshow databases;show create database hzyc98;#Updatealter database hzyc98 character set gbk;#Deletedrop database hzyc98;drop database ifexists hzyc98; #查看当前使用的数据库select database();show tables;usehzyc98
1.操作表list:
表名/表头为:zoomlist
1
2
3
4
5
6
7
8
9
#查show tables; -- show tables_in_hzyc98desc zoomlist;#增create table zoomlist (
Name varchar(30),
Age int,
ID int,
Height double(5,1))#删drop table ifexists zoomlist;#改alter table zoomlist rename to newzoomlist;alter table zoomlist character set gbk;alter table zoomlist add Name varchar(20);#加列alter table zoomlist change Age newAge int;alter table zoomlist modify Age char(8);alter table zoomlist drop Name;/*设置类型:*/
- int、double(5,1)、varchar(20)
- date#yyyy-MM-dd
- datetime #yyyy-MM-dd HH:mm:ss
- timestamp#时间戳 yyyy-MM-dd HH:mm:ss
2.对表内数据进行操作:
1
#除了数字,其他都需要引号来赋值insert into zoomlist (Name, Age, ID, Height) value('美洲豹',5,'20201207',3.2);insert into zoomlist ('美洲豹',5,'20201207',3.2);#删除deletefrom zoomlist where [条件];deletefrom zoomlist;TRUNCATE TABLE zoomlist;#修改update zoomlist set Name = '大笨象'Age = 12 where address = '深圳';update zoomlist set address = '深圳';
a.查询
1
#查询#尽量不要用 * 先desc一下表里面有啥,然后在决定展示什么东西。SELECT * FROM zoomlist; SELECT Name,Age FROM zoomlist; --只显示某个列,方便查看!SELECT DISTINCT Name FROM zoomlist; --去除结果中[完全重复]的SELECT Name,score1,score2,scroe1+scroe2 FROM zoomlist;--as:自定义名字展示,也可以不写asSELECT Name,scroe1+IFNULL(scroe2,0) 总分 FROM zoomlist; --ifnull遇到没有值的直接给赋值为0SELECT Name,score1,score2,scroe1+IFNULL(scroe2,0) AS 总分 --显示表头FROM zoomlist,peoplelist; --从zoomlist、peoplelist里面获取
b.where条件:
1
* > 、< 、<= 、>= 、= 、!=、<>--不等号* and、or、not --关键字比&&、||、!好用推荐* BETWEEN...AND --范围内都符合就行* IN( 集合) --特定值的范围* LIKE:模糊查询(1)_:单个任意字符;(2)%:多个任意字符* IS NULL例子:select Name, Age from Student where age between 12 and20;select Name, Age from Student where age in (12,14,16,18);select Name, Age from Student where name like '%牛%'; --查名字里面包含了牛的学生select Name, Age from Student where name is not null; -- 查询学生:名字空的不查
三、查询
1. 排序查询
1
select * from employee order by age;select * from employee order by age asc; --升序select * from employee order by age desc; --降序select * from employee order by age desc height desc; --第一个一样的时候,才会用第二个方法排序(age降序,身高降序)
2. 聚合函数(列的计算)
排除了null数据,并且有null的数据就不参与计算,不会报错!
count:统计个数
min、max、sum、avg:求值
1
select count(*) from student;select count(ifnull(age,20)) from student; select count(age) from student;--如果没有就不记录select count(id) from student; --我们一般选用主键来统计个数select max(age) from student;select min(age) from student;select sum(age) from student;select avg(age) from student;
3. 分组查询
group by 之后就是两个不同的组别了,他们不能再去查看一个独立的个体了。
分组之后查询的字段:分组字段、聚合函数。
where和having的区别?
where在分组前限定,having在分组之后限定;
where不符合条件的不参与分组,having不符合条件不会显示;
只有having可以后跟聚合函数判断。
1
select sex,count(name) from employee group by sex having count(name)<6;select sex,count(name) from employee where name = '张四'group by sex ;
4. 排序查询
limit是一个MySQL的方言,用于分页
1
SELECT * FROM student LIMIT 0,5; -- 第1页,从0索引开始,读5个数据SELECT * FROM student LIMIT 7,10; -- 第2页,从7索引开始(第8个数据),读10个数据
四、约束
约束:
主键约束:primary key
非空约束:not null
唯一约束:unique
外键约束:foreign key
1.非空约束:not null
1
2
3
4
5
6
7
-- 建表时添加非空约束:
create table employee(
name char(30),
sex char(8) not null
)
alter table employee modify sex char(8) not null; --添加非空约束
alter table employee modify sex char(8); --破除非空约束
2.唯一约束
只可以有一个null值,不能再多了; 删除约束只可以用drop index来删除unique约束
1
2
3
4
5
6
7
8
-- 建表时添加唯一约束:
create table employee(
name char(30),
sex char(8),
score int unique --分数要唯一
)
--添加唯一约束alter table employee modify name char(8) unique;
--破除唯一约束-- alter table employee modify sex char(8); 不可用--破除name身上的unique约束用drop index除去索引alter table employee drop index name;
实例操作:
3.主键约束:primary key
一个表只有一个primary key,非空且唯一 做记录的唯一标识,相当于index
1
2
3
4
5
6
7
-- 建表时添加主键约束:
create table employee(
id int primary key, --给id加上主键约束
name char(30),
)
--添加唯一约束alter table employee modify id int primary key;
--破除唯一约束-- alter table employee modify id int; 不可用!--破除id身上的primary key约束只能用drop primary keyalter table employee drop primary key;
4.自动增长:auto_increment
只对数值有用,而且一般可以放给主键做自动增长
1
2
3
4
5
6
7
-- 建表时添加auto_increment:
create table employee(
id int auto_increment, --给id加上auto_increment
name char(30),
)
--添加auto_increment,自动从1开始alter table employee modify id int auto_increment;--设置初值alter table employee auto_increment = 100;
--破除auto_incrementalter table employee modify id int;