2018-07-29学习mysql小结

    此前在校学习期间,只是知道数据库很重要,但是并未主动去学习了解。现在的学习过程中学了一遍mysql,就简单的做一个总结吧。

    首先记住三个概念:

    1.数据库(Database)是按照数据结构来组织、存储和管理数据的建立在计算机存储设备上的仓库。   

    2.SQL :结构化查询语言(Structured Query Language)

    3.MySQL:关系型数据库管理系统

    database中存储着各种数据,sql语句用于从database中找出我们需要的数据,mysql是一种应用软件,通过语句对database进行操作。

    MySQL我使用的是5.6版本,通过管理员身份打开cmd后,启用mysql服务为:net start mysql56,关闭服务为:net stop mysql56。登录:mysql -h localhost -u root -p     回车后输入密码:123456(用户名和密码在安装时进行设置)


    下面将会从四个方面进行总结:

    1.数据定义语言(DDL)

    2.数据操作语言(DML)

    3.数据查询语言(DQL)

    4.函数

    本篇小结大部分是语句格式,如果需要代码实现的截图,以及部分额外知识点标注,可以下载安装xmind软件后,下载云盘里的思维导图进行查看。

链接:https://pan.baidu.com/s/18a3gY9Rzu7TOox-Tkgq-_w 密码:qrie

ximd内容如下:


 

数据定义语言(DDL):

1.建表:

create table 表名(                                                    create table test(

          列名称1 数据类型 ,                                                           id int,

          列名称2 数据类型,                                                            name char(10),

          .........                                                                                    ......

          列名称n 数据类型);                                                       birthday date);

常用的数据类型:整数(int,tinyint,smallint),小数(float,decimal),字符串(char,varchar),时间(date,time)

2.约束:

primary key(主键):约束唯一标识数据库表中的每条记录

foreign key(外键):foreign key就是表与表之间的某种约定的关系,从一个表指向另一个表

unique:约束用于限制加入表的数据的类型

建表时可在数据类型后添加:

create table test(id int primary key);

create table test(id int,primary key(id);

或者建表时未添加,后面需要时再添加:

alter table test add primary key(id);

删除主键:alter table test drop primary key;

unique的用法与primary key 相同。

外键的用法:

create table t1(id int primary key,name char(10));

create table t2(id int primary key,pri_id int,name char(10),

constraint i foreign key(pri_id) references t1(id));

意思为:把t2中的pri_id 作为外键,指向t1中的主键id

删除外键:constraint i,把外键命名成i,方便了我们删除外键

alter table t2 drop foreign key i; 

3.字段属性

1.unsigned(无符号型),只能用在数值型字段

2.zerofill(自动补零),只能用在数值型字段,前导零,同时该字段自动式UNSIGNED

3.AUTO_INCREMENT(自动增长),寄生于主键

4.NOT NULL:强制约束列不守NULL值,即不添加数值就无法插入数据

5.缺省值(default):给数据一个默认值(系统默认值是NULL),不能与auto_increment同时用于一个字段上

写法均为:create table test(id int 字段属性);


DML(数据操作语言):

1.索引:

创建:create index 索引名 on 表名(字段);            create index i on test(id);

删除:drop index 索引名;                                        drop index i;

2.对数据操作:

insert(插入):

插入单独数据:insert into 表名 字段1、字段2... values(值1、值2...);

插入默认数据:insert inro 表名 values(值1、值2...);

insert into test(id,name) values(008,'周星星');

update(修改、更新):修改(更新)数据:update 表名 set 字段=新值 where 列名称=某值

update test set name='詹姆斯' where id=008;

修改多条数据:

update test set name=case id

when 001 then 'qwe'

when 002 then 'asd'

end

where id in(001,002)

replace(批量更新数据)(也可用于数据添加):replace into 表名(字段1、字段2...) values(值1、值2...)

replace into test(id,name) values(002,'777');

未添加的数据会默认为null

批量更新(通过update):

insert into test(id,name) values(001,'i am 001'),(002,'i am 002')

on duplicate key update

id=values(id),name=values(name);

delete删除数据(一条):delete from 表名 where 条件

delete from test where id=001;

 

3.对表操作

修改表名:alter table 旧表名 rename as 新表名;

alter table test rename as father;

修改字段的数据类型:alter table 表名 modify 字段名 数据类型

alter table test modify id char(10);

增加字段:alter table 表名 add 字段名1 数据类型

alter table test add address char(30);

删除字段:alter table 表名 drop 字段名;

alter table test drop address;


DQL(数据查询语句insert):

1.交叉查询:select 表1.字段,表2.字段 from 表1,表2;

多表联合查询:select 表1.字段,表2.字段 from 表1,表2 where 表1.id=表2.id;

select * from t1,t2 where t1.id=t2.id;

2.查询不重复的数据:select distinct 字段 from 表名;

select distinct id from test;

3.取别名alias:select * from 表名 as 别名;

自连接:select b.* from shop as a,shop as b where a.name='包子' and a.price

把shop表取别名为a,b,通过a和b进行自己数据的对比

4.limit(偏移量):

查询前n条数据:select *from 表名 limit n;

查询前n条数据后的i条数据:select *from 表名 limit n,i;

5.in(在where后规定多个值):select 字段 from 表名 where 字段 in(值1,值2);

select * from test where id in(1,2,3);

6.like(用于where子句中搜索列中的指定模式):select 字段 from 表名 where 字段 like 表达式;

例如搜索王姓青年:

select *from test where name like'王%';

%:表示0~多个字符

_:表示一个字符

7.order by(排序):select 字段 from 表名 order by 字段(排序方式) [desc](使用倒序排列)

select * from test order by id desc;

表示通过id从大到小进行排序显示

8.join连接:

inner jor(内连接),left join(左连接),right join(右连接)

full join(全连接)(mysql不支持全连接)

格式都相同:select 字段 from 表1 inner/left/right join 表2 on 表1.字段=表2.字段;

9.union(联合查询):select *from 表1 union select *from 表2;


函数:

1.合集函数:操作面向一系列的值,并返回一个单一的值

写法都相同:

查询某列的平均值:select avg(字段) from 表名;

返回某列的行数:select count(字段) from 表名;

查询某列最大值:select max(字段) from 表名;

查询某列最小值:select min(字段) from 表名;

返回某列总和:select sum(字段) from 表名;

分组显示总和:select sum(字段) from 表名 group by 字段;

2.标量函数:操作面向某个单一的值,并返回基于输入值的一个单一的值

写法也都相同:

ucase(把字段的值转换为大写):select ucase(字段) from 表名;

lcase(把字段的值转换为小写):select lcase(字段) from 表名;

mid(提取字符):mid(字段,起始,结束):

select mid(name,2,3) from test;

表示从name列的第二个数据开始,每个数据只显示3位

len(返回文本长度):select length(字段) from 表名;

round(把数值四舍五入,并保留相应小数位):select round(字段,数字) from 表名;

now()(查询当前时间):select now() from 表名;(有几个数据就出现几个)


    此文章虽然是自己的学习小结,而且还是身为初学者的我写的,但也希望更多的朋友能看到我的文章,如果有不足之处或疑问,欢迎到留言区留言。

猜你喜欢

转载自www.cnblogs.com/bkytep/p/9385049.html