-- 查看当前数据库中所有表showtables;-- • 创建表-- • auto_increment 表示自动增长-- ENUM 枚举类型第一个位置值是1-- decimal(a,b) 长度a 小数点b createtable students(
id intunsignedPRIMARYkeyauto_incrementnotnull,
name VARCHAR(10)DEFAULT'',
age tinyintunsigneddefault0,
height decimal(5,2),
gender ENUM('男','女'));-- 修改表-添加字段altertable students add add_col int;-- 修改表-修改字段:重命名版-- alter table 表名 change 原名 新名 类型及约束;altertable students change add_col col1 DATEnotnull;-- 修改表-修改字段:不重命名版-- alter table 表名 modify 列名 类型及约束;altertable students modify col1 DATETIMEnotnull;-- 修改表-删除字段-- alter table 表名 drop 列名;altertable students drop col1;-- 查看表的创建语句-- show create table 表showcreatetable students;-- 删除表-- drop table 表名;droptable classes;-- • 查看表结构-- desc 表名desc students;
对表内容的-增删查改
-- curd 的解释: 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)-- 增加-- 格式:INSERT [INTO] tb_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... • 说明:主键列是自动增长,但是在全列插入时需要占位,通常使用 0 或者 default 或者 null 来占位,插入成功后以实际数据为准-- 全列插入:值的顺序与表中字段的顺序对应insertinto students values(0,"张三",18,175,1);-- 部分列插入:值的顺序与给出的列顺序对应insertinto students(id,name)values(0,"李四");-- 一次插入多行数据,VALUES 后面用 , 隔开insertinto students values(0,"王五",16,173,2),(0,"马六",20,178,1);insertinto 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=18where id=6;-- 删除-- DELETE FROM tbname [where 条件判断]-- delete from 表名 where 条件deletefrom 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 可以消除重复的行selectdistinct 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 >3and gender=0;-- 例 6:查询编号小于 4 或没被删除的学生select*from students where id <4or 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 between3and8;-- 例 12:查询编号是 3 至 8 的男生select*from students where(id between3and8)and gender=1;-- 空判断-- • 注意:null 与''是不同的-- • 判空 is null-- 例 13:查询没有填写身高的学生select*from students where height isnull;-- • 判非空 is not null-- 例 14:查询填写了身高的学生select*from students where height isnotnull;-- 例 15:查询填写了身高的男生select*from students where height isnotnulland gender=1;-- 优先级 •优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符-- • and 比 or 先运算,如果同时出现并希望先算 or,需要结合()使用
备份及恢复
备份-运行 mysqldump 命令
注: > 重定向到当前目录下
mysqldump -u root -p Python >python.sql
恢复
连接mysql,创建一个空的数据库,然后执行下列命令
mysql -u root -p 新数据库名 < python.sql(注意数据库中必须存在对应的数据库名称)