MySQL基础语法及MySQL的备份及恢复

本文介绍了如何对MySQL数据库进行操作,包括创建、查看、修改和删除数据表,以及对表内容的增、删、查、改(CRUD)操作。此外,还详细阐述了如何使用mysqldump进行数据库备份,并通过恢复命令还原数据。
摘要由CSDN通过智能技术生成

对数据库的操作

在这里插入图片描述

show databases;

create database day1 charset=utf8;

use day1;

select database();

drop database day1;

对数据表的操作DDL

对表的操作

-- 查看当前数据库中所有表
show tables;

-- • 创建表

-- • auto_increment 表示自动增长
-- ENUM 枚举类型第一个位置值是1
-- decimal(a,b) 长度a 小数点b 
create table students(
	id int unsigned PRIMARY key auto_increment not null,
	name VARCHAR(10) DEFAULT '',
	age tinyint unsigned default 0,
	height decimal(5,2),
	gender ENUM('男','女')
);


--  修改表-添加字段
alter table students add add_col int;

-- 修改表-修改字段:重命名版
-- alter table 表名 change 原名 新名 类型及约束;
alter table students change add_col col1 DATE not null;

-- 修改表-修改字段:不重命名版
-- alter table 表名 modify 列名 类型及约束;
alter table students modify col1 DATETIME not null;

-- 修改表-删除字段
-- alter table 表名 drop 列名;
alter table students drop col1;

-- 查看表的创建语句
-- show create table 表
show create table students;

--  删除表
-- drop table 表名;
drop table classes;

-- • 查看表结构
-- desc 表名
desc students;

对表内容的-增删查改

-- curd 的解释: 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)

-- 增加
-- 格式:INSERT [INTO] tb_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... • 说明:主键列是自动增长,但是在全列插入时需要占位,通常使用 0 或者 default 或者 null 来占位,插入成功后以实际数据为准

-- 全列插入:值的顺序与表中字段的顺序对应
insert into students values(0,"张三",18,175,1);

-- 部分列插入:值的顺序与给出的列顺序对应
insert into students(id,name) values(0,"李四");

-- 一次插入多行数据,VALUES 后面用 , 隔开
insert into students values(0,"王五",16,173,2),(0,"马六",20,178,1);
insert into students(id,name) values(0,"七"),(0,"八");


-- 修改

-- 格式: UPDATE tbname SET col1={expr1|DEFAULT}[,col2={expr2|default}]...[where 条件判断]
 
-- update 表名 set 列 1=值 1,列 2=值 2... where 条件
update students set name = "七",age=18 where id=6;


-- 删除

-- DELETE FROM tbname [where 条件判断]
-- delete from 表名 where 条件
delete from students where name="七";


-- 查询基本使用
-- • 使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;

-- • 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;

-- • 在 select 后面列前使用 distinct 可以消除重复的行
select distinct gender from students;

-- 使用 where 子句对表中的数据筛选,结果为 true 的行会出现在结果集中
select * from students where id=1;

-- • where 后面支持多种运算符,进行条件的处理
-- – 比较运算符
-- – 逻辑运算符
-- – 模糊查询
-- – 范围查询
-- – 空判断

-- 比较运算符
-- • 是否等于: =
-- • 大于: >
-- • 大于等于: >=
-- • 小于: <
-- • 小于等于: <=
-- • 不等于: != 或 <>

-- 例 1:查询编号大于 3 的学生
select * from students where id > 3;
-- 例 2:查询编号不大于 4 的学生
select * from students where id <= 4;
-- 例 3:查询姓名不是“黄蓉”的学生
select * from students where name != '黄蓉';
-- 例 4:查询没被删除的学生
select * from students 

-- 逻辑运算符
-- • and
-- • or
-- • not

-- 例 5:查询编号大于 3 的女同学
select * from students where id > 3 and gender=0;
-- 例 6:查询编号小于 4 或没被删除的学生
select * from students where id < 4 or is_delete=0;

-- 模糊查询
-- • like
-- • %表示零个或任意多个任意字符
-- • _表示一个任意字符

-- 例 7:查询姓黄的学生
select * from students where name like '黄%';
-- 例 8:查询姓黄并且“名”是一个字的学生
select * from students where name like '黄_';
-- 例 9:查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';

-- 范围查询
-- • in 表示在一个非连续的范围内

-- 例 10:查询编号是 1 或 3 或 8 的学生
select * from students where id in(1,3,8);
-- • between ... and ...表示在一个连续的范围内,闭区间
-- 例 11:查询编号为 3 至 8 的学生
select * from students where id between 3 and 8;
-- 例 12:查询编号是 3 至 8 的男生
select * from students where (id between 3 and 8) and gender=1;


-- 空判断
-- • 注意:null 与''是不同的
-- • 判空 is null

-- 例 13:查询没有填写身高的学生
select * from students where height is null;
-- • 判非空 is not null
-- 例 14:查询填写了身高的学生
select * from students where height is not null;
-- 例 15:查询填写了身高的男生
select * from students where height is not null and gender=1;


-- 优先级 •优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
-- • and 比 or 先运算,如果同时出现并希望先算 or,需要结合()使用

备份及恢复

备份-运行 mysqldump 命令

注: > 重定向到当前目录下

mysqldump -u root -p Python >python.sql

恢复

连接mysql,创建一个空的数据库,然后执行下列命令

mysql -u root -p 新数据库名 < python.sql(注意数据库中必须存在对应的数据库名称)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值