mysql基础

mysql基础总结:

1.什么是数据库?数据库database就是一个存储数据的仓库

2.sql:结构化查询语言,数据库管理系统通过sql语言来管理数据库中的数据

sql:分为DDL数据定义语言、DML数据操作语言、DCL数据控制语言

DDL:CREATEALTERDROP

DML:updateinsertdeleteselect

DCL:用来设置或者更改数据库用户或者角色权限的语句,grant,deny,revoke

3.mysql:是一个关系型数据库管理系统

(1)安装:liunx中使用yum安装

(2)登录:mysql -h host -P 3306 -uuser -ppassword

(3)windows中,使用navicat、sqlyag等工具连接,输入主机名或者ip地址、端口、用户名、密码

4.数据类型:整数类型、浮点数、定点数、日期和时间类型、字符串类型、二进制数据尅性

intfloat(double)decimaldate imechar(定长字符串类型)varchar(变成字符串类型) ext(文本类型)inary

5.linux输入命令

显示所有数据库:show database;

选择默认数据库:use dbname;

显示默认数据库中的所有表:show tables;

放弃正在输入的命令:c;显示命令清单:h;退出mysql程序:q;查看mysql服务器状态信息:s

5.创建数据库:create database 数据库名 charset=utf8(设置语言)

 显示数据库结构:show create database 数据库名;

6.删除数据库:drop database 数据库名

7.innoDB(行锁)和mysiam(表锁)和memory:存储引擎,第一种是查询少的时候使用,而第二种相反

指定表的存储引擎:create table tmp()ENGINE=MyISAM;

8.修改表:alter table:包括修改表名、字段数据类型、字段名、增加字段、删除字段、修改字段的排列位置、修改默认存储引擎、删除表的外键约束等

(1)alter table 旧表名 rename 新表名;

(2)alter table 表名 modify 属性名(列) 数据类型;

    alter table 表名 change 旧属性名 新属性名 新数据类型;

(3)alter table 表名 add 属性名1 数据类型 [完整性约束,例如auto_increment必须有primary key一起]

(4)alter table 表名 drop foreign key 外键别名;

(5)alter table 表名 modify 属性名1 数据类型 first|after 属性名2;

(6)alter table 表名 drop 字段名;

(7)alter table 表名 ENGINE=存储引擎名;

9.清空表:truncate 表名;与delete的区别:truncate删除后不能回滚,delete删除后可以回滚。

10.删除表:drop table 表名;

二、表的创建、增加、删除、修改DDL

1.创建表:create table 表名(列名 列类型 列的完成性约束)engine=InnoDB default charset=utf8;

(1)自增长:auto_increment

(2)主键:primary key

(3)外键:foreign key

(4)非空值约束:not null

(5)唯一性约束:unique

(6)默认值约束:default

2.查看表结构:desc 表名;show create table 表名;

 3.插入:

(1)insert into 表名 values('内容',1);

(2)insert into 表名(列1,列2) vaules('内容',1);

(3)insert into 表名(列1,列2) vaules('内容',1),('内容',2)....;

(4)insert into 表名(属性列表1) select 属性列表2 from 表名2 where 条件表达式;

4.更新数据:

(1)update 表名 set 属性名1=取值1,属性名2=取值2 where 条件表达式;

5.删除数据:

delete from 表名 [where 条件表达式];

6.查询数据:

基本语法形式:select */属性列表

from 表名

[where 条件表达式]

[group by 属性名1 [having 条件表达式2]]分组

[order by 属性名2[asc|desc]]排序

(1)查询所有字段:select * from 表名;

(2)查询指定字段:select 列1,列2 from 表名;

(3)查询指定记录:select * from 表名 where 条件表达式;

(4)多表查询:分为左连接、右连接、内连接、等号连接

select * from 表1 left join 表2 on 表1.id=表2.s_id(关联id与s_id);以左边表的数据匹配右边的表中的数据,左边匹配的全部显示

select * from 表1 right join 表2 on 表1.id=表2.s_id(关联id与s_id);以右边表的数据匹配右边的表中的数据,右边匹配的全部显示

select * from 表1 inner join 表2 on 表1.id=表2.s_id(关联id与s_id);匹配两站表中的数据,匹配到的显示。

select * from 表1,表2 where 表1.id=表2.s_id;

(5)起别名as

select * from hhhhhhh as a;或者select * from hhhh a;

条件:

(6)in关键字:可以判断某个字段的值是否在指定的集合中

select * from 表名 where id in (1,100);/not in(2,3);

(7)or关键字:可以用来联合多个条件进行查询,只要有一条正确,就会查询出这个条正确的,错误的不显示。

(8)and关键字:必须全部满足,才可以查询出来内容where name='fd' and id=8;

 (9)剃重distinct,表中的某些字段上没有唯一性约束,查询时会有重复的行,可以使用

(10)限制:limit;select * from 表名 limit 1,5;可查询出第二条以后的5条数据,如果写0才会从第一条数据查询

(11)between and:查询出字段的值在指定的范围内 where age between 10 and 22;(10~22),并且包括10和22

(12)like/not like:where name like '%张_';--其中%0个或者多个字符,_,任意一个字符

(13)查询空值:is [not] null;注意:数据库中的空白和null显示并不相同。

(14)聚合函数:COUNT()统计;SUM()求和;AVG()平均;MAX()最大;MIN()最小,group by通常需要与集合函数一起使用

(15)合并结果集:union/union all

select 列1int from table1 union select 列1int from table2;

(16)子查询:update 表 set name='ff' where id in (select id from 表2 where ...);

(17)运算符:=<>!=><<=>=

 三、DML:数据库中的user表

数据库授权:grant 命令的格式

取消数据库用户权限:revoke

四、数据库的备份和恢复

(1)备份mysqldump

 mysqldump -uuser -ppassword dbname table[option] > xx.sql;

 mysqldump -uuser -ppassword -A > xx.sql;

 (2)恢复:mysql -uroot -p123456 数据库名 < 数据库名.sql

五、索引

create unique index 索引名称 on 表名(列)

alter table 表名 add index 索引名称(列)

drop index 索引名 on 表名;

六、存储过程

delimiter $$;

create procedure 名称(参数列表)

BEGIN

 SQL语句块

END

$$;

delimiter;

call 名称();

原文地址:https://www.cnblogs.com/victory-0315/p/6242531.html