全部文章/数据库

MySQL基础整理

mysql基础入门知识点

整型

  • TINYINT
  • SMALLINT
  • MEDIUMIN
  • INT
  • BIGINT
字节数分别为 1 2 3 4 8

浮点型

  • float(M,D) 字节数4 单精度浮点行
  • double(M,D) 字节数 8 双精度浮点数
M表示浮点型数据总共的位数;

D表示浮点型数据小数点之后的精度,四舍五入法;

M-D为小数点前面位数,超出插入会提示报错,小数点最多显示D位;

定点型

decimal

decimal在插入数据的时候是多少就是多少,float/double存在小数点后精度丢失问题;

float/double存储的是近似值——–decimal以字符串形式;

日期类型

  • date
  • time
  • year
  • datetime
  • timestamp
date        yyyy-MM-dd time        HH:mm:ss 时分秒 year        yyyy datetime  yyyy--MM--dd HH:mm:ss  8字节 timestamp   yyyy-MM-dd HH:mm:ss 4字节       可以做时间戳--查看mysql时间戳 select current_timestamp,current_timestamp();

datetime默认为空 插入null则位null; timestamp默认值不为空,插入null则为当前时间;

字符类型

  • char
  • varchar
  • blob
  • text
char固定长度字符串,varchar可变长度字符串;

 

数据库操作

mysql -uroot -p  登录数据库;

show databases;  显示数据库;

create database dataname;  新建dataname数据库;

use dataname;  使用dataname数据库;

exit;  退出连接;

数据表操作

show tables;  显示当前数据库中表;

create tables name( id int(4), name varchar(5), death date ); 创建name表;

select * from name; 查询表name全部数据; describe name; 查询name表结构;

insert into name vlaues(‘123’,‘王二’,‘2000-10-1’); 插入数据 insert name values(‘123’,‘王二’,‘2000-10-1’); insert name(id) values(‘123’); delete from name where name=‘王二’;   删除数据; update name set name=’王三‘ where name=’王二‘  更新数据; drop table name; 删除name表;

约束条件

主键约束 primary key

主键具有唯一性,非空,一个表只能有一个主键,也可组合;
create table keytest( id int(3) key, name varchar(5) ); 创建主键id
 
create table keytest( id int(3), name varchar(5), key(id,name) ); 创建组合主键

唯一约束 unique

具有唯一性,可以为空
create table uniquetest( id int(3), name varchar(5) unique ); name值不能重复插入会报错

非空约束 not null

不可为空
create table nulltest( id int(3) not null, name varchar(5) ); 创建id为非空约束

外键约束 foreign key

创建表设置外键
create table class( c_id int key not null ); create table foreigntest( id int not null key, name varchar(5), id_c int, foreign key(id_c) references class(c_id) ); 删除外键 alter table foreigntest drop foreign key 外键; show create table foreigntest;查看
外键作用:束缚子表数据插入,束缚父表数据操作(子表引用父表数据则更改不了父表)

自增长 auto_increment

create table test( id int key AUTO_INCREMENT, name varchar(5) ); 插入数据时可以null则自增长

数据库用户

查看用户 select host,user,authentication_string from mysql.user;

创建用户 create user “username”@“host” identified by “password”;

删除用户 drop user ‘username’@‘host’;

授权 grant privileges on databasename.tablename to ‘username’@‘host’ IDENTIFIED BY ‘password’;

删除权限 revoke privileges on databasename.tablename from ‘username’@‘hose’;

 

 

 

 

检索

用途从一个或多个表中检索信息;
select id,name from tablename; 检索tablename表中的id列,name列并显示出来;

distinct关键字 返回不同的值,id,name都加的话则两个列都不同,否则返回; limit关键字 limit n 返回不多于n行,limit x,y 返回从x+1行开始 检索y行; select table.name 返回表的name列 也可限制from中条件;

排序

用途从检索的数据中进行排序
order by select * from table order by id,name; 对表进行id排序在进行name排序;

指定排序方向 默认的排序方向是升序排序(A-Z) 降序排序(Z-A)使用desc关键字 select * from table order by id desc;

select * from table order by id desc, name asc; 将id降序,name升序排列;

过滤

作用筛选自己需要的数据 where
select * from table where id=001; 返回id=001的结果;

<> / != 不等于 between 在两者之间

select * from table where id between 01 and 04; 返回id在01到04的项,包括两端;

多个条件可以使用and 或者 or 连接; in指定条件范围 select * from table where id in(100,103); not否定后面跟的条件

通配符

作用匹配值的一部分的特殊字符
like操作符 通配符%:任何字符出现的任意次数 通配符_:单个字符

select * from table where name like ”李%“;

函数

聚集函数 avg() count() max() min() sum() 聚集不同值 distinct关键字

select avg(distinct fenshu) from table; 只考虑分数中的不同值

分组

作用把数据分为多个逻辑组,在进行聚集计算;
group by 分组数据 having 过滤分组 where过滤行

select id, count() as sum from table group by id having count()>2; 先将表对id进行分组,过滤出id频率大于2的数据;

select子句顺序
select子句 select 要返回的列或表达式 from 从中检索数据的表 where 行级过滤 group by 分组说明 按组计算聚合时使用 having 组级过滤 order by 输出排序顺序 limit 检索的行数

 

联结表
等值联结,内部联结,自联结

在一条select语句中关联表

id name在table1,class在table2中 select table1.id,name,class from table1,table2 where table1.id=tables2.id

笛卡儿积:由没有联结条件的表关系返回的结果为笛卡尔积,返回的则是第一个表行数*第二表行数

内部联结
select table1.id, name ,class from table1 inner join table2 on table1.id=table2.id 与上面的语句返回的是一样的结果

表别名用法 select t1.id,name,class from table1 t1,table2 t2 where t1.id=t2.id

组合查询
union作用:连接两个以上select语句结果结合到一个结果集合中;自动过滤重复的行 union all 返回所有行;
插入数据
表插入数据 insert into table values(1,'a',null); insert into table(id,name) values(1,'a'); 表插入查询数据 insert into table3(idid,namename) select id,name from table1;
更新数据
update table set name='蜗牛' where id=1; 将table表id=1的name改为'蜗牛'
删除数据
delete from table where id=1 删除table表id为1的行
更新表
alter table table1 add phone char(20) 增加列 alter table table1 drop column phone 删除列

重命名表:rename table table1 to table

 

最近又看了一本关于MySQL的书《MySQL数据库应用从入门到精通》感觉比以前看的《MySQL必知必会》里面讲解的要更加细致,于是分享一下笔记。

索引

索引:索引是创建在数据库对象上的,由表中的一个字段或多个字段组成

索引可分为普通索引,唯一索引,全文索引,多列索引

创建/查看普通索引

创建普通索引 index |key 索引名(属性名1)长度 ASC|DESC :index index_name (name) 在字段name上创建索引名为index_name

存在的表中创建索引 create index 索引名 on 表名 (属性名 长度 asc|desc) create indet index_name on t_name (name)

alter table 添加 :alter table t_name add index | key 索引名(属性名 长度 asc|desc)

创建查看唯一索引:创建索引时,限制索引的值必须唯一 -------自动索引和手动索引

创建表示创建唯一索引 unique index |key 索引名(属性名1) 长度 asc|desc

存在表中创建索引 create unique index 索引名 on 表明 (属性名 长度 asc|desc)

alter table 添加 :alter table t_name add unique index |key 索引名(属性名 长度 asc|desc)

创建和查看全文索引:全文索引主要关联在char varchar text字段上

fulltext index|key 索引名 属性名 长度 asc|desc

create fulltext idnex 索引名 on 表名 属性名 长度 asc|desc

alter table 添加:alter table t_name add fulltext index|key 索引名 属性名 长度 asc|desc

创建和查看多列索引

index |key 索引名 (属性名1 长度 asc |desc ,属性名2 长度 asc|desc)

create index 索引名 on 表名(属性名1 长度 asc|desc,属性名2 长度 asc|desc)

alter table t_name add index|key 索引名 (属性名1 长度 asc|desc,属性名2 长度 asc|desc)

删除索引:

drop index index_name on t_name

视图

视图:视图是虚拟的表,视图的内容更新影响基本表

创建

create view view_name

as 查询语句

查看

show tables 查看视图名

show table status from db 查看视图详细信息

show create view view_name 查看视图定义信息

desc view_name 查看视图设计信息

通过系统表查看视图信息

修改

create or replace view view_name

as 查询语句

alter view view_name

as 查询语句

删除

drop view view_name

视图操作基本表

insert into view_name values()

update view_name set name='abc' where id='1'

delete from view_name where id='2'

触发器

触发器:自动执行 delete语句 insert语句 update语句

创建触发器

一条执行语句

create trigger trigger_name

before|after trigger_event

on t_name for each row trigger_stmt

before|after 触发器在事件发生前|后执行

trigger_event 触发事件(包含insert|update|delete)

t_name 触发事件操作表的名字

for each row 任一条记录上的操作满足触发条件都会触发该触发器

trigger_stmt 触发器被执行的语句

:create tri_test

before insert

on student for each row

insert into log_table values(………)

创建触发器tri_test 在student表插入数据前向表log_table插入数据

多条执行语句

create trigger trigger_name

before|after trigger_event

on t_name for each row

begin

trigger_stmt

end

查看

show triggers

在information_schema数据库 triggers表存储所有触发器

删除

drop trigger trigger_name

由于笔记不是在一台终端上写的所以字体会不一样,还请谅解。