服务器端 | 客户端 |
sudo /etc/init.d/mysql start | mysql -h主机地址 -u用户名 -p密码 |
sudo /etc/init.d/mysql stop | mysql -uroot -p123456(服务器在本机上) |
sudo /etc/init.d/mysql restart |
|
sudo /etc/init.d/mysql reload |
|
sudo /etc/init.d/mysql status |
|
如何更改默认字符集
1、方法
通过更改MySQL配置文件实现
2、步骤
1、获取root权限
sudo -i
2、备份配置文件
cd /etc/mysql/mysql.conf.d/
cp mysqld.cnf mysqld.cnf.bak
3、更改mysqld.cnf配置
subl mysqld.cnf
[mysqld]
character_set_server=utf8
4、重启mysql服务
sudo /etc/init.d/mysql restart
命令 | 库 | 表 |
创建 | create database 库名[character set utf8]; | create table 表名( 字段名1 数据类型, 字段名2 数据类型, 字段名3 数据类型)[character set utf8] |
查看已有的(库/表) | show databases; | show tables; |
查看创建(库/表)的语句(字符集) | show create database 库名; | show create table 表名; |
查看当前所有(库) | select database(); |
|
切换(库) | use 库名; |
|
查看(库/表)中已有的(表/结构) | show tables; | desc 表名; |
删除(库/表) | drop database 库名; | drop table 表名; |
表内分类 | 命令 | 命令代码 |
增 | 插入记录 | insert into 表名 values(值1,值2,值3),(值1,值2,值3) |
insert into 表名 (字段名1,字段名2) values(值1,值2),(值1,值2) | ||
添加字段(add) | alter table 表名 add 字段名 数据类型; | |
alter table 表名 add 字段名 数据类型 first; | ||
alter table 表名 add 字段名 数据类型 after 字段名; | ||
删 | 删除字段(drop) | alter table 表名 drop 字段名; |
删除表记录(delete) | delete from 表名 where 条件; | |
改 | 修改字段数据类型(modify) | alter table 表名 modify 字段名 新数据类型; |
修改字段名(change) | alter table 表名 change 原字段名 新字段名 数据类型; | |
更新表记录(update) | update 表名 set 字段1=值1,字段2=值2 where 条件; | |
查 | 查询表记录 | select * from 表名[where 条件] |
select 字段名1,字段名2 from 表名 [where 条件] |
比较方式 | 命令 | 备注 |
数值比较运算符 | =、!=、>、>=、<、<= |
|
字符比较运算符 | =、!= |
|
逻辑比较 | and、or |
|
范围内比较 | where 字段名 between 值1 and 值2 # 值为数字 |
|
where 字段名 in(值1,值2,...) |
| |
where 字段名 not in(值1,值2,...) |
| |
匹配空 | is null |
|
匹配非空 | is not null |
|
模糊比较 | where 字段名 like 表达式 | 表达式: 1、_:匹配单个字符 2、%:匹配0到多个字符 |
SQL查询
3、select ...聚合函数 from 表名
1、where ...#筛选(表内字段筛选)
2、group by ...#分组=
4、having ...#进一步筛选(聚合函数筛选)
5、order by ...#排序
6、limit ...#再进一步筛选(显示数量筛选)
约束:
默认约束 | 字段名 数据类型 default 值 | 插入记录时,不给该字段赋值,使用默认值 |
非空约束 | 字段名 数据类型 not null | 不允许该字段的值有NULL记录 |
索引类型 | 创建索引 (创建表) | 创建索引(已有表) | 查看 | 删除 | 规则 |
普通索引(index) | index(字段名) | create index 索引名 on 表名(字段名) | desc 表名; show index from 表名; | drop index索引名 on 表名; | 1、一张表中可以有多个index字段 2、约束:字段的值可以重复,且可以为NULL 3、经常把做查询条件的字段设置为index字 4、index字段的key标志为 MUL |
唯一索引(unique) | unique(字段名) | create unique index 索引名 on 表名(字段名) | 查看删除同普通索引 Non_unique:0------>唯一索引 Non_unique:1------>普通索引 | 1、一个表中可以有多个unique字段 2、约束:对应字段的值不允许重复,但可以为NULL 3、key标志:UNI | |
主键(primary key) | create table t1( id int primary key auto_increment, ……) 指定自增长属性起始值: create table t1( id int primary key auto_increment, ……)auto_increment=1000; | alter table 表名 add primary key(字段名)
指定自增长起始值: alter table 表名 auto_increment=值 |
1、删除自增长属性 alter table 表名 modify id int; 2、删除主键 alter table 表名 drop primary key; | 附加属性 自增藏属性:auto_increment 1、一个表中只能有一个主键字段 2、约束:对应字段值不允许重复,且不能为NULL 3、key标志:PRI 4、把表中能够唯一标识一条记录的字段设置为主键,通常把记录编号的字段设置为主键 | |
外键(foreign key) | foreign key(参考字段名) references 主表(被参考字段名) on delete 级联动作 on update 级联动作 | alter table 从表 add foreign key(参考字段名) references 主表(被参考字段名) on delete 级联动作 on update 级联动作 | 外键名查看方式:show create table 从表名; alter table 从表 drop foreign key 外键名; | 让当前表的字段值从另一个表的范围内选择 1、两张表参考字段和被参考字段数据类型要一致 2、被参考字段必须为key的一种,通常primary key 级联动作: 1、cascade : 主表删除记录或更改被参考字段值,从表级联更新 2、restrict(默认): 从表中有相关联记录,不允许主表操作 3、set null: 主表删除或更改被参考字段值,从表中相关联记录字段值设置为 NULL 4、no action: 同 restrict,都是立即检查外键限制 |
数据导入导出:
数据导入 | 数据导出 |
load data infile '文件名' into table 表名 fileds terminated by '分隔符' ['tɝmə,net] lines terminated by '\n' | select ... from 表名 into outfile '文件名' fields terminated by '分隔符' lines terminated by '\n' |
表的复制 | 表的结构复制 |
create table 表名 select ... from 表名; | create table 表名 select * from 表名 where false; |
嵌套查询:
select * from 表名 where 字段名 比较符(查询语句)
连接查询
连接方式 | 语法 | 备注 | 示例 | |
多表查询 | select 字段列表 from 表列表 where 条件 | 只显示查到的信息 | select sheng.s_name,city.c_name,xian.x_name from sheng,city,xian where sheng.s_id = city.cfather_id and city.c_id=xian.xfather_id; | |
内连接 | select ... from 表1 inner join 表2 on 条件 | 只显示查到的信息 | select s.s_name,c.c_name,x.x_name from sheng as s inner join city as c on s.s_id=c.cfather_id inner join xian as x on c.c_id=x.xfather_id; | |
外连接 | 左连接 | select ...from 表1 left join 表2 on 条件 | 以左表为主显示查询结果 | select s.s_name,c.c_name,x.x_name from sheng as s left join city as c on s.s_id=c.cfather_id left join xian as x on c.c_id = x.xfather_id |
右连接 | select ...from 表1 right join 表2 on 条件 | 以右表为主显示查询结果 | select s.s_name,c.c_name,x.x_name from sheng as s right join city as c on s.s_id=c.cfather_id left join xian as x on c.c_id = x.xfather_id |
数据备份(mysqldump,在linux终端操作)
| 命令 | 示例 | 说明 |
数据备份(mysqldump,在linux终端操作) | mysqldump -u用户名 -p 原库名 >路径/***.sql | mysqldump -uroot -p db4 > ~/mydata/db4.sql | 源库名的表示方式: --all-databases 备份所有库 库名 备份单个库 -B 库1 库2 ... 备份多个库 库名 表1 表2 ... 备份指定库的指定表 |
数据恢复 | mysql -u用户名 -p 目标库名 <路径/***.aql | 1、先创建空库 create database db4 character set utf8; 2、恢复 mysql -uroot -p db4 < db4.sql | 从所有库的备份中恢复某一个库(--one-database) mysql -u用户名 -p --one-database 目标库名 < all.sql 注意:恢复库时,原库中表中记录会被覆盖,新增的表不会删除 |
import pymysql
db = pymysql.connect("localhost","root","123456")
cursor = db.cursor()
cursor.execute("create database indexdb;")
cursor.execute("use indexdb;")
cursor.execute("create table t1(id int,name char(20));")
n = 1
name="lucy"
while n <= 2000000:
cursor.execute("insert into t1 values('%s','%s')" % (n,name+str(n)))
# n = int(n)
n += 1
db.commit()
cursor.close()#关闭游标
db.close()#断开数据库连接