基本语句
命令行操作
创建数据库
create database shop charset utf8;
复制
展示数据库
show databases;
复制
使用/删除数据库
use shop ;
drop databases shop;
drop table tbale 删除表
drop databases if exists shop; 如果shop 存在,则删除,否则报错
复制
查看数据库结构
show databases;
复制
查看表结构
desc stduent;
复制
创建表
create table student (id int PRIMART KEY AUTO_INCREMENT,name varchar(20) not null);
复制
插入语句
insert into student set name = '张三';
insert into student (name,age) values('张三',20);
insert into student (name,age) values('张三',20),('张三',20); 一次插入多个值
复制
创建表复制其他表结构
create table school like student; #创建表school结构来源于studetn表
insert into school select * from student; 复制student表数据到school中
insert into school (name) select name from student; 将student表中字段为name的值复制到 school 表中
create table class select * from student 在创建表时将student的所有数据复制到class表中
create table bclass (id INT PRIMARY KEY AUTO_INCREMENT,name varchar(30) ) select name from student 在创建bclass表时,将stdent表中字段为name的值复制到 bclass表中
复制
查询语句
select * from class;
select name,id from class;
select * from class where name = '张三' ; 查找name为张三的所有数据
模糊查询
select * from class where name like '%三%' and age > 22; 查询name字段中包括三并且age>22 的数据
select * from class where name not like '%三%' and age > 22; 查询name字段中不包括三并且age>22 的数据
连接字段使用
select concat(name,age) as info from student; 将student 表中的name ,age 字段合并 返回字段为info 的所有数据
select * from student where info = 2 and age >22; 查询student表中info等于2并且age>22的所有数据
select * from shop where price between 20 and 40; 查找shop表中price 在20 到 40之间的数据
select * from shop where price = 20 or prince = 30 查找shop表中price等于20 或者 等于30
select * from shop where pricle not in (20,30) 查找shop表中price不在20 30 这个范围
复制
Mysql 对Null 的处理
select name,if(age,age,'没有数据') from shop; 查找shop表中name和age字段的数据,当age为空时, 显示 ‘没有数据’,有数据则为age
复制
排序 order by
order by 字段 asc
asc 从小到大
desc 从大到小
select age,name from student order by age asc; 查找student表中,按年龄从小到大输出age和name的数据。
复制
Limt 使用
limit 开始索引,取得数量;
select * from student order by asc limit 1,2; 从student表中按从小到大取2个数据
select age form student where class_id = 2 and age is not null order by age asc limit 1; 查找student表,条件为class_id为2并且age不为空,年龄按从小到大排序,只显示age字段为1条数据
复制
子查询
查询的条件依据另一条sql语句的结果查询
select * from student where age = (select age from student where class_id = 3 and age > 23 limit 1)
复制
更新
update student set class_id = 2 where class_id is null;
update student set price = price+10 where name = '海军'; 当name为海军时,将price+10分
复制
删除
delete from student where price < 60 and place is not '北京';
复制
小结
数据的增删改查的结构几乎相近,结构为
select * from 表 where 按条件查询 ---------------- 按条件查询
update 表 set 字段 where 条件语句 ---------------- 按条件更新
delete from 表 where 条件语句 --------------- 按条件删除具体语句
insert into 表 set 字段 = 值 ;
insert into 表 (字段1,字段2) values (字段1值,字段2值);
insert into 表 (字段1,字段2) values (字段1值,字段2值),(字段1值,字段2值),(字段1值,字段2值);
一次插入多个值
数据库表功能使用
修改表名字
alter table table1 rename table2
将表1 改成表2 名字
rename table table1 to table2
将表1 改成表2 名字
复制
字段修改
modify 修改类型
alter table table1 modify name varchar(20) not null;
修改table1中的字段为name,类型改为varchar
change 修改字段名和类型
alter table table1 change name sname char(20) not null;
修改table1表中的字段为name,改为sname,类型修改为char
add 字段添加
alter table table1 add sex char(20) not ull;
修改table表,添加一个字段为sex,类型为char;
add *** after 指定字段在哪个字段前增加
alter table table1 add sex char(20) not null after name;
修改table表,添加一个字段为sex,类型为char,并且顺序在name的后面
alter***drop 删除字段
alter table table1 drop name
删除table1表字段为name;
复制
小结
任何修改操作都是这样的结构: alter table 表名 功能函数 作用语句
eg: alter table student modify age int not null;
alter table student change name username varchar(20) not null;
校对规则
mysql 默认是不区分大小写,想要改掉, 可以为字段添加排序规则为 utf8_bin
复制
时间格式化
DATE_FORMAT(字段名,'显示格式')
TIME_FORMAT(字段名,'显示格式')
%Y年%m月%d %H时%i分%s秒 ---> 1999年04月12 08时20分33秒
复制
时间常用函数
now() 获取当前时间 -----> 2020-03-13 22:22:38
CURRENT_DATE() 获取当前日期 ------> 2020-03-13
TIME_TO_SEC(time) 将时间转为秒
SEC_TO_TIME(seconds) 将秒转为时间
addTime(now(),'08:00:00') ----> 在现在的时间上加8个小时
复制
日期与实践差值计算
-- 计算日期的差值
DATEDIFF(now(),birthday)
--计算现在日期到出生日期经过了多少天
--计算时间的差值
timediff(time(now()),time(birthday))
-- 生日时间到现在经过的时间差值
#常用#-- 根据单位来获取时间的差值,例如获取差值多少小时,多少年 --
timestampdiff(day,birthday,now())
--出生到现在所经历了多少天 ,day可以更换单位, year 年
复制