python数据库操作命令mysql使用手册

数据库操作命令

【数据库操作分为对库的操作和表和数据的操作,这里做简单介绍,命令和用法】
【注意: 命令以\g或者;结束 quit或者exit退出】

一、对库的操作

1、查看库

show databases;

2、创建库

create database 数据库名 default charset=utf8;
#数据库名不要纯数字,不要汉字

3、删除库

drop database 数据库名;

4、选中库

use 数据库名;

5、查看库中表

show tables;

6、查看数据库创建语句

show create database 数据库名;

7、查看选中的数据库

select database();

8、修改数据库字符集

alter database 数据库名 default charset=utf8;

二、对表操作

1、创建表

create table if not exists 表名(
列名1      类型	【限制】,
列名2      类型	【限制】,
...
列名n	类型	【限制】
)[engine = myisam | innodb][default charset = utf8];
#注释:primary key 主键   不允许重复,不允许为空
	  auto_increment	自增长,只能对int型主键起作用
	  复合主键,在最后加 primary key(sid,cid);

2、删除表

drop table 表名;

3、查看表结构

desc 表名;

4、查看建表语句

show create table 表名;

5、修改表

----修改字段类型

alter table 表名 modify 字段名 类型 [限制];

----增加字段

alter table 表名 add [column] 字段名 类型 [限制];

----删除字段

alter table 表名 drop [column] 字段名;

----修改字段名和类型

alter table 表名 change [column] 旧字段名 新字段名 类型 [限制];

----修改表名

alter table 表名 rename 新表名;

----通过first,after指定插入位置

alter table 表名 add 字段 类型 [限制] after 指定字段名;
alter table 表名 add 字段 类型 [限制] first 指定字段名;

字段限制

primary key	主键
not null	非空
unique	唯一
default	默认

三、对数据操作

1、insert
写法一:insert into 表名(字段1,字段2) values (值1,值2);
写法二:insert into 表名 values (值1,值2...); #每一列都要给值
写法三:#插入多行数据: insert into 表名 (字段1,字段2...) values (值1,值2...), (值1,值2...), (值1,值2...)...
写法四:insert into 表名 (name,age,sex) select name, age, sex from stars; #从其他地方获取数据来添加
2、update 【不能缺少where条件】

update 表名 set 字段1=值1,字段2=值2 where 条件;

3、delete 【不能缺少where条件】

delete from 表名 where 条件;      #自增主键不会重新开始
truncate table 表名;	#清空,自增主键从1开始**慎重操作

数据查询
【结伴结构】select [字段1,字段2…] from 表名;
1、基础查询

select username,password from user;
select username as un,password as pd from user;
select * from user;		#一般不建议使用
select 2018,username,password from user;	#常亮可以有
select distinct username from user;	#去重

2、排序

order by        #asc(升序)    desc(降序)

3、限制

limit n	#取前几条
limit offset,n	#从offset条开始取,取n条

4、聚合函数
【注意:聚合函数不能直接使用在where后面的条件中,但可以在子查询中,后边介绍有子查询】

count	max		min		avg		sum

5、分组

group by

小结:

整体排序不能颠倒 []表示可选

select 字段名 from 表名 [where 条件] [group by] [having] [order by] [limit];

查询中的关键词主要有6个:

#书写顺序:select-from-where-group by-having-order by
#执行顺序:from-where-group by-having-select-order by
#多表查询执行顺序为:from-join-on-where-group by-聚合函数-having-select-order by

高级:

1、子查询

----子查询嵌入到其他查询语句中查询语句,子查询只能出现在from,where中
----子查询不要用select * ,exists除外

select username from user where id in (select id from class where name='张三');

多表链接必须要有链接的条件

两种写法:隐式连接和显示连接
隐式(标准)连接,连接写到where语句中。

#隐式内连接: select * from A 别名1,B 别名2 where 别名1.xx=别名2.xx;
#显示内连接: select * from A 别名1 [inner] join B 别名2 on 别名1.xx=别名2.xx;

内连接和外链接

左外连接: select * from A left outer join B on条件;
右外连接: select * from A right out join B on 条件;

左外连接就是左边的表的内容全部显示,然后匹配右边的表,如果右边的表匹配不到,则空。
右外连接就是右边的表的内容全部显示,然后匹配左边的表,如果左边的表匹配不到,则空。

总结:

内连接就是两个表的交集
左外连接就是左边表加两表交集
右外连接就是右边表加两表交集

sql语句中可以使用case代替if 语句

case
	when A then "A"
	when B then "B"
else "Q" end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值