数据库

1. MySQL安装

Windows:
可执行文件
点点点
压缩包
放置任意目录
初始化
服务端:E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --initialize-insecure
# 用户名 root 密码:空
启动服务端:
E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld\mysqld

客户端连接:
E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld\mysql -u root -p

发送指令:
show databases;
create database db1;

环境变量的配置:
E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin
mysqld

windows服务:
E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --install
net start MySQL

E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --remove

net start MySQL
net stop MySQL

2. 关于连接

文件夹【数据库】
文件【表】
数据行【行】
数据行
数据行

连接:

默认:用户root


show databases;

use 数据库名称;

show tables;

select * from 表名;

select name,age,id from 表名;

mysql数据库user表
use mysql;
select user,host from user;


创建用户:
create user 'alex'@'192.168.1.1' identified by '123123';
create user 'alex'@'192.168.1.%' identified by '123123';
create user 'alex'@'%' identified by '123123';
授权:
权限 人

grant select,insert,update on db1.t1 to 'alex'@'%';
grant all privileges on db1.t1 to 'alex'@'%';

revoke all privileges on db1.t1 from 'alex'@'%';

DBA: 用户名密码


3. 学习SQL语句规则

操作文件夹
create database db2;
create database db2 default charset utf8; *****
show databases;
drop database db2;

操作文件
show tables;
create table t1(id int,name char(10)) default charset=utf8;
create table t1(id int,name char(10))engine=innodb default charset=utf8;
create table t3(id int auto_increment,name char(10))engine=innodb default charset=utf8; *****

create table t1(
列名 类型 null,
列名 类型 not null,
列名 类型 not null auto_increment primary key,
id int,
name char(10)
)engine=innodb default charset=utf8;
# innodb 支持事务,原子性操作
# myisam myisam

auto_increment 表示:自增
primary key: 表示 约束(不能重复且不能为空); 加速查找
not null: 是否为空
数据类型:

数字:
tinyint
int
bigint

FLOAT
0.00000100000123000123001230123
DOUBLE
0.00000000000000000000100000123000123001230123
0.00000100000123000000000000000
decimal
0.1

字符串:
char(10) 速度快()
root
root
varchar(10) 节省空间
root
PS: 创建数据表定长列往前放

text

上传文件:
文件存硬盘
db存路径
时间类型
DATETIME

enum
set


create table t1(
id int signed not null auto_increment primary key,
num decimal(10,5),
name char(10)
)engine=innodb default charset=utf8;

清空表:
delete from t1;
truncate table t1;
删除表:
drop table t1;

操作文件中内容
插入数据:
insert into t1(id,name) values(1,'alex');
删除:
delete from t1 where id<6
修改:
update t1 set age=18;
update t1 set age=18 where age=17;
查看数据:
select * from t1;

外键:

create table userinfo(
uid int auto_increment primary key,
name varchar(32),
department_id int,
xx_id int,
constraint fk_user_depar foreign key (department_id) references color(id)
)engine=innodb default charset=utf8;

create table department(
id bigint auto_increment primary key,
title char(15)
)engine=innodb default charset=utf8;
innodb原子操作
今日内容:
0.唯一索引
create table t1(
id int null,
num int,
xx int,
unique uq1 (num,xx) #单列唯一/联合唯一:不能完全相同
)
ps:
唯一:
约束不能重复(可以为空)
ps:主键不能重复(不能为空)
加速查找

1.外键的变种

a.FK() + 唯一

一对一

多对多


2.SQL 语句数据行操作补充
创建表:create table tb12(
id int auto_increment primary key,
name varchar(32),
age int
)engine=innodb default charset=utf8;

insert into tb11(name,age) values('egon',18);

insert into tb11(name,age) values('egon',19),('alex',20);
insert into tb12(name,age) select name,age from tb11; 从tb11中找出添加到tb12中

delete from tb12;
delete from tb12 where id > 2;
truncate table tb12;

update tb12 set name='alex' where id>12 and name='xx;
update tb12 set name='alex',age=19 where id>12 and name='xx;

多的:
select * from tb12;
select id,name,age from tb12;
select id,name,age from tb12 where id > 10 or name='xx';
select id,name as cname,age from tb12 where id > 10 or name='xx';
select name,age,1 from tb12; #加额外的列
ps:不等于 != / <>
其他:
select * from tb12 where id in (1,5,10);
select * from tb12 where id not in (1,5,10);
select * from tb12 where id between 5 and 12;#范围,闭区间

select * from tb12 where name like 'a%'# name 以a开头的
select * from tb12 where name like 'a-'# name 以a开头的两位字母的

select * from tb12 limit 10;取前10条
select * from tb12 limit 0,10;取从0开始的10条

select * from tb12 limit 10 offset 0;取10条从0条


排序:
select * from tb12 ordel by id desc;大到小
select * from tb12 ordel by id asc;小到大

select * from tb12 ordel by id desc limit 10;后10条

分组:
select count(id),max(id),part_id from userinfo5,group by part_id;

count
max
min
sum
avg

如果对于聚合函数结果进行二次筛选时,必须使用having
select count(id),part_id from userinfo5,group by part_id having count(id)>1;

连表操作:
select * from userinfo,department where userinfo.part_id = department.id
-转储SQL文件
命令行:mysqldump -uroot db1>db1.sql -p;备份
命令行:mysqldump -uroot db1<db1.sql -p;导入

python操作mysql
import pymysql
  
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
  
# 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
r = cursor.execute(sql,(args,))
  
result = cursor.fetchone()
  
conn.commit()
cursor.close()
conn.close()

 

 
posted @ 2018-09-05 15:49  月黑风高夜夜欢  阅读(790)  评论(0编辑  收藏  举报