这篇文章给大家分享的是有关mysql基本操作有那些的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

文章目录

一、SQL是什么?分类:二、关于数据库CRUD操作1.操作表list:2.对表内数据进行操作:a.查询b.where条件:三、查询1. 排序查询2. 聚合函数(列的计算)3. 分组查询4. 排序查询四、约束1.非空约束:not null2.唯一约束实例操作:3.主键约束:primary key4.自动增长:auto_increment五、总结错误实例
一、SQL是什么?

structured Query Language:结构化查询语言

分类:

1) DDL(Data Definition Language)数据定义语言
用来定义数据库对象:数据库,表,列等。关键字:create, drop,alter 等
2) DML(Data Manipulation Language)数据操作语言
用来对数据库中表的数据进行增删改。关键字:insert, delete, update 等
3) DQL(Data Query Language)数据查询语言
用来查询数据库中表的记录(数据)。关键字:select, where 等
4) DCL(Data Control Language)数据控制语言(了解)
用来定义数据库的访问权限和安全级别,及创建用户。关键字:GRANT, REVOKE 等

二、关于数据库CRUD操作

#Createcreate database hzyc;create database if not exists hzyc98 character set gbk;#Retrieveshow databases;show create database hzyc98;#Updatealter database hzyc98 character set gbk;#Deletedrop database hzyc98;drop database if exists hzyc98; #查看当前使用的数据库select database();show tables;use hzyc981.操作表list:

表名/表头为:zoomlist

#查show tables; -- show tables_in_hzyc98desc zoomlist;#增create table zoomlist (Name varchar(30),Age int,ID int,Height double(5,1))#删drop table if exists 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:ss2.对表内数据进行操作:

#除了数字,其他都需要引号来赋值insert into zoomlist (Name, Age, ID, Height) value('美洲豹',5,'20201207',3.2);insert into zoomlist ('美洲豹',5,'20201207',3.2);#删除delete from zoomlist where [条件];delete from zoomlist;TRUNCATE TABLE zoomlist;#修改update zoomlist set Name = '大笨象' Age = 12 where address = '深圳';update zoomlist set address = '深圳';a.查询

#查询#尽量不要用 * 先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条件:

* > 、< 、<= 、>= 、= 、!=、<>--不等号* and、or、not --关键字比&&、||、!好用推荐* BETWEEN...AND --范围内都符合就行* IN( 集合) --特定值的范围* LIKE:模糊查询(1)_:单个任意字符;(2)%:多个任意字符* IS NULL例子:select Name, Age from Student where age between 12 and 20;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. 排序查询

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:求值

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可以后跟聚合函数判断。

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的方言,用于分页

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 key1.非空约束:not null

-- 建表时添加非空约束: 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约束

-- 建表时添加唯一约束: 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

-- 建表时添加主键约束: 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

只对数值有用,而且一般可以放给主键做自动增长

-- 建表时添加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;

感谢各位的阅读!关于mysql基本操作有那些就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!