mysql语句总结

-- 1,通过windows提供的服务管理来完成

-- services.msc

-- 2,dos下的命令来完成

-- 停止

-- net stop mysql

-- 启动

-- net start mysql

-- mysql的bin目录下的mysqldLauren完成

--mysql.exe  mysql的客户端软件

--mysqld.exe mysql的服务器软件

--dos下

--mysqld.exe --defaults-file="D:\amp\mysql\my.ini"

header(‘Content-Type:text/html;Charset=utf-8’);

------------------------------------

select * from it_goods \G;5

数据库的3码合一

set names gbk;(有时要先声明后才创建语句)

1,mysql -uroot -p 回车

密码

2,use php2016;

3,set names gbk;

-----------------------------------------

DDL:Data Definition Language,数据定义语言

DML:Data Manipulation Language,数据操作语言

DCL:Data Control Language,数据控制语言

-------------------------------------------

SQL:结构化查询语言 又分成三种:DDL  DML(DQL) DCL

非关系型数据库

Mysql的软件架构:C/S模型,先要开启Mysql服务器,windows服务管理的方式,cmd(net stop,net start)mysqld

数据库的操作

创建数据库  create database 数据库名;

删除:drop database 数据库名;

修改:数据库的选项  charset utf8|gbk|其他字符集

查看:show  databases  查看当前系统中有哪些数据库

show  create  database 数据库名 查看数据库的创建语句

show global variables like 'port';查看数据库端口

数据表的操作-----------------

一般要先选择默认的操作数据库:use 数据库名

或者每次操作的时候都显示的声明:库名.表名

创建数据表:create table 数据表名(字段名1  字段类型1……,);

删除数据表:drop table 表名;

-- 删除数据,清空

truncate 表名;

查看数据表:show  tables;查看当前数据库中有哪些数据表

show create table 数据表名;查看当前数据表的创建语句

desc 表名;查看数据表的结构

修改数据表------------------------------:

修改表名:rename table 旧表名 to 新表名;可以用rename语句将一个数据库的中表移动到另外一个数据库中

rename table 旧表名 to 数据库.新表名

修改列定义:前面都是alter table 表名

增加一列:add 字段名 字段类型; //add primary key(id)

删除一列:drop 字段名;自增长的必须先改了才能删,先去

自增长的不能直接删除,要先去除主键,获取直接删除name名

修改字段类型:modify 字段名 字段新类型

重命名字段:change 旧字段名 新字段名 新字段名的类型

最后alter 哪个字段后面

------------------------------------------------------

数据操作(CURD)

插入数据 insert  into  表名(字段列表|或者省略) values (值列表1),(值列表2);

删除数据 delete from 表名 where删除条件

查询数据 select  *|字段列表 from 表名 where查询条件

修改数据 update 表名 set 字段1=值1,字段2=值2  where修改条件

加上if exists 之后,及时删除一个不存在的表,即不执行也不会报错!

show engines\G查看mysql的引擎

show index from 表\G   查看索引

select database();   查看当前所处数据库

mysql -h192.168.16.120 -uroot -pzhouyang  不要分号

http://192.168.16.102/php17/index.php

---------------------------------------------------------

unsigned           无符号的

primary key        设置主键,在每一个表中唯一标签一条

auto_increment 自增长

not null     设置不得为空

default     设置缺省值

unique             唯一键约束!

comment '教师的姓名',

key  单纯是主键,key()是普通索引。要先创建后,才能添加

----------------------------------------------------------

insert into admin select id,username from tp_user;  (填充)

数值型--------------------------------------------------------------------

1整数型      tinyint 1 smallint 2  mediumint  3 int/integer 4  bigint 8

2小数型  浮点型 float 4 Double 8

定点型 decimal

字符串------------------------------------------------------------------

set     enum     blob     text    varchar   char

日期时间类型-------------------------------------------------------------

year timestamp time data datetime

-------------------------------

1. 按照key分区

create table t1(

id int not null auto_increment primary key,

name varchar(10)

) engine=myisam default charset=utf8

partition by  key(id) patitions 5;

-------------------------------

select [select 选项] *|字段列表[as 字段别名]

from数据源[where 子句][group by 子句][having 子句][order by 子句][limit 子句];

-------------------------------

联合查询union

关键字using 关键字(所有字段名相同的列表)

还有交叉  各种内外  左  右,自然外   左 右  连接

笛卡尔积!

---------------------------------

--子查询

标量查询--一个值。

列查询 --用来匹配

行子查询 ---比较时,必须构造一个行元素

表子查询==往往作为数据源。

exists子查询   返回布尔值

-------------------------------

数据备份

select *|字段列表 into outfie 文件地址 from 表名

select * into outfile 'd:/backup/php_student.txt' from php_student;

首先要把文件目录创建好

还原语法

load data infile 文件路径 into table 表名[字段列表]

load data infile  'd:/backup/php_student.txt' into table php_student;

----------------------------------------

--------------------------------

-- 第3讲

--创建数据库---------------------------

create database php2016;

create database `123`;

create database `#@$%*()`;

set names gbk;

-- 删除数据库

-- drop database 数据库名;

drop database  `传智播客`;

-- 修改数据库

-- alter database 数据库名[库选项]

alter database  php2016 charset gbk;

-- 查看数据库

show databases;

-- 查看数据库的创建语句

show create database 数据库名;

-- 第4讲

-- 1,显示的指定数据库

desc database.user;

-- 2,隐式的指定数据库

use php2016;

desc php17;

--前期操作数据库的三步曲:

1,mysql -uroot -p 回车

密码

2,use php2016;

3,set names gbk;

-- 创建数据表---------------------------

create table 表名(字段名1, 字段1类型[,字段名2 字段类型2类型])[表选项]

create table test1(

a int,

b float,

c varchar(20)

);

-- 表前缀

-- 学生管理系统

create table info_student(

stu_name varchar(20),

stu_no varchar(20),

stu_age int

);

-- 在线答题系统

create table exam_student(

stu_name varchar(20);

stu_no varchar(20);

stu_score float

-- 查看当前数据库下的所有表

show tables;

-- 查看以php开头的数据库

show databases like 'php%';

-- 查看某个应用的表

show tables like 'php%';

--查看表的创建语句

-- 用\G代替分号

show create table info_student;

show create table info_student\G

-- 查看表的结构

desc exam_student;

-- 删除数据表  删除一个不存在的表,会报错!

drop table  test1;

-- 加上if exists 之后,及时删除一个不存在的表,即不执行也不会报错!-------------

drop table if exists test1;

-- header(‘Content-Type:text/html;Charset=utf-8’);

-- 完整的形式 if exists  存在则。if no exists 不存在则

create database if not exists php2016;

drop database if exists php2017;

create table if no exists info_student;

drop table if exists test1;

-- 修改表名

--- rename table 旧表名 to 新表名;

rename table exam_student to student;

-- 利用rename将一个数据库中的表移动到另一个的数据库

--- rename table 旧表名 to 数据库.新表名;

rename table student to test2.student;

-- 增加一列

alter table info_student add stu_add varchar(100);

-- 删除一列

alter table info_student drop stu_add;

--修改字段类型

alter table info_student modify stu_no int;

-- 重命名字段

alter table info_student change stu_no stu_num varchar(20);

--修改列定义

--修改表选项就是修改表的字符集和存储引擎

alter table info_student charset utf8;

-- 第五讲 数据操作

-- inset into 表名(字段列表) values(值列表);

-- 插入记录

inset into info_studen values('路飞',1234,23);

-- 一次插入多条记录

insert into info_student values('索罗',1234,24),('娜美',1234,24);

insert into info_student values(('索罗',1234,24),('娜美',1234,24));

-- 只插入部分字段

insert into info_student (stu_name,stu_age) values('鸣人',30);

-- 查看表的数据

select */字段列表 from 表名[查询条件];

select * from info_student;

select * from info_student where 1;

select * from info_student where stu_age > 20;

-- 删除数据

-- delete from 表名[删除条件];

-- 在实际的应用中,删除条件是必须的,如果没有删除条件,相当于删除了整个数据表的全部数据

delete from info_student where stu_age = 20;

-- 修改数据库

update 表名 set 字段1=新值1,字段2=新值2,...[修改条件];

-- 修改数据库

update info_student set stu_age = stu_age + 1;

-- 第7讲,校队规则

%和?和*功能一样

show collation;查询当前系统中所的校对规则

show collation like 'utf8%';

常见的:

ci结尾的:不区分大小写

cs结尾的:区分大小写

bin结尾的:二进制编码进行比较

utf8不支持中文的比较,但是gbk支持(比较拼音)

create database PHP017 charset utf8 collation utf8_bin;

--第8讲

create table tb1_int(

age tinyint unsigned,

score tinyint

);

insert into tb1_int values(255,127);

insert into tb1_int values(255,128);--超出范围报错

--可以指定数据默认的宽度,如果不足4位,前面以0填充

alter table tb1_int add num tinyint(3) zerofill;

insert into tb1_int(num) values(12),(123),(1234),(12345);

alter table tb1_int add c bool;

desc tb1_int;

-- 浮点数

create table xiaoshu(

a float,

b double

);

insert into xiaoshu values(123456789.123456789,123456789.123456789);

--设定数据的范围

create table xiaoshu1(

a float(6,2),

b double(7,3)

);

-- 科学计数法

insert into xiaoshu1 values(0,1234E3,-45.678E-4);

-- 定点数 --decimal(M,D)默认的M是10,D为2!M代表总位数,D代表的是小数位数

create table xiaoshu2(

money decimal(40,10)

);

insert into xiaoshu2 values(123456789123.123);

第9讲 日期时间类型

create table datetime1(

a datetime,

b timestamp

)

inset into datetime1 values('2016-04-03 11:25:45',12345678)--错误

inset into datetime1 values('2016-04-03 11:25:45','2015-05-14 15:45:20')

--下面的b字段就超出了范围

inset into datetime1 values('2016-04-03 11:25:45','2038-05-14 15:45:20')

-- 这样也可以

inset into datetime1 values('2016-4-3 11:25:45','2015-5-14 15:45:20')

inset into datetime1 values('20160403112545','20150514154520')

inset into datetime1 values(now(),'20150514154520')

总结:用什么分隔符不重要,日期的范围和规范才重要

-- time型

create table datetime2(

jiange time

);

inset into datetime2 values ('5 12:54:50');

create table datetime4(

nian year

);

insert into datetime4 values(1988);

insert into datetime4 values(80);-- 可以省略,说明:如果只写2位:大于70的表示是19XX年 小于或等于69的表示20XX年

第10讲  字符串类型

create table char1(

a varchar(30000);

); -- 超出了范围,因为UTF8占了3字节。

create table char1(

a varchar(30000);

)charset gbk;

create table char2(

a varchar(21844);

);

-- enum类型

create table enum1(

gender enum('female','male') -- 插入值必须是默认的值

);

insert into enum1 values ('male');  --只能插入一个值

insert into enum1 values ('fmale');

insert into enum1 values ('secret');

create table enum1(

gender enum('female','male','secret') -- 其实在数据库里面存的是整数,

);

select gender+0 from enum2;  -- 这样就能看到数据库里面存储的数据是什么,开始是1,2.。最多能保存65535个选项,

-- 用一个或者两个字节就能保存

-- set类型

create table set1(

hobby set('football','basketball','pingpang','doudizhou','cs','war3','wow')

);                1         2             4         8         16    32     64

insert into set1 values ('doudizhou,football,war3');

select hobby+0 from set1;

11讲 列属性

unsigned           无符号的

primary key        设置主键,在每一个表中唯一标签一条

auto_increment 自增长

not null     设置不得为空

default     设置缺省值

unique             唯一键约束!

key  单纯是主键,key()是普通索引。要先创建后,才能添加

-- not null 属性

create  table yueshu1(

a int,

b int not null

);

insert into yueshu1(b) values(123);

-- default 属性

create  table yueshu2(

a int,

b int not null default 100

);

insert into yueshu2(a) values(123);

insert into yueshu2 values(123,234);

insert into yueshu2 values(123,default);

-- primary key主键

-- 第一种

create table pk1(

id int unsigned primary key,

name varchar(20),

class_name  varchar(20),

day_num tinyint unsigned

);

-- 第二种 如果要定义组合主键的话,就必须采用第二种方式

create table pk1(

id int unsigned,

name varchar(20),

class_name  varchar(20),

day_num tinyint unsigned

primary key(id)

);

-- 组合主键

create table pk1(

name varchar(20),

class_name  varchar(20),

day_num tinyint unsigned

primary key(name,class_name) -- 设置组合主键

);

-- 定义为主键后,该字段就不可能重复

insert into pk1 values (1,'李四','0428',17);

insert into pk1 values (2,'张三','0428',20);

insert into pk1 values (null,'李四','0428',17);-- 出错,主键不能为空

-- unique属性 唯一键约束

create table unique1(

a int unique,

b int primary key,

c int unique key

);

insert into unique1 values (null,100,200);

insert into unique1 values (123,101,201);

insert into unique1 values (123,102,202)-- 出错,123重复

-- auto_increment自动增长属性 约束

create table auto1(

id int primary key auto_increment, -- 自动增长

name varchar(20),

class_name  varchar(20),

day_num tinyint unsigned

);

-- 默认从100开始

create table auto2(

id int primary key auto_increment, -- 自动增长

name varchar(20),

class_name  varchar(20),

day_num tinyint unsigned

)auto_increment 100;

insert into auto2 values(null,'张三','0323',17);

select * from auto2;

-- comment注释 建议是注释,方便自己以后认识,不然睡一觉都忘了

create table auto3(

id int primary key auto_increment comment '主键字段,并设置为自动增长',

name varchar(20) comment '教师的姓名',

class_name  varchar(20) comment '班级号码',

day_num tinyint unsigned comment '教学的天数'

)auto_increment 100;

-- 12讲 索引

create table suoyin(

a int,

b int,

c int,

primary key(a), -- 设置了一个主键索引

unque key(b),   -- 设置了一个唯一键索引

key(c)     -- 设置了一个普通索引

);

-- 当然,索引在提高查找速度的同时,也降低了增删改的速度,因为内部机制要重新排序!

--14讲  外键

-- 直接在创建的时候,外键和级联

create table pk2(id int, foreign key(id)

references pk1(id) on update cascade on delete cascade);

-- foreign key (外键字段) references 父表名(父表的关联字段)

--先定义父表

create table itcast_class(

class_id int primary key auto_increment,

class_name varchar(20) not null default 'itcast-php'

);

-- 在定义子表

create table itcast_student(

stu_id int primary key auto_increment,

stu_name varchar(20),

class_id int,

foreign key (class_id) references itcast_class(class_id),-- foreign key (外键字段) references 父表名(父表的关联字段)

);

-- 如果先插入子表数据吗,会报错

insert into itcast_student values (null,'鸣人',1);

-- 应该先插入父表数据

insert into itcast_class values(null,'php2016');

-- 再插入子表

insert into itcast_student values (null,'鸣人',1);

--因为创建外键时,系统自动给了外键名,通过show create table 表名,可以看到

--要先删除外键名

--语法:

alter table 表名 drop foreign key 外键名;

--其中外键名是创建外键的时候系统自动设定的,可以通过数据表的创建语句进行查看!

-- 现在,再给itcast_student 增加外键

alter table 表名 add foreign key(外键定义) references 主表(字段名);

--增加外键和级联操作

alter table 表名 add foreign key(外键定义) references 主表(字段名)  on update cascade|set null|restrict on delete cascade|set null|restrict;

cascade|set null|restrict

同步|   设置为空|  拒绝父表的更新或者删除

alter table itcast_student add foreign key(class_id)

references itcast_class(class_id) on update cascade;-- 当更新时,同步更新

第15讲 存储引擎

create table engine1(

a int,

b float

)engine Myisam; -- 存储引擎

17讲 其他数据操作

create table ruchong1(

a int

);

insert into ruchong1 values(1),(2),(3);

蠕虫复制

-- 蠕虫复制

insert into ruchong1 select * from ruchong1;

create table ruchong2(

b int

);

-- 表复制

insert into ruchong2 select * from ruchong1;

-- 主键重复的时候-

create table rekey(

id int primary key auto_increment,

name varchar(20),

home varchar(40)

);

insert into rekey values(null,'鸣人','木叶');

insert into rekey values(1,'小樱','木叶');-- 主键插入失败

-- 使用如下的语法

insert into 表名[字段列表] values (值列表) on duplicate key update name = ''

-- 如果主键存在,就修改原主键内容,

-- 1即使主键已经存在,也要插入成功!

insert into rekey values (1,'小樱','木叶') on duplicate key update name = '小樱',home='木叶';

--2如果主键冲突,主键删除,原纪录再插入

replace  into 表名[字段列表] values(值列表)

replace into rekey values(2,'飞段','晓');

replace into rekey values(2,'鼬','晓');

-- limit子句

update 表名 set 字段1=值1,字段2=值2 where 条件 limit 数据量;

delete from 表名 where 删除条件 limit 数据量;

-- 删除数据,清空

truncate 表名;

---比较完整的查询语句 五子查询

select [select 选项] *|字段列表[as 字段别名]

from数据源[where 子句][group by 子句][having 子句][order by 子句][limit 子句];

条件 分删排显

select选项

是指系统在查询出数据后,要不要去除重复的记录!,有两个选项:

all:也是默认值,保留所有的查询结果!

distinct:

etween and

范围的比较,相当于闭区间!

比如:between A and B意思就是数学上的[a,b] 当然这里的A要小于B

in 和not in

-- 别名

可以的as可以省略,为了增加可读性,强烈建议加上!

第一,如果出现多表查询的时候,往往不同的表会有相同的字段名,如果要使用同名的字段,就必须给其中的一个或者两个起上别名,否则不好区分!

第二,通常都要给一个表达式起一个别名,

create table score(

math float,

chinese float,

english float

);

insert into score values(78,5,87,98);

select math+chinese+english as sum from score;--别名sum

select (math+chinese+english)/3 as avg from score;--别名avg

-- 模拟一下子查询

select * from (select * from score); -- 出错

select * from (select * from score)as score;

select * from where1 where a-2;

mysql运算符

select * from where1 where a in(1,3,5,7,9);

select * from where1 where a>2 and b<60;

select * from where1 where b between 20 and 50;

逻辑运算符

and 或 &&

or 或||

not 或

-- 20讲 group by 子句

create table php_student(

id int primary key auto_increment,

name varchar(20),

gender enum('male','female'),

class_id tinyint unsigned,

age int unsigned,

home varchar(40),

score tinyint unsigned

);

insert into php_student values

(null,'孙悟空','male',17,600,'花果山',98),

(null,'孙悟空1','male',171,6001,'花果山1',91),

(null,'孙悟空2','male',172,6002,'花果山',92);

--分组查询

select * from php_student group by home;

-- 求个数,总数,平均值

select count(*),sum(age),avg(score) from php_student group by home;

select home,count(*),sum(age),avg(score) from php_student group by home;

--一般还会给各个统计函数起一个别名

select home as '家乡分组',

count(*) as '分组个数',

sum(age) as '年龄总和',

avg(score) as '平均值'

from php_student group by home;

create table php_student(

id int primary key auto_increment,

name varchar(20),

gender enum('male','female'),

class_id tinyint unsigned,

age int unsigned,

home varchar(40),

score tinyint unsigned

);

insert into php_student values

(null,'孙悟空','male',17,600,'花果山',98),

(null,'猪悟能','male',17,700,'高老庄',88),

(null,'沙悟净','male',17,750,'流沙河',78),

(null,'唐僧','male',17,30,'东土大唐',100),

(null,'高翠兰','female',16,18,'高老庄',70),

(null,'小猴子','male',16,100,'花果山',95),

(null,'皇帝','male',16,60,'东土大唐',93),

(null,'高翠华','female',16,16,'高老庄',80);

create table php_class(

class_id tinyint unsigned primary key,

class_teacher varchar(20)

);

insert into php_class values

(15,'马浩洋'),

(16,'王金涛'),

(17,'周洋');

-- 多字段分组,分组多了

select home,gender,count(*),sum(age),avg(score) from php_student group by home;

-- 回溯统计 with rollup 就是往上多统计一次

select class_id,count(*) from php_student group by class_id  with rollup;

--分组函数 ,只要有统计函数,就会分组,而不管是不是有group by

sum():求和,将某个组内的某个字段的值全部相加

max():求某个字段的最大值

min():求某个字段的最小值

avg():平均值

count():个数

--21讲 having 筛选

select home as '家乡分组',

count(*) as '分组个数',

sum(age) as '年龄总和',

avg(score) as '平均值'

from php_student group by home having avg(score)>85;

--22讲 onder by子句 根据某个字段进行排序,有升序和降序

-- order by 字段1[asc|desc]

-- 默认是acs 也就是升序,可以省略

select * from php_student order by score desc;

select * from php_student order by score,age desc;--先按score升序排序,如果遇到age相同的,则按降序排序

--22讲 onder by子句

--根据某个字段进行排序,有升序和降序

--order by 字段1[asc|desc]

--默认是acs 也就是升序,可以省略

select * from php_student order by score desc;

-------------------------

--23讲 limit子句

--linit就是限制的意思,所以,limnit子句就是限制查询记录的条数!

--llinit offset,length

--offset指偏移量,可以省略,默认是0

--取3到5的数据

limit 2,3

偏移2条,开始取3条。

如果想取到第n条,到第m条

linit n-1,m-n+1;

select * from php_student limit 2,3;

--------------

-- 24讲,联合查询union

--先查 高老庄中成绩最高的

selec * from php_student where home='高老庄' order by score desc limit 1;

-- 花果山成绩最低的

selec * from php_student where home='花果山' order by score limit 1;

-- 联合

(selec * from php_student where home='高老庄' order by score desc limit 1)

union

(selec * from php_student where home='花果山' order by score limit 1);

-- 注意

1 联合查询中,有order by 就必须加上括号,其他的不用

2 order by 必须搭配 limit才能生效

可以在limit后面加上很多的数

limit 999999999;

(selec * from php_student where home='高老庄' order by score desc limit 999999)

union

(selec * from php_student where home='花果山' order by score limit 999999);

--25讲,交叉连接cross join

select * from pk1 cross join pk2;

-- 内连接  匹配成功的保留

--左边 inner join右表 on 左表。字段名=右表,字段名 【inner可以省略】

select * from pk1 inner join pk2 on pk1.id=pk2.id; --当左右字段名相同,最好是加上表名

select * from pk1 inner join pk2 on pk1_id=pk2_id;--如果不一样,则可以不用加上表名

--使用别名

select * from pk1 as a inner join pk2  as b on a.id=b.id;

select * from pk1 inner join pk2  where pk1_id=pk2_id;

--左外连接 匹配成功保留,不成功,值保留左表的

select * from pk1 left outer join pk2 on pk1.id=pk2.id;

--右外连接`

select * from pk1 right outer join pk2 on pk1.id=pk2.id;

--自然内连接 如果左表右某些字段的值和右表某些字段的值相等,就自动匹配,!!自动

select * from  pk1 natural join pk2;

--自然内连接会自动只保留一个字段名形态的那个字段,并且默认放在第一个字段ll了!

--自然外连接

--又可以分为自然外连接和自然内连接

左表 natural left|right join 右表

--using 关键字(所有字段名相同的列表)

select * from pk1 right outer join pk2 using(id);

--子查询

标量查询--一个值。

列查询 --用来匹配

行子查询 ---比较时,必须构造一个行元素

表子查询==往往作为数据源。

exists子查询   返回布尔值

最高分的那个

1,先得到最高的那个分数值

select max(score) from php_student;  --还有一种复制的,selec * from php_student  order by score desc limit 1

2,然后匹配

select * from php_student where score=(select max(score) from php_student);

--列查询

--先找出所有已经开班的班级id

select class_id from php_class;

--匹配学生

select * from php_student where class_id in(select class_id from php_class;)

还有其他三个运算符

any

some  any和some完全一样

all

--行子查询select * 字段列表 from 表名 where(字段1,字段2。。。)= (行子查询语句);

select * from php_student where (age,score)= (select max(age),max(score) from php_student);

--32讲 表子查询 找出表中每一个家乡分数最低的那个学生

--找出表中每一个家乡分数最低的那个学生

--第一:先对整个表以score字段进行一次排序

select * from php_student order by score;

--第二,对排序后的表以home字段进行分组,也就是group by,因为group by就是取一条记录

select * from (select * from php_student order by score) as s group by home;

--select *.min(score) from php_student group by home;

数据源,应该是张表,是表就应该与别名。不然会报错---------

--33讲 exists子查询

--返回一个布尔值,如果子查询可以返回数据,就认为exists后的表达式为真,否则没返回数据就为假

--

--34讲 php操作mysql

在phpczmysql.php那里

------------------

37讲 数据的备份

所谓的备份,就是将已有的数据复制一份,存放在其他的服务器上!

文本备份

数据备份

sql备份

-

文本备份是最简单的!-----------

不同的存储引擎,又有不同的备份方式!

采用innoDB引擎的文本备份:

先在相应的数据库文件夹备份对应的表结构!,再备份外部的数据和索引文件:ibdatal

由于innoDBibdatal文件包含了所以的innoDB表的数据,所以,每次备份的还原都是针对所有表同步进行的

很不灵活!也很浪费存储空间!

采用Myham存储引擎

相对灵活,因为每一张表都有结构,数据,索引文件。所以

每次的数据表都可以单独备份

数据的备份-------------

也就是只备份一张表的数据部分

不备份表的结果

select *|字段列表 into outfie 文件地址 from 表名

select * into outfile 'd:/backup/php_student.txt' from php_student;

首先要把文件目录创建好

delete * from php_student;

还原语法

load data infile 文件路径 into table 表名[字段列表]

load data infile  'd:/backup/php_student.txt' into table php_student;

-------------

sql备份

将表的结构和数据一起备份!

备份的语法是:

mysqldump.exe -hPup 数据库名[数据库表1 数据表2]>存储的路径

mysqldump -uroot -p php2016 php_student > d:/backuo/1.sql 不要分号

输入密码

注意:如果只写数据库名,就是将整个数据库备份

drop table php_student

还原

有两种

第一种

mysql.exe -hPup 数据库名 < 备份路径

mysql -uroot -p php2016 < d:/backup/1.sql;

第二种

使用sql命令 source 备份路径

优点:可以备份表结构

缺点:并整表都进行备份,耗时比较长,备份也比较大

现实中,一般都采用sql备份!,采用的策略是n天轮流备份!

比如:只备份最近7天的!

到第8天,删除第一天的,第9天,删除第2天的。。。。。

一般最近一个月

---------------------------------

第38讲 用户的权限管理

目前为止。默认使用的都是root用户,超级管理员,拥有最高的全部的权限

但是一个大的项目通常都是有很多项目小组共同实现,也就是一个数据库服务器,上面

可以运行多个项目的数据库!

密码是sha1加密,老版本是md5加密

create user 用户名【@主机地址】 identified by '密码

用户名:用户登录的名字(比如以前的是root)

主机地址运行用户登录的ip地址,不选的话,就是没有限制。就是所有的ip

比如我们当前的局域网是:

用户名@'192.168,1,1

只给本机访问

create user 'user1'@localhost identified by 'wujunbin';

局域网访问

create user 'user2'@'192.168.16.%' identified by 'wujunbin';

全网访问

create user 'user3' identified by 'wujunbin';

mysql -h192.168.16.120 -uroot -pzhouyang  不要分号

分配权限---

grant 权限列表 on 数据库.数据表 to 用户(用户名@主机地址)

grant select on php2016.php_student to 'user2'@''192.168.16.%;

回收权限

revoke select on php2016.php_student from 'user2'@''192.168.16.%;

删除用户

drop user 'user2'@'192.168.16.%';

root密码修改

mysqladmin -uroot -pwujunbin password

new password:新密码;

------------------------------

-- 同个项目的所有表的,字段,最好也不要起一样,不然,联合时给其取别名

news(id,title,cid)  news_cate(cid,cname)

-- 单表

select * from news where id>10;

-- 两个表联表查询,内连  约束2边

select * from news,news_cate where news.id=news.id and id>10;

select n.id,title,n.cid,c.cid,name from news n inner join  news_cate c  on n.cid=c.cid and id>10;

select news.id,news.title,news.cid,news_cate.cid,news_cate.name from news,news_cate where news.cid=new_cate.cid and id>10;

-- 优化

select n.id,title,n.cid,c.cid,name from news n,news_cate c where n.cid=c.cid and id>10;

-- 左联 只约束右表,左表全出

-- 删除新闻频道,用左连接,不能用内连接,因为,当新闻频道里面没新闻的时候,删不掉新闻频道。

--

delete n,c from news_cate c left join news n on (c.cid=n.cid) where c.id=5;

mysql语句总结的更多相关文章

  1. 如何根据执行计划,判断Mysql语句是否走索引

    如何根据执行计划,判断Mysql语句是否走索引

  2. 让dede运行php代码和mysql语句

    一.dede运行php代码 举例1: {dede:name runphp='yes'} $str = "hello ";@me = $str;@me .= "world& ...

  3. php代码优化,mysql语句优化,面试需要用到的

    首先说个问题,就是这些所谓的优化其实代码标准化的建议,其实真算不上什么正真意义上的优化,还有一点需要指出的为了一丁点的性能优化,甚至在代码上的在一次请求上性能提升万分之一的所谓就去大面积改变代码习惯, ...

  4. mysql语句:批量更新多条记录的不同值[转]

    mysql语句:批量更新多条记录的不同值 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 帮助 1 UPDATE mytable SET myfield = 'value' WHERE ...

  5. Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值

    Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值 Thinkphp 的文档经常不够完整的表达MYSQL的各种组合,is not null在thinkp ...

  6. MySQL语句进行分组后的含有字段拼接方法

    MySQL语句: SELECT GROUP_CONCAT(DISTINCT transaction_no) FROM `lm_wh_trans` GROUP BY staff_code; 如果tran ...

  7. shell脚本循环执行mysql语句

    参考资料:Shell脚本中执行mysql语句 需求:数据库里有张数据表存储的是用户对电影的评价(user_id movie_id rating time),但是我现在要每部电影的总评分. 解决方法: ...

  8. 【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

    一.WampServer2.2设置数据库默认编码(此部分转自http://www.cnsecer.com/5984.html) wamp下MySQL的默认编码是Latin1,不支持中文,要支持中文的话 ...

  9. mysql语句中把string类型字段转datetime类型

    mysql语句中把string类型字段转datetime类型   在mysql里面利用str_to_date()把字符串转换为日期   此处以表h_hotelcontext的Start_time和En ...

  10. php中mysql语句的基本写法

    php中mysql语句的基本写法 php作为一门后台语言必须要与mysql数据库打交道,做到将内容存储到数据库以及数据库数据读写的操作,那么下面就来说下最近学习的一些东西: 在具体将之前先说一下编码的 ...

随机推荐

  1. Linux内核基础--事件通知链(notifier chain)【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...

  2. 在Perl中采用open进行管道操作

    在Perl中采用open进行管道操作 http://blog.sina.com.cn/s/blog_4840fe2a0100b8na.html perl exec管道和子进程 http://blog. ...

  3. 创建数据库表的SQL语句

    创建表.视图.索引的sql语句如下: CREAT TABLE (列名,数据类型,约束) create view(创建视图) create index (创建索引) 1.primary key(主键) ...

  4. 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到Ubuntu 16.04

    .Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...

  5. J2EE MySQL Date数据保持一致解决方案

    1.设置MySQL时区,明确指定 MySQL 数据库的时区,不使用引发误解的 CST show variables like '%time_zone%';set global time_zone = ...

  6. vmware + ubuntu 64 安装 node.js v8.9.3

    第一次使用虚拟机,第一次使用linux系统,第一次安装使用node.js 虚拟机安装不用多说,安装好之后下载ubuntu 64位版本文件 在vm中点击“创建新的虚拟机”,选择下载的ubuntu iso ...

  7. Linux(CentOS)下的JDK的安装和环境配置

    下载对应版本JDK,如jdk-6u45-linux-i586-rpm.bin添加执行权限:#chmod +x jdk-6u45-linux-i586-rpm.bin安装:#./jdk-6u45-lin ...

  8. python 编码处理

    # -*- coding: utf-8 -*-import easygui as gimport sysreload(sys)sys.setdefaultencoding('utf-8')

  9. 从TS流定位H264的每一个视频帧开始,判断出帧类型

    从TS流定位H264的每一个视频帧开始,判断出帧类型(待续)

  10. 【笔试题】Java 易错题精选

    笔试题 Java 易错题精选 1.写出下列程序的运行结果( )String 不变性Java 值传递 public class Test { public static void main(String ...