MYSQL常用操作指令

MySQL关系型数据库NoSQL数据库

点击“前端自学社区”查看更多精彩

picture.image

        前两章介绍了PHP7的基本语法和面向对象开发,本章将介绍Mysql的基本使用和一些常用指令,PHP + Mysql 是一对孪生兄弟,两个结合就可以为网站或者APP 做后端。MYsql 的操作熟练,决定了你能否写出一手好CURD,也为你使用PHP做接口开发更丝滑了。那么我们开始吧。

Mysql

基本语句

命令行操作

创建数据库

  
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 在2040之间的数据  
  
  
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,类型为charadd ***  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秒  ---> 19990412 08时2033

时间常用函数

  
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 年  
  
  

以上介绍了Mysql的基本增删改查,和一些使用技巧,只要你多实操就会越来越熟练。Mysql掌握差不多了,就可以使用结合PHP来开发一些动态网站了。

——前端扫地僧

picture.image

ES6_11_Module 的语法(import, export,export default )

ES6_08_Promise

ES6_02_变量解构赋值

PHP7-1:从0开始入门学习

PHP7-2: 面向对象开发

Android 基础入门干货分享 (UI控件篇)

ES6系列文章已经在公众号更新完

picture.image

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
云原生数据库 veDB 核心技术剖析与展望
veDB 是一款分布式数据库,采用了云原生计算存储分离架构。本次演讲将为大家介绍火山引擎这款云原生数据库的核心技术原理,并对未来进行展望。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论