mysql ------python3(一)笔记

SQL是结构化查询语言,是一种用来操作RDBMS(关系型数据库管理系统)的数据库语言。
当前关系型数据库都支持使用SQL语言进行操作,也就是说可以通过SQL操作oracle,sql server,mysql等关系型数据库。

1、概念
数据库----->文件夹
数据库表------>文件
数据行------>文件中的一行数据
2、初始:
找到MySQL文件下bin目录,输入mysql -u root -p,进入mysql,会提示输入密码
退出则输入exit
如:
D:\program\mysql\MYSQL\bin>mysql -u root -p
Enter password: ******

mysql> show databases;#查看当前mysql都有哪些数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |#mysql---本身构架相关数据
| jingdong           |
| mysql              |#mysql --用户权限相关数据
| performance_schema |
| sys                |
| test               |#test---用于用户测试数据
| test1              |
+--------------------+
7 rows in set (0.48 sec)

数据库级别

mysql> create database food;#创建food数据库
Query OK, 1 row affected (0.75 sec)

mysql> use food;#进入数据库food
Database changed
show databases;#查看当前mysql都有哪些数据库
create database 数据库名;#创建数据库
use 数据库名;#使用选中数据库,进入目录
show tables;#查看当前数据库下都有哪些表
desc 表名;#表下面有哪些列

表级别

数据类型

  • 整型int;

  • 小数decimal:表示浮点数,如decimal(5,2)表示共存5位数,小数占2位.

  • 字符串varchar(m)、char(m):
    varchar是可变长度的字符串,如varchar(3),填充’ab’时会存储’ab’;
    char是固定长度字符串,如char(3),填充’ab’时会存储’ab '(ab后有一个空格);

  • 字符串text用于存储大文本

  • 图片、音频、文本等二进制文件,则是上传到某个服务器上,然后在表中存储这个文件的保存地址。

  • 日期时间date、time、datetime。
    date 2020-01-01
    time 23:59:59
    datetime 2020-01-01 23:59:59
    year 2020

  • 枚举类型enum:(单选)
    把所有可能列举出来,如性别只有2种男、女,星期只有周一到周日7种
    如size enum(‘x-small’,‘small’,‘medium’)

  • 集合(多选)
    set(‘a’,‘b’,‘c’)
    values(‘a,b’,‘d,a’,‘a,d,a’,‘d,a,d’)

show tables;#查看当前数据库中所有的表

创建表

#create table tablename(字段 约束[,字段 约束 类型]);
create table demo1(
    id int,
    name varchar(30)
);

查看表结构

#desc 数据表的名字;
desc demo1;

查看表的创建语句

# show create table 表名字;
show create table students;
  1. 是否为空,null—可空,not null—不可空
  2. 默认值 创建列时可以指定默认值,当插入数据时,如果未主动设置,则自动添加默认值
  3. 自增:如果为某列设置自增列,插入数据时无需设置此列,表中只有一个自增列,必须是数字,必须索引–主键 auto_increment primary key
  4. 主键:一张表只有一个主键(可以多列组成主键),唯一不能重复,不能为空
  5. 外键:foreign key,一对多,两张表建立约束

修改表

创建classes表(id、name):

create table classes(
    id int primary key not null auto_increment,
    name varchar(30)
);

修改表-添加字段

# alter table 表名 add 列名 类型;
alter table students add birthday date;

修改表-修改字段(不重命名)

#alter table 表名 modify 列名 类型及约束;
alter table students modify birthday date default '1990-1-1';

修改表-删除字段

# alter table 表名 drop 列名;
alter table students drop height;

删除表或者数据库

drop table 表名;
drop database 数据库;

1、增:

insert into 表 (列名,列名...) values(值,值...)#插入一条数据
insert into 表(列名,列名...) values(值,值...),(值,值...)...#插入多条数据
insert into 表1(列名,列名...)select 列名,列名,... from2#将表2列数据插入到表1列

2、删:

delete from 表;#表全部删除
delete from 表 where id=1 and name='alex';#表中部分行
#where后面>,>=,<,<=,!=,全都有,and ,not,or

3、改:

update 表 set name='alex'where id>1;
update 表 set age=1;#age整列都为1
update 表 set name='alex'where id>12;#只要id>12,name为alex

4、查:

select * from 表名;
select * from 表名 where id>1;
select nid name gender from 表 where nid>1 and name='kakaka' ;

5、其他
a.条件

select * from 表名 where id>1 and name!='alex' and num=12;
select * from 表名 where id between 5 and 6;#id在5到16所有数字(5,6,7,8...16)
select * from 表名 where id in (11,22,33);#只要id包含(11,22,33)
select * from 表名 where id not in (11,22,33);#只要id不包含(11,22,33)
select * from 表名 where id in(select nid from 表);

b、通配符(模糊搜索)

select * from 表名 where name like 'ale%';#ale开头的所有(%--多个字符串)
select * from 表名 where name like 'ale_';#ale开头的所有(_--一个字符串)

c、分页

select * from 表名 limit 5#前五行
select * from 表名 limit 02#从第1行开始取2行
select * from 表名 limit 45#从第5行开始取5行
select * from 表名 limit 5 offset 4#从第1行开始取2行

d、排序

select * from 表名 order by 列名 ass;#根据列从小到大排序,升序
select * from 表名 order by 列名 desc;#根据列从大到小排序,降序
select * from 表名 order by 列1 desc,列2asc;#根据列1从大到小排序,,如果相同按列2从小到大排序

e、分组

顺序 where    group by    order by
select num from 表名 group by num;
select num,nid from 表名 group by num,nid;
select num,nid from 表名 where nid>10 group by num,id order by nid desc;
select num,nid,count(*),sum(score),min(score),max(score) from 表名 group by num,id ;
select num from 表名 group by num having sum(id)>10#聚合条件进行分组用having

f、组合

自动处理重合
select 列 from A union select 列 from B;
不处理重合
select 列 from A union all select 列 from B;

g、连表操作

#无对应关系
select A.num,b.name from A,B;
select * from A,B;
#连表
select * from A,B where A.x=B.o;
select * from userinfo,part where userinfo.p_nid=part.nid;
#join
select * from A left join B on A.x=B.o;
select * from userinfo left join part on userinfo.p_nid=part.nid;#在后表中找不到,用NULL填补
#
inner join(不会出现NULL)
select * from A inner join B on A.x=B.o;

猜你喜欢

转载自blog.csdn.net/ingenuou_/article/details/104681106