SQL基础语句
一、DDL 操作库结构
1、创建数据库
1.创建数据库
create database 数据库名;
2.判断是否存在并创建数据库
create database if not exists 数据库名;
3.创建数据库并指定字符集
create database 数据库名 character set 字符集;
2、查看数据库
1.查看所有的数据库
show databases;
2.查看某个数据库的定义信息
show create database 数据库名;
3、修改数据库
1.修改数据库默认的字符集
alter database 数据库名 default character set 字符集;
4、删除数据库
1.删除数据库
drop database 数据库名;
5、使用数据库
1.查看正在使用的数据库
select database();
2.使用/切换数据库
use 数据库名;
二、DDL 操作表结构
1、创建数据表
1.创建表的格式
create table 表名 (
列名 1 列类型 1,
列名 2 列类型 2
);
2.列类型特殊情况
- double:小数类型
列名 double(数字位数,小数位数)。例:score double(5,2) - dete:日期,年月日。例:2021-02-05
- detetime:日期,年月日时分秒。例:2021-02-05 17:29:24
- timestamp:时间戳,年月日时分秒,不赋值或赋值为null时,默认为当前系统时间
- varchar:字符串,列名 varchar(位数),例:name varchar(20)
2、查询数据表
1.查看某个数据库中的所有表
show tables;
2.查看表结构
desc 表名;
3.查看创建表的 SQL 语句
show create table 表名;
3、修改数据表
1.修改表名
rename table 表名 to 新表名;
2.修改字符集
alter table 表名 character set 字符集;
3.添加表列
alter table 表名 add 列名 数据类型;
4.修改列类型
alter table 表名 modify 列名 新的类型;
5.修改列名
alter table 表名 change 旧列名 新列名 类型;
6.删除列
alter table 表名 drop 列名;
4、删除数据表
1.直接删除表
drop table 表名;
2.判断表是否存在,如果存在则删除表
drop table if exists 表名;
三、DML 操作表中的数据
1、添加数据
1.插入全部数据
insert into 表名 values(值 1, 值 2, 值 3…),(值 1, 值 2, 值 3…),(值 1, 值 2, 值 3…);
2.插入部分数据
insert into 表名 (列名1, 列名 2, …) values(值 1, 值 2, …),(值 1, 值 2, …),(值 1, 值 2, …);
注:没有添加数据的字段会使用 NULL
除了数字类型,其他类型都需要单引号或者双引号引起来
2、删除数据
1.不带条件删除数据
delete from 表名;
注:不建议使用,列多的情况耗时较长
2.带条件删除数据
delete from 表名 where 条件;
例:delete from student where id=1;
3.使用 truncate 删除表中所有记录
truncate table 表名;
注:truncate 相当于删除表的结构,再创建一张表
3、修改数据
1.修改所有的行
update 表名 set 列名1 = 值1,列名2 = 值2,…,列名n = 值n;
例:不带条件修改数据,将所有的性别改成女
update student set sex = ‘女’;
2.带条件修改数据
update 表名 set 列名1 = 值1,列名2 = 值2,…,列名n = 值n where 条件列名1 = 值1;
例:带条件修改数据,将 id 号为 2 的学生性别改成男
update student set sex=‘男’ where id=2;
一次修改多个列,把 id 为 3 的学生,年龄改成 26 岁,address 改成北京
update student set age=26, address=‘北京’ where id=3;
4、蠕虫复制
1.将表名2中的所有的列复制到表名1中
insert into 表名1 select*from 表名2;
例:将 student 表中的数据添加到 student2 表中
insert into student2 select * from student;
2.只复制部分列
insert into 表名1(列1, 列2) select列1, 列2 from 表名2;
例:如果只想复制 student 表中 name,age 字段数据到 student2 表中,两张表都写出相应的列名
insert into student2 (name,age) select name,age from student;
四、DQL 查询表中的数据
1、简单查询
1.使用*表示所有列
select * from 表名;
2.查询指定列的数据,多个列之间以逗号分隔
select 列名1, 列名2, 列名3, … from 表名;
3.对列指定别名
select 列名1 as 别名, 列名2 as 别名… from 表名;
4.对列和表同时指定别名
select 列名1 as 别名, 列名2 as 别名… from 表名 as 表别名;
5.查询指定列并且结果不出现重复数据
select distinct 列名1,列名2 from 表名;
6.某列数据和固定值计算
select 列名1 + 固定值 from 表名;
7.某列数据和其他列计算
select 列名1 + 列名2 from 表名;
注: 参与运算的必须是数值类型
如果null参与计算,结果都为null
可用ifnull(表达式1,表达式2)把null替换为其他值
表达式1为需要判断的列名
表达式2为替换的值
例:查询 math + english 的和
select *,(math+english) as 总成绩 from student;
as 可以省略
select *,(math+english) 总成绩 from student;
2、条件查询
1.select 列名 from 表名 where 条件;
2.运算符:> 、< 、<= 、>= 、= 、!=、<> ,其中<>在 SQL 中表示不等于
and(&&):与,SQL中建议使用前者,后者并不通用。
or (||):或
not(!):非
3.in 关键字
select 列名 from 表名 where 条件列 in (条件数据1,条件数据2…);
注:in里面的每个数据都会作为一次条件,只要满足条件的就会显示
例:查询 id 是 1 或 3 或 5 的学生
select * from student3 where id in(1,3,5);
查询 id 不是 1 或 3 或 5 的学生
select * from student3 where id not in(1,3,5);
4.范围查询
between 值1 AND 值2
表示从值1到值2范围,包头又包尾
例:查询 english 成绩大于等于75,且小于等于90的学生
select * from student3 where english between 75 and 90;
5.like 模糊查询
select * from 表名 where 列名 like ‘通配符字符串’;
6.MySQL通配符
% 匹配任意多个字符串
_ 匹配一个字符
例:查询姓名中包含’德’字的学生
select * from student3 where name like ‘%德%’;
查询姓马,且姓名有两个字的学生
select * from student3 where name like ‘马_’;