- 库操作
- mysql -u root -p; -----------------------------启用 进入musql
- show databases; -----------------------------查看所有库
- create database 库名 default charset=utf8; --创建库
- 表操作
- --------------------表结构操作------------------------------
- show tables; ----------------------------------查看库中的表
- desc 表名; ------------------------------------查看表结构
- create table 表名( 字段 属性(约束条件) ,) default charset = utf8; --------创建表
- drop table 表名--------------------------------删除表
- alter table 表名 add 新字段(约束条件)--------------------------添加新字段,默认尾部添加
- alter table 表名 add 新字段(约束条件) after 字段-------------- after 向某个字段后追加一个新字段
- alter table 表名 drop 指定字段 --------------------------------- 指定删除某个字段
- alter table 表名 change 旧字段名 新字段名 (约束条件)--------- change 可修改字段名
- alter table 表名 modify 字段(新约束条件)-----------------------不可修改字段名,只能修改字段属性
- alter table 表名 add unique 字段(唯一索引)-------------------- unique 添加唯一索引
- alter table 表名 add index 字段( 普通索引) -------------------- index 添加普通索引
- alter table 表名 drop (unique or index) 索引值 ---------------- drop 删除索引
- delete from 表名 ------------------------------------------------ 删除数据,(不重置自增)
- truncate table 表名 --------------------------------------------- 清空表数据,重置自增
- alter table 旧表名 rename as 新表名 --------------------------- rename as 修改表名
- alter table 表名 auto_increment = 1 ---------------------------- 重置自增初始值
- alter table 表名 engine = 'myisam'-------------------------------更改表类型
- innodb在删除数据并重启后,会重新计算最大的id
- myisam在删除数据并重启后,不会重新计算,
- -- 常用的表类型(引擎) Innodb Myisam
- Innodb
- 当前表是由两个文件组成 .frm .ibd
- 一个存储 表结构,一个存储数据和索引
- 对数据的操作效率高,
- 支持事务
- Myisam
- 一个表由 三个 文件组成 .frm .MYD .MYI
- 一个存储表结构,一个存储数据,一个存储索引
- 数据的读,效率高
- MyISAM相对简单所以在效率上要优于InnoDB
- 不支持事务
- ----------------------数据增删改查----------------------------
- select * from 表名 ;--------------------------------------------------查询表内数据;
- select 字段1,字段2 from 表名---------------------------------------查询表内指定字段数据
- select 字段1 as 别名 from 表名-------------------------------------- as 别名查询(用于长字段重复操作)
- select * from 表名 where 条件 -------------------------------------- where 条件查询
- 可使用比较运算符 > < >= <= !=
- 逻辑运算 and or in not in
- select * from 表名 where 字段 like '_%' ----------------------------- like 模糊查询
- % 代表任意位数的任意字符
- _ 代表任意的单个字符
-
- select max(age) ,min(age),avg(age) from 表名; -------------------- 统计函数(聚合函数)
- max() 最大值 min() 最小值, avg() 平均值 cont() 总个数 sum() 和 数值
- select 字段, 字段1 from 表名 group by 分组字段;------------------- group by 分组统计
- 分组 根据一个或者多个列队进行分组 用于统计 给某个字段 进行分组
- 分组 根据一个或者多个列队进行分组 用于统计 给某个字段 进行分组
- select * from 表名 order by 字段[id] -(默认esc)-------------------- order by 排序 asc升序 desc降序
- 可以在where条件后面 指定字段进行排序
- 默认为升序 asc
- 也可以降序 desc
- select * from 表名 limit 2 ( 某表前两条数据) ------------------------- limit 分页 关键字 查询部分数据
- 一共10条数据,每页显示2条,
- 第一页 limit 0,2
- 第二页 limit 2,2
- 第三页 limit 4,2
- 第n页 limit (n-1)*2,2
- 第n页 limit (n-1)*2,2
- select * from [where ...] [group by ...] [ order by ...] [limit ...] -----sql 语句 顺序
- select 表1.字段,表2 字段 from 表1,表2 where 表1.id = 表2.id;-------- where 多表连查( 两表要有关联字段)
- -- 查询所有学生信息,并跨表显示对应的班级名称信息
- select stu.*,class.* from stu,class where stu.classid = class.id;
- -- select s.*,c.* from stu s,class c where s.classid = c.id;
- select stu.*,class.cname from stu,class where stu.classid = class.id;
- -- select s.*,c.cname classname from stu s,class c where s.classid = c.id;
- -- 获取张三和所在班级信息
- select stu.*,class.cname from stu,class where stu.classid = class.id and stu.username ='张三';
- -- select stu.id,stu.username,class.cname from stu,class where stu.classid = class.id and stu.username ='张三';
- -- 统计每个班级的人数
- -- select classid,count(id) from stu group by classid; X
- select c.cname,count(s.id) from stu as s,class as c where s.classid = c.id group by s.classid;
- select 表1.*, 表2.* from 表1 inner join 表2 on 表1.id = 表2.id;------ inner join on 链接式查询
- inner join 内连接 获取两个表中字段匹配关系的记录。
- left join 左链接 获取左表所有记录,即使右表没有对应匹配的记录
- right join 右链接 与 LEFT JOIN 相反
- insert into 表名 ( 字段) ,values(值) ------------添加值
- -- 指定部分字段和值 注意,有些字段不能为null,必须写
- insert into stu(username) values('aabbcc');
- -- 批量添加
- insert into stu values
- (null,'ccc',20,'男'),
- (null,'eee',20,'男'),
- (null,'adf',20,'男');
- delete from 表名 where id = ? ---------------删除指定数据
- updata 表名 set 字段 = 新值,字段=新值 where 条件-----------修改字段值
- --------------------表结构操作------------------------------
- 事务
- 事务就是把 多条sql操作 看成一个操作单元,要么都成功,要么都失败
- 注意,只右innodb支持事务
- BEGIN 事务开启
- ROLLBACK 事务回滚 意味回到事务开启前的状态
- COMMIT 事务提交 意味着执行事务中所有操作
mysql 数据库 简单命令
最新推荐文章于 2022-06-02 09:29:12 发布