1、DDL库操作
关键字:
database:数据库
create:创建
alter:修改
drop:删除
show:展示
use:使用
1)显示当前系统中所有数据库
show database 数据库名;
2)创建数据库(不指定字符集)
create database 数据库名;
3)创建数据库(指定字符集)
create database 数据库名 character set 字符集;
4)查看指定的数据库的信息
show create database 要查看的数据库名;
5)修改数据库字符集
alter database 要修改的数据库名 character set 新字符集;
6)指定要操作的数据库
use 数据库名;
7)drop database 数据库名;(谨慎使用)
2、DDL表操作
1)数据库常用的数据类型
类型
用途
说明
int
整型
整数
float
浮点型
单精度
double
浮点型
双精度
char
字符串
固定长度,少于给定长度时用空格填充
varchar
字符串
可变长度,少于给定长度时按实际长度分配空间
date
日期
年月日
time
日期
时分秒
datetime
日期
年月日时分秒,最大时间9999-12-31 23:59:5 ,默认值是null
timestamp
日期
年月日时分秒,最大时间2037-12-31 23:59:59,默认值是当前系统时间
数据库表操作的关键字:
table:表
create:创建
alter:修改
drop:删除
desc/show:显示
1)创建数据库(指定当前操作的数据库是哪一个)
use 数据库名
create table 表名(列名 列数据类型(长度),列名 列数据类型(长度)...);
2)查看当前库中所有表
show tables;
3)查看指定表的表结构
desc 表名;
4)修改表名
alter table 旧表名 rename 新表名;
5)修改列,字符串类型,长度
alter table 表名 change 旧列名 新列名 数据类型(长度);
6)新增列,字符串类型,长度
alter table 表名 add 列名 数据类型(长度);
7)删除列
alter table 表名 drop 列名;
8)删除表
drop table 表名;
DML数据操纵语言
关键字:
table:表
insert into:新增
update set:修改
delete from:删除
1、新增操作
1)单行添加(值1的数据类型必须符合列名1的要求,........)
insert into 表名(列名1,列名2,......) values(值1,值2,.......);
2)多行添加
insert into 表名(列名1,列名2,......) values(值1,值2,.......),(值1,值2,.......),...;
2、修改数据(修改通常都会有where)
update 表名 set 列名=值,列名=值,...... where 列=值;
3、删除指定的数据
delete from 表名 where 列=值;
两种清空数据方式的不同:
1、delete清空,将表中数据一条一条删除
2、truncate清空,现将表删除,再创建一张跟之前一样的表
DQL数据查询语言
主要对表进行查询操作。查询操作只是将需要的数据从原始表中筛选出来,对原始表数据没有任何影响
关键字:select
单表查询
1、查询tb_student表所有的数据
select * from 表名;
2、查询某列数据
select 列名1,列名2,...... from 表名;
3、查询的同时起别名
select 列名 as 别名,列名 as 别名... from 表名;(as可以省略)
4、按照指定要求查询
select * from 表名 where 列=值;-----等于
select * from 表名 where 列!=值;-----不等于
select * from 表名 where 列<>值;-----不等于
select * from 表名 where 列=值 and 列=值;-----并集
select * from 表名 where 列=值 && 列=值;-----并集
select * from 表名 where 列=值 or 列=值;-----交集
select * from 表名 where 列=值 || 列=值;-----交集
select * from 表名 where 列>=值 and 列<=值;-----小于等于
select * from 表名 where 列 is null;-----值为空
select * from 表名 where 列 is not null;-----值不为空
select * from 表名 where 列 between 值1 and 值2;---两者值之间
select * from 表名 where 列 in(值1,值2,值3,...);---指定几个值
5、模糊查询
模糊查询关键字like
通配符:
%:任意个字符
_:单个字符
语法:select * from tb_student where 列 like ‘要求’;
6、limit关键字
select * from 表名 limit 数值;---指定前几行的数据
7、分页查询:limit
select * from 表名 limit 数值1,数值2
数值1:从第几条开始拿数据(数据的索引)
数值2:拿几条数据
提供的信息:页码page,每页显示条数:count
数值1的变化规律:(page-1)*count
8、排序
order by 要排序的列 asc(升序)/desc(降序)/不写(默认升序)
语法:select * from 表名 order by 列 顺序;
9、去重
关键字;ditinct
语法:select distinct 列名 from 表名;