一、Mysql
# 1.登录数据库客户端 mysql -uroot -p Enter password:****** # 2.查看所有库 show databases; # 3.新建一个库 create database 库名 charset utf8; # 4.进入数据库 use 库名; # 5.查看所有表 show tables; # 6.创建一张表 create table 表名(字段名 类型;字段名 类型,.....); # 7.查看一下表结构 desc 表名; # 8.查看当前在哪个数据库下 select database(); # 9.删除表 drop table表名; # 10.删除库 drop database 库名;
二、Mysql表操作
1.1常用数据类型:
- 整数:int bit
- 小数:decimal
- 字符串:varchar, char
- 日期时间: date, time, datetime
- 枚举类型(enum)
1.2约束
- 主键(primary key):物理上存储的顺序 MySQL建议所有表的主键都叫id字段,类型为int unsigned
- 非空(not null):不允许填写空值
- 唯一(unique):值不允许重复
- 默认(default):当不填写字段对应的值会使用默认值,如果填写时以填写为准
- 外键(foreign key):对关系字段进行约束,当为关系字段填写值时,会到关联的表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并抛出异常
2.1表结构的操作
2.1.1创建表
example: create table subject( ->id int primary key, ->subname varchar(32) ->); create table student( ->id int primary key, ->name varchar(32), ->age int not null, ->snum varchar(32) unique, ->gender int default 1, ->sub_id int, ->foreign key(sub_id) references subject(id) ->);
2.1.2修改表
alter table 表名 add 列名 类型;
2.2数据添加
1)全列插入
insert into 表名 values (……);
2)部分列插入
insert into 表名 (列1,……) values(值1,……);
3)全列多行插入
insert into 表名 values(……),(……);
4)部分列多行插入
insert into 表名(列1,……) values(值1,……),(值2,...)...;
2.3数据修改
update 表名 set 列1=值1,列2=值2... where 条件
2.4数据删除
delete from 表名 where 条件
注意:上面的操作称之为物理删除,一旦删除就不容易恢复。
逻辑删除,本质就是修改操作
例:
update student set isdelete=1 where id=1;
3.数据查询
- 查询所有字段
select * from 表名
- 查询指定字段
select 列1,列2,... from 表名;
例:
select name,age from students
4. 条件
4.1条件查询语法:
select * from 表名 where 条件;
select * from students where id=1;
-
比较运算符
-
逻辑运算符
-
模糊查询
-
范围查询
-
4.3比较运算符
-
-
大于: >
-
大于等于: >=
-
小于: <
-
小于等于: <=
-
不等于: != 或 <>
例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 where is_delete=0;
4.4逻辑运算符
-
-
or
-
select * from students where id > 3 and gender=0;
例6:查询编号小于4或没被删除的学生
select * from students where id < 4 or is_delete=0;
优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用
4.5 模糊查询
-
-
%表示任意多个任意字符
-
例7:查询姓黄的学生
select * from students where name like '黄%';
例8:查询姓黄并且“名”是一个字的学生
select * from students where name like '黄_';
例9:查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';
4.6 范围查询
-
in表示在一个非连续的范围内
例10:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
例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;
注意: between A and B在匹配数据的时候匹配的范围空间是 [A,B]
4.7 空判断
-
注意: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;
4.8 排序的使用
排序查询语法:select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
语法说明
* 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推 * 默认按照列值从小到大排列(asc) * asc从小到大排列,即升序 * desc从大到小排序,即降序
浙公网安备 33010602011771号