😘温馨提示:本文讲述的mysql语句属于基础内容,如需更深入学习,请另找高就😘
数据库
1.最基本的建库语句
-- 语法如下
create database <库名>;
create database class;
2.具有判断库是否存在时的建库语句
create database if not exists <库名>;
create database if not exists school;
3.删除库的基本语句
drop database <库名>;
drop database school;
4.具有判断库是否存在的删库语句
drop database if exists <库名>;
drop database if exists school;
5.进入与切换数据库
use <库名>;
use class;
数据表
一、增
1.创建数据表
create table <if not exists> <表名>(
<列名> <类型>,
<列名> <类型>,
……
)
<ENGINE='引擎名' DEFAULT CHARSET=编码格式>
create table if not exists students(
name varchar(15),
stunum int,
class varchar(50)
);
2.插入数据
insert into <表名>(列名1,列名2,……) values (值1,值2,……);
insert into students(name,stunum,class) values ('张三',01,'1班');
插入多个值时,可以不断往后堆叠
insert into students(name,stunum,class) values ('张三',01,'1班'),('李四',02,'1班');
二、删
1.删除数据表
drop table <表名>;
drop table students;
2.清空数据表
truncate table <表名>;
truncate table students;
3.删除表中的数据
delete * from <表名>;
delete * from students;
如果是指定列删除
-- 删除name为张三的列 delete * from students where name='张三';
三、改
1.修改指定的数据
update <表名> set <列1>=<值>, <列2>=<值> where <条件>;
update students set name='李四' where stunum=1;
四、查
1.查询所有列
select * from <表名>;
select * from students;
2.指定列名的查询
select <列名> from <表名>;
select name from students;
3.设置别名
select * from <列名> as <别名> from <表名>;
select * from name as n from students;
4.查询时去除重复项
select distinct <列名> from <表名>;
select distinct name from students;
5.排序查询
select <列名> from <表名> order by <列名> <desc | asc>; --desc是降序 asc是升序
select name,stunum from students order by name desc;
6.具有条件的查询
select <列名> from <表名> where <条件>;
select * from students where stunum>1;
7.模糊查询
select <列名> from <表名> where <查询列> like <表达式>;
select * from students where name like '张%';
8.分页查询
select <列名> from <表名> limit <查询的条数>;
select * from students limit 3;
9.从自定义条数开始查询
select <列名> from <表名> limit <查询的条数> offset <开始查询位置>;
select * from students limit 3 offset 4;
offset可以省略
本文参考: