MySQL 的增删改查
文章目录
0x01 PHP 常用数据库操作 增删改查
进入数据库
mysql -uroot -p
密码 为数据库的密码 123456
1.数据库的操作
1.1 创建数据库
语法:
create database [if not exists] 数据名 [选项]
-- 创建数据库
MariaDB [(none)]> create database fengzilin;
-- 多次创建相同名称数据库,会提示报错
MariaDB [(none)]> create database `test`;
# ERROR 1007 (HY000): Can't create database 'test'; database exists
-- 特殊字符、关键字做数据库名,使用反引号将数据括起来
MariaDB [(none)]> create database `test1`;
-- 创建数据库时指定存储的字符编码,默认使用安装数据库时指定的编码
MariaDB [(none)]> create database emp charset=gbk;
1.2 显示所有数据库
语法:
show databases;
注:默认带四个数据库
1.3 删除数据库
语法:
drop database [if exists] 数据库名
MariaDB [(none)]> drop database `test1`;
-- 判断数据库是否存在,如果存在就删除
MariaDB [(none)]> drop database if exists emp;
1.4 显示创建数据库的语句
show create database 数据库名
MariaDB [(none)]> show create database fengzilin;
1.5 修改数据库
只能修改数据库选项,数据库的选项只有字符编码
语法:
alter database 数据库名 charset=字符编码
数据库中 字符编码之间没有横杠 utf8
1.6 选择数据库
语法:
use 数据库名
MariaDB [(none)]> use fengzilin;
2.表的操作
2.1 创建表
语法:
create table [if not exists] `表名`(
`字段名` 数据类型 [null|not null] [default] [auto_increment] [primary key] [comment],
`字段名 数据类型 …
)[engine=存储引擎] [charset=字符编码]
-- 解释
null|not null 是否为空
default: 默认值
auto_increment 自动增长,默认从1开始,每次递增1
primary key 主键,主键的值不能重复,不能为空,每个表必须只能有一个主键
comment: 备注
engine 引擎决定了数据的存储和查找 myisam、innodb
#表名和字段名如果用了关键字,要用反引号引起来。
-- 设置客户端和服务端通讯的编码
MariaDB [fengzilin]> set names gbk;
-- 创建简单的表
MariaDB [fengzilin]> create table stu1(
-> id int auto_increment primary key,
-> name varchar(20) not null
-> )engine=innodb charset=gbk;
2.2 显示所有表
语法:
show tables;
2.3 显示创建表的语句
语法:
show create table 表名; -- 结果横着排列
show create table 表名\G -- 将结果竖着排列
MariaDB [fengzilin]> show create table stu1\G
2.4 查看表结构
语法:
desc 表名
MariaDB [fengzilin]> desc stu1;
2.5 复制表
语法一:
create table 新表 select 字段 from 旧表
-- 特点:不能复制父表的键,能够复制父表的数据
create table stu2 select id,name from stu1;
语法二:
create table 新表 like 旧表
-- 特点:只能复制表结构,不能复制表数据
MariaDB [fengzilin]> create table stu3 like stu1;
2.6 删除表
语法:
drop table [if exists] 表1,表2,…
MariaDB [fengzilin]> drop table stu3;
-- 判断表存在就删除
MariaDB [fengzilin]> drop table if exits st4;
2.7 修改表
语法:
alter table 表名
创建初始表
create table stu(
id int,
name varchar(20)
);
添加字段:alter table 表名add [column] 字段名 数据类型 [位置]
MariaDB [fengzilin]> alter table stu add `add` varchar(20);
3.数据操作
3.1 插入数据
语法:
insert into 表名 (字段名, 字段名,…) values (值1, 值1,…)
MariaDB [fengzilin]> insert into stu (id,name,`add`) values (1,'tom','北京');
3.2 更新数据
语法:
update 表名 set 字段=值 [where 条件]
MariaDB [fengzilin]> update stu set name='fzl' where id=1;
-- 编号(id)为1的学生的名字改为 fzl
3.3 删除数据
语法:
delete from 表名 [where 条件]
MariaDB [fengzilin]> delete from stu where id=1;
3.4 查询数据
select 列名 from 表名
MariaDB [fengzilin]> select id from stu;
-- 查询所有字段的值
MariaDB [fengzilin]> select * from stu;
0x02 实例:查询语句的使用
语法:
语法:select [选项] 列名 [from 表名] [where 条件] [order by 排序] [limit 限制]
1.练习数据
将book 数据导入数据库中
使用 xshell 上传到 服务器 /root 下,然后进入数据库 使用 以下命令导入sql 文件
MariaDB [fengzilin]> source /root/book.sql
2.字段表达式
-- 直接输出内容 生成随机数
MariaDB [fengzilin]> select rand();
通过 as 给字段取别名
MariaDB [fengzilin]> select bId,price '价格' from books;
3.from 子句
from:来自,from后面跟的是数据源。数据源可以有多个。
MariaDB [fengzilin]> select * from books;
4.where 子句
where后面跟的是条件,在数据源中进行筛选。返回条件为真记录
MySQL支持的运算符
-- 比较运算符
> 大于
< 小于
>= 大于等于
<= 小于等于
= 等于
!= 不等于
-- 逻辑运算符
and 与
or 或
not 非
-- 其他
in | not in -- 字段的值在枚举范围内
between…and|not between…and -- 字段的值在数字范围内
is null | is not null -- 字段的值不为空
例题:
-- 查找 publishing为 清华大学出版社
MariaDB [fengzilin]> select * from books where publishing= '清华大学出版社';
-- 查找作者为 苗壮 或者 白立超的书
MariaDB [fengzilin]> select * from books where author='苗壮' or author='白立超';
5.order by 排序
asc:升序【默认】
desc:降序
例题:
-- 将 bTypeId 按照从大到小 排序
MariaDB [fengzilin]> select * from books order by bTypeId desc;
6.limit
语法:limit [起始位置],显示长度
-- 找出最贵的5本书
MariaDB [fengzilin]> select * from books order by price desc limit 0,5;