MySQL学习笔记——常用指令

1. 库命令

  1. Show databases 显示数据库
  2. Create database 库名 创建数据库
  3. Drop database 库名 删除数据库
  4. Use 库名 使用数据库

2. 表命令

1.Show tables 显示当前数据库中的表
2.Create table 表名 创建一个数据表

列名称1 数据类型 [约束]
列名称2 数据类型 [约束]
列名称3 数据类型 [约束]
… …

主键约束:(primary key)不重复且不为空,通过主键唯一确定一条记录
可以写在每一行末尾,也可以写在最后,当需要用联合主键时,则写在最后,如primary key ( userId,name )
外键约束 :涉及到两个表,主表和副表,外键约束写在副表中。
例子:Foreign key(class_id) references classes(id)
这个id必须为主键,而class_id不可以是主键
主表 classes 中没有的数据值,在副表中是不可以使用的。由于主表中的记录可能会被副表引用,是不可以被修改和删除的,必须先解除外键约束。
自增约束 :auto_increment
非空约束:not null
唯一约束:unique,可以类似联合主键添加在末尾,不同时重复就可以
默认约束:default 数据(与类型对应),类似缺省形参
常见类型: int , char (定长字符), varchar(不定长字符)
3. Desc 表名 查看表的信息,更详细记录 show create table 表名
4. Drop table [if exists] 表名 删除数据表
5. 插入字段
Insert into 表名 values(值1,值2,值3,)
Insert into 表名 (列1,列2,列3) values (值1,值2,值3)
6. 查询表中记录
Select 字段1,字段2,,, from 表名 where 条件
Select * from 表名 条件
7. 更新记录(修改)
Update 表名 set 字段=新内容 where 条件
8. 删除纪录
Delete from 表名 where 条件
9. ALTER修改表结构
①基本的增删改

alter table table_name
	 rename  新表名                         //改表名
	 
	 add  字段  类型  [约束]                //添加字段
	 add  字段  类型  [约束] after 字段名   //按一定顺序添加字段
	 add  字段  类型  [约束] first         //添加到最前面
	
	drop [if exist]  字段                  //删除字段
	
	modify 字段 数据类型 [完整约束]            //改字段的约束
	change 旧字段名 新字段名 新数据类型 [完整约束]  //改字段名+类型
	change 旧字段名 新字段名 旧数据类型 [完整约束]  //改字段名

②主键操作

alter table 表名 add primary key (字段名)  //添加主键
alter table 表名 drop primary key          //删除主键
alter table 表名 modify 字段 字段名 primary key  修改字段添加主键约束

③外键操作

alter table table_name 
	add constraint [foriegn key name] foriegn key (字段) references 表名(字段)                                           //添加外键
	add foreign key(字段) references 表名(字段)  //添加外键
	drop foreign key [外键名]                     //删除外键

删除外键后用desc 查看表结构会发现还有mul标志,这是因为创建了索引,使用show index from 表名找到对应的索引(一般就是对应的字段名),再用drop index 索引名 on 表名即可解决这一问题。
(约束名可以通过show create table表名查到)

3. 查询指令

1. 直接查询
select * from 表名直接查询表中所有数据
select 字段1,字段2,字段2 from 表名 [where …]
可以直接列出所有的字段,与第一条等价,但是指定字段查询效率更高
2. 去重 distinct
select distinct 字段 from 表名
3. 排序 order by
select * from 表名 order by 字段1 [desc/asc],字段2 [desc/asc]
排序默认按照ascll升序,使用desc则按照降序排列,如果按多个字段排序则依次进行,对前面排序好的内容再进一步排序,不可违背已有的排序规则
4. 限制 limit
Select * from 表名 order by 字段 limit 5 显示5行 (常和排序连用)
Select * from 表名 limit 3,5 从第三行开始,显示5行
5. 聚合
汇总函数,如sum求和,count统计,max最大值,min最小值,avg平均值等
使用示例:select sum(salary) from jpb
Group by,对于分类聚合的字段进行分组
使用实例:select age count(1) from job group by age;得到按年龄分组的人数统计
With ,对分组的数据再次汇总
使用示例:select age count(1) from job group by age with rollup;既统计各年龄段人数,又统计总人数
Having 再次过滤,分类后过滤,而where是分类前进行过滤
select age count(1) from job group_by age with rollup having count(1)>1;
这样将会把分组后人数不大于1的过滤掉。

6. 子查询
将一条查询语句的结果作为另一条查询语句的条件,常用关键词(in , not in , = , != ,exists , not exists等)
示例:select * from job where type in (select type from job_type);

7.联合查询
Select type from job union all select type from job_type; 直接将结果合并
Select type from job union select type from job_type; 对结果进行去重

8.表连接查询
内连接,选出两张表中相互匹配的内容(第二条语句与内连接效果一样)
Select job.name , job_type.name from job join job_type on job.type=job_type.type;
Select job.name , job_type.name from job , job_type where job.type=job_type.type;

左外连接:选出包含左表的全部纪录
Select job.name , job_type.name from job left join job_type on job.type=job_type.type;

右外连接:选出包含右表的全部记录
Select job.name , job_type.name from job right join job_type on job.type=job_type.type;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值