MySQL 的增删改查

本文详细介绍了MySQL数据库的常用操作,包括创建、显示、删除和修改数据库,以及操作表的各个步骤,如创建、删除、修改表结构等。此外,还深入探讨了数据的插入、更新、删除和查询,提供了丰富的实例来演示如何使用查询语句,如where子句、order by排序和limit限制。
摘要由CSDN通过智能技术生成

MySQL 的增删改查

0x01 PHP 常用数据库操作 增删改查

进入数据库

 mysql -uroot -p
密码 为数据库的密码 123456

image-20210301142316578

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;

image-20210301142830105

1.2 显示所有数据库

语法:

show databases;

image-20210301143221766

注:默认带四个数据库

1.3 删除数据库

语法:

drop database [if exists] 数据库名
MariaDB [(none)]> drop database `test1`;

-- 判断数据库是否存在,如果存在就删除
MariaDB [(none)]> drop database if exists emp;

image-20210301143509678

1.4 显示创建数据库的语句
show create database 数据库名
MariaDB [(none)]> show create database fengzilin;

image-20210301143646602

1.5 修改数据库

只能修改数据库选项,数据库的选项只有字符编码

语法:

alter database 数据库名 charset=字符编码

image-20210301143832326

数据库中 字符编码之间没有横杠 utf8

1.6 选择数据库

语法:

use 数据库名
MariaDB [(none)]> use fengzilin;

image-20210301144123569

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;

image-20210301144821787

2.2 显示所有表

语法:

show tables;

image-20210301145132838

2.3 显示创建表的语句

语法:

show create table 表名;	 -- 结果横着排列
show create table 表名\G  -- 将结果竖着排列
MariaDB [fengzilin]> show create table stu1\G

image-20210301145225646

2.4 查看表结构

语法:

desc 表名
MariaDB [fengzilin]> desc stu1;

image-20210301145328276

2.5 复制表

语法一:

create table 新表 select 字段 from 旧表
-- 特点:不能复制父表的键,能够复制父表的数据

create table stu2 select id,name from stu1;

image-20210301145703496

语法二:

create table 新表 like 旧表
-- 特点:只能复制表结构,不能复制表数据
MariaDB [fengzilin]> create table stu3 like stu1;

image-20210301145907042

2.6 删除表

语法:

drop table [if exists] 表1,表2,… 
MariaDB [fengzilin]> drop table stu3;

-- 判断表存在就删除
MariaDB [fengzilin]> drop table if exits st4;

image-20210301150051273

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);

image-20210301151142646

3.数据操作

3.1 插入数据

语法:

insert into 表名 (字段名, 字段名,…) values (值1, 值1,…)
MariaDB [fengzilin]> insert into stu (id,name,`add`) values (1,'tom','北京');

image-20210301153035297

3.2 更新数据

语法:

update 表名 set 字段=值 [where 条件]
MariaDB [fengzilin]> update stu set name='fzl' where id=1;
-- 编号(id)为1的学生的名字改为 fzl

image-20210301153417199

3.3 删除数据

语法:

delete from 表名 [where 条件]
MariaDB [fengzilin]> delete from stu where id=1;

image-20210301153554314

3.4 查询数据
select 列名 from 表名
MariaDB [fengzilin]> select id from stu;

image-20210301153745198

-- 查询所有字段的值
MariaDB [fengzilin]> select * from stu;

image-20210301153845281

0x02 实例:查询语句的使用

语法:

语法:select [选项] 列名 [from 表名] [where 条件] [order by 排序] [limit 限制]

1.练习数据

将book 数据导入数据库中

使用 xshell 上传到 服务器 /root 下,然后进入数据库 使用 以下命令导入sql 文件

MariaDB [fengzilin]> source /root/book.sql

image-20210301161611274

2.字段表达式

-- 直接输出内容 生成随机数
MariaDB [fengzilin]> select rand();

image-20210301162438360

通过 as 给字段取别名

MariaDB [fengzilin]> select bId,price  '价格' from books;

image-20210301162800429

3.from 子句

from:来自,from后面跟的是数据源。数据源可以有多个。

MariaDB [fengzilin]> select * from books;

image-20210301162945307

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= '清华大学出版社';

image-20210301163442313

-- 查找作者为 苗壮 或者 白立超的书
MariaDB [fengzilin]> select * from books where author='苗壮' or  author='白立超';

image-20210301163838208

5.order by 排序

asc:升序【默认】

desc:降序

例题:

-- 将 bTypeId 按照从大到小 排序
MariaDB [fengzilin]> select * from books order by bTypeId desc;

image-20210301164249014

6.limit

语法:limit [起始位置],显示长度

-- 找出最贵的5本书
MariaDB [fengzilin]> select * from books order by price desc limit 0,5;

image-20210301164559189

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值