一、SQL语句
1.概述
SQL:结构化查询语言。通过sql语句可以操作数据
SQL作用:实现数据库客户端和数据库服务器端之间的通信,sql就是通信的桥梁
2.sql语句分类
- 掌握DDL :数据库中的表的操作 (数据定义语言)
- create table (create:创建 table 表)
- alter table (alter : 修改)
- drop table (drop:删除/放弃)
- 掌握DML : 对于数据库表中的数据操作 (做的增删改操作)
- update (修改)
- delete (删除)
- insert into (添加)
- 掌握简单的DQL : 查询
- select (查询)
3.DDL
做表的操作
数据库/表
一台mysql服务器可以有很多的数据库,一个数据库中有可以有很多的表。表类似于excel表格
通过代码实现创建表和创建数据库
-- 创建数据库
-- 语法: create database [if not exists] 数据库名;
create database if not exists kunkun;
-- 删除数据库
-- 语法:drop database 数据库名;
drop database kunkun;
4.DML
数据类型
类型 | 大小 | 范围 | 用途 |
---|---|---|---|
tinyint | 1字节 | -128 ~ 127 | 小整数值 |
smallint | 2字节 | -32768 ~ 32767 | |
mediumint | 3字节 | -8388608 ~ 8388607 | |
int | 4字节 | -2147483648 ~ 2147473647 | 大整数值 |
bigint | 8字节 | ||
float | 4字节 | 存放单精度小数 | |
double | 8字节 | 存放双精度小数 | |
decimal | 16字节 | 精确位数最高的 |
字符串类型
类型 | 用途 |
---|---|
char | 定长字符串 |
varchar | 变长字符串 |
blob | 二进制形式存储的文本数字 |
text | 长文本数据 |
日期类型
类型 | 格式 |
---|---|
Year | YYYY |
Time | HH:mm:ss |
5.创建表
-- 创建数据库
-- 语法: create database [if not exists] 数据库名;
create database if not exists kunkun;
-- 创建表
-- 语法:create table 表名(列明1 列的数据类型,列明1 列的数据类型,....);
-- 使用数据库kunkun
use kunkun;
-- 创建表
create table student(
id int,
name varchar(20),
age int,
birthday date
)
注意:
- 在同一个数据库中,不能存在相同表名的表
- 每一个字段末尾都用逗号分隔开(英文输入法的逗号),最后一个不要加逗号
- 每一个字段数据命名尽量做到见名知意(可以用英文也可以用拼音)
6.约束
6.1 主键约束
主键:唯一(不能重复),且非空
每张表都会有一个主键,且每张表只有一个主键
-- 创建表并添加主键约束语法:
/*
create table [if not exists] 表名(
列名1 数据类型 primary key,
列名2 数据类型,
.....
列名n 数据类型
)
*/
create table if not exists student(
id int primary key,
name varchar(20),
age int
)
6.2 复合主键
多个列共同组成一个主键
-- 创建表并添加主键约束语法:
/*
create table [if not exists] 表名(
列名1 数据类型,
列名2 数据类型,
.....
列名n 数据类型,
primary key(列1,列2,...)
)
*/
create table if not exists student(
id int primary key,
name varchar(20),
age int,
primary key(id,name)
)
6.2 唯一约束
unique: 唯一
唯一:不能重复,但是可以为空(NULL)
/*
-- 唯一约束语法:
create table [if not exists] 表名(
列名1 数据类型 unique,
列名2 数据类型,
.....
列名n 数据类型
)
*/
create table student(
id int primary key,
name varchar(20) unique,
age int
)
6.3 非空约束
空指的是Null
/*
-- 非空约束语法:
create table [if not exists] 表名(
列名1 数据类型 not null,
列名2 数据类型,
.....
列名n 数据类型
)
*/
create table student(
id int primary key auto_increment,
name varchar(20) not null,
age int,
address varchar(50) unique not null
)
7.设置属性【备注】
/*
-- 备注:
create table [if not exists] 表名(
列名1 数据类型 comment '备注信息',
列名2 数据类型 comment '备注信息',
.....
列名n 数据类型 comment '备注信息'
)
*/
create table student(
id int primary key auto_increment comment '学号',
name varchar(20) not null comment '姓名',
age int comment '年龄',
address varchar(50) unique not null comment '家庭住址'
)
二、数据增删改SQL
1.增加
增加数据
1.1 基本增加
-- 语法:
insert into 表名[(列1,列2,....)] values(值1,值2,....)
- 单词解释
- insert into : 插入
- values:值
- 表名后面的列名没有省略,要求values后面的值和前面的列一一对应
- 表名后面的列名省略,要求values后面的值和数据库表中设计的列要一一对应
- 除了数值类型(整数和小数),其它的数据类型的值,必须用单引号括起来 (数值类型的数据的值也可以使用单引号括起来)
/*
插入数据的语法:
insert into 表名[(列1,列2,....)] values(值1,值2,...)
1.表名后面的列名没有省略,要求values后面的值和前面的列一一对应
2.除了数值类型(整数和小数),其它的数据类型的值,必须用单引号括起来 (数值类型的数据的值也可以使用单引号括起来)
3.表名后面的列名省略,要求values后面的值和数据库表中设计的列要一一对应
*/
insert into student(id,name,birthday,age) values(null,'蔡徐坤','2000-11-11',18);
insert into student(id,name,birthday,age) values(2,'蔡鸡','2000-12-12','19');
insert into student(name,birthday,age) values('背带裤','2010-05-6',17);
insert into student(name) values('rap');
insert into student values(null,'2年半实习生',18,'2010-7-6');
1.2 批量增加
-- 批量增加语法:
insert into 表名[(列1,列2,....)] values(值1,值2,...),(值1,值2,...),(值1,值2,...);
-- 批量增加
insert into student(name,birthday,age) values('房祖名','2010-6-6',18),('柯震东','2010-5-5',19);
2.修改
-- 语法:
update 表名 set 列1=值1,列2=值2,.... [where 条件];
- 单词解释
- update: 修改
- set: 设置
- where: 哪里…,表示条件也就是要修改哪条数据
- where 可以省略不写,如果不写,表示修改所有行的某列数据 (企业开发中不可能不加)
- 修改的条件一般是主键
-- 修改表中所有数据的年龄为18岁
update student set age = 18;
-- 修改名字叫:背带裤,他的年龄改为28岁
update student set age = 28 where name = '背带裤';
-- 将id是5的这条数据,它的名字改为“2年半练习生”,年龄改为26岁
update student set name = '2年半的练习生',age = 26 where id = 5;
3.删除
-- 语法:
delete from 表名 [where 条件];
- 单词解释:
- delete : 删除
- from : 从…到…
- 如果不加条件,表示删除整张表中的所有数据,实际开发中一般会加上条件
- 删除数据后,自增的id不会重新开始,而是接着
三、数据查询SQL
1.基本的查询
-- 语法:
select 列1,列2,.... from 表名;
- 单词解释:
- select: 查询
/*
-- 语法:
select 列1,列2,.... from 表名;
*/
-- 可以查询指定的列
select id,name,birthday,age from student;
select id,name from student;
-- 查询表中所有的列 (实际开发中要求不能这么写的)
select * from student;
2.别名
外号/花名
-- 语法:
select 列名1 as '别名1',列名 别名2,.... from 表名;
- as可以省略不写
- 别名一般作为字符串,需要用单引号括起来,但是也可以不用单引号
/*
-- 语法:
select 列名1 as '别名1',列名 别名2,.... from 表名;
- as可以省略不写
- 别名一般作为字符串,需要用单引号括起来,但是也可以不用单引号
*/
-- 别名
select id as '学号',name '姓名',birthday 生日,age 年龄 from student;
3.比较运算符
比较运算符除了=外,可以用于比较数值型数据或日期的大小
符号 | 意思 |
---|---|
= | 等于 |
> | 大于 |
< | 小于 |
!= 或 <> | 不等于 |
>= | 大于或等于 |
<= | 小于或等于 |
-- 需求1:查询编号大于4的学生
select * from student where id > 4;
-- 需求2:查询编号不大于4的学生信息
select * from student where id <= 4;
-- 需求3:查询编号不等于的学生信息
select * from student where id != 4;
select * from student where id <> 4;
-- 需求4:查询id=4的学生信息
select * from student where id = 4;
4.算术运算符
+ - * /
-- 算术运算符
-- 需求1:将id是1的学生年龄加2岁
update student set age = age + 2 where id = 2;
-- 需求2:将id是5的学生年龄减少5岁
update student set age = age - 2 where id = 5;
-- 需求2:将id是5的学生年龄乘以2
update student set age = age * 2 where id = 5;
5.逻辑运算符
符号 | 意思 |
---|---|
and 或 && | 与,并且 |
or 或 || | 或,或者 |
not | 非,取反 |
-- 需求1:查询名字叫“张三”,并且年龄是30岁的学生信息
select * from student where name='张三' and age = 30;
select * from student where name='张三' && age = 30;
-- 需求2:查询年龄是32,或者名字叫李四的学生信息
select * from student where age = 32 or name = '李四';
select * from student where age = 32 || name = '李四';
-- 需求3:查询年龄不是null的学生信息
select * from student where age is not null;
6.like模糊查询
-- 语法:
select 列n from 表名 where 列 like 占位符
- 占位符
%
: 表示任意的字符串(包含空字符)_
: 表示单个字符
-- 查询所有姓柯的学生信息
select * from student where name like '柯%';
-- 查询名字中含有柯的学生信息
select * from student where name like '%柯%';
-- 查询姓柯,并且单名
select * from student where name like '柯_';
-- 查询姓柯,并且名字两个字
select * from student where name like '柯__';
7.in模糊查询
-- 语法:
select * from 表 where 列 in (值1,值2,....);
-- 需求:查询id是1或3或5的学生信息
select * from student where id = 1 or id = 3 or id =5;
select * from student where id in (1,3,5);
-- 需求:查询名字是蔡徐坤,房祖名和柯震东的学生信息
select * from student where name in('张三','李四','王五');
select * from student where name like '%柯%';
-- 查询姓柯,并且单名
select * from student where name like '柯_';
-- 查询姓柯,并且名字两个字
select * from student where name like '柯__';
7.in模糊查询
-- 语法:
select * from 表 where 列 in (值1,值2,....);
-- 需求:查询id是1或3或5的学生信息
select * from student where id = 1 or id = 3 or id =5;
select * from student where id in (1,3,5);
-- 需求:查询名字是蔡徐坤,房祖名和柯震东的学生信息
select * from student where name in('张三','李四','王五');