-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMySQL
More file actions
209 lines (174 loc) · 6.75 KB
/
Copy pathMySQL
File metadata and controls
209 lines (174 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
mysql 必知必会
w3school http://www.w3school.com.cn/sql/index.asp
菜鸟教程 http://www.runoob.com/mysql/mysql-tutorial.html
MySQL练习网站 https://leetcode.com/problemset/database/
MySQL的基本管理 https://mp.weixin.qq.com/s/P2d4bpLgKrfR7JzAeJI36A
=
select amount from orders order by amount limit 2, 1;
2代表从结果的第2行开始,不包括第2行;1表示返回1行结果
1265 - Data truncated for column错误
是因为报错的字段的字节个数或者类型不正确
Numeric value out of range: 1264 Out of range value for column
最好解决方法是改变字段类型
https://blog.csdn.net/coreyc/article/details/80281456
https://blog.csdn.net/hanshangzhi/article/details/76177994
# MySQL概述
数据库是保存有组织的数据的容器
mysql软件是DBMS,是数据库管理系统
mysql语言是DBMS系统与数据库通信的语言
=
shell 连接数据库的语法:
mysql -h127.0.0.1 -uroot -proot
使用命令操作mysql,每条语法后面必须携带分号;
使用pymysql操作数据库,当语句中使用in的时候
sql_select = "select post_url from post_list where user_id in %s and post_url is not null"
s = [['134780078','106991820']]
cur.execute(sql_select,(s))
mysql 导入csv表格数据
database-table-import 使用notepad,将文件转成 utf-8 格式
workbench 导入数据多列变成一列
设置seperate
推荐使用python向MySQL导入csv数据
df_post.to_csv('df_post.csv',index=False) 避免生出第一列
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/tianya_scrapy?charset=utf8')
df_post.to_sql(name='df_post', con=engine, if_exists='append',index=False,index_label=False)
1175错误
SET SQL_SAFE_UPDATES = 0
把Preference中的标签由General切换到SQL Queries标签,然后支去掉safe updates前的选项框
大段注释用/* */
将查询结果导出成sql文件。
select * from article limit 150 into outfile '//test//article.sql' 双引号
The MySQL server is running with the --secure-file-priv option。
导出至安全路径 show variables like '%secure%';
使用workbench导出表和表结构
server - dataexport
MySQL server version for the right syntax to use near 。
语法错误
nan not a number
非空,占字符
auto_increment
主键唯一且非空,即使删除字段也不会生成同样的值
Error 1366: Incorrect string value: '\xF0\x9F\x91\xBD\xF0\x9F...' for column 'content'
ALTER TABLE b_law CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci
修改my.ini
https://www.cnblogs.com/shihaiming/p/5855616.html
MySQL中my.ini的位置 promgranData被隐藏了
更改了配置文件my.ini,首先将原始文件存档,然后按照配置文件的描述,将配置文件放在指定的目录下
windows下重启mysql的方法
http://www.jb51.net/article/58890.htm
Mysql 服务无法启动 服务没有报告任何错误
https://www.cnblogs.com/wangjunyan/p/5183366.html
Mysql 服务连接本地服务器 Access denied for user 'root'@'localhost' (using password: YES)'
https://blog.csdn.net/qingyuanluofeng/article/details/48679903
自动字段添加时间
alter table b_law add column create_time datetime NOT NULL DEFAULT NOW()
自动字段修改时间
ADD COLUMN `UpdateTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间'
https://www.cnblogs.com/lhj588/p/4245719.html
mysql b-tree是索引
索引介绍 https://blog.csdn.net/ithomer/article/details/6589651
Error Code: 2013. Lost connection to MySQL server during query
【Edit】->【Preference】->【SQL Editor】将下图DBMS connection read time out (in seconds)适当调大:
1110 specified twice
某个column被使用了两次 mysql大写会转为小写
ERROR 1205: Lock wait timeout exceeded
https://blog.csdn.net/wp1603710463/article/details/51721894
=
# 操作数据库
新建数据库
create database db
指定数据库
use db
查询服务器下有哪些数据库
show databases
修改数据库名称
CREATE DATABASE new_db_name;
RENAME TABLE db_name.table1 TO new_db_name.table1;
DROP DATABASE db_name;
删除数据库
drop database test2 数据库
=
# 表和行操作
新建表格
create table orders
(
id_o3 int(11) auto_increment ,
ordersno varchar(30),
id_p int(11),
birthday date default '2017-5-12',
CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)
constraint FK_sid foreign key (id_p) references persons(id_p)
)
ENGINE = MyISAM DEFAULT CHARSET = gbk
查询数据库下有哪些表格
show tables
查看表的搜索引擎
show create table orders
查询表格下有哪些列
show columns from orders
修改表格名称
alter table orders2 rename orders3
删除表
drop table orders3
复制表
create table orders2 select * from orders
http://www.cnblogs.com/zhangjpn/p/6231662.html
插入列
alter table orders add column items varchar(45)
修改列名称及属性
alter table orders change column items item varchar(20)
删除列
alter table orders drop column item
=
# 行的操作
插入行数据
insert into orders(items) values('knife')
插入多行数据
insert into persons values(2, 'Bush', 'BushGeorge', 'Fifth Avenue', 'New York'),
(3, 'Carter', 'Thomas', 'Changan Street', 'Beijing')
更新行数据
update orders set orderno= '100.' where id_o=1
删除行
delete from orders where id_p=2
查询所有
select * from orders
查询指定列
select id_p from orders
删除重复值
select distinct id_p from orders
删除空格查询,分trim,ltrim和rtrim
select ltrim(id_p) as 'all' from orders
合并列
select concat(id_p, ' yes ', OrderNo) as 'all' from orders
取某几个字符,right(),left()
select left(lastname,3) as ll from persons
取中间第几个字符,取几个
select substr(lastname,3,2) as ll from persons
取长度
select length(id_p) from orders
取某个字符位置
select locate('e', lastname) from persons
替换
select replace(lastname, 'a', 'b') as count_lastname from persons
查询时间函数,类似函数 now,date,datediff
返回当前日期
select now() as d from orders
select date(now()) as d from orders
查询相隔天数,前面减去后面
select datediff(date(now()), '2017-5-2') as d from orders
查询计算函数,类似函数 count,avg,min,max,sum
计数
select count(id_p) from persons
求和
select sum(orderno) from orders
排名筛选
select Salary as SecondHighestSalary FROM Employee order by Salary desc limit 1,1
两表联和能解决很多查询的问题 a 没有冒号
SELECT * FROM Employee AS a, Employee AS b 总共有 a 个数乘以 b 个数个结果
SELECT p1.* FROM Persons as p1, Persons as p2 总共有 a 个数乘以 b 个数个结果 省略显示横向,变换而已
数据存在与表1而不在表二
select id from t1 where id not in (select id from t2)
union
select id from t2 where id not in (select id from t1)