码迷,mamicode.com
首页 > 数据库 > 详细

MySQL数据类型/属性/增删改查(14)

时间:2018-04-18 23:41:42      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:dom   数据库   user   nio   字符集   查询   日期类型   inner   表示   

MySQL数据类型

  • 日期类型

·date

date数据类型负责存储日期信息(1000-01-01到9999-12-31)
可以使用数字和字符串插入(20180809或"2018-08-09")非数字或字母使用分隔符

·datetime

datetime数据类型负责存储日期和时间信息的组合(1000-01-01 00:00:00到9999-12-31 23:59:59)
可以使用数字和字符串插入(20180809122556或"2018-08-09 12:25:56")

·time

time数据类型负责存储时间信息(-838:59:59到838:59:59)

·timestame

timestame自动获取当前的日期和时间

  • 数值数据类型

·bool和boolean

bool和boolean只是tinyint(1)的别名

·tinyint

tinyint数据类型是MySQL排名第五的整数范围(无符号值:0~255 有符号值:-128~127)

·smallint

smallint数据类型是MySQL排名第四的整数范围(无符号值:0~65535 有符号值:-32768~32767)

·mediumint

mediumint数据类型是MySQL排名第三的整数范围(无符号值:0~16777215 有符号值:-8388608~8388607)

·int

int数据类型是MySQL排名第二的整数范围(无符号值:0~4294967295 有符号值:-2147483648~2147483647)

·bigint

bigint数据类型是MySQL排名第一的整数范围(无符号值:0~18446744073709511615 有符号值:-9233372036854755808~9233372036854755807)

·decimal

decimal数据类型是存储为字符串的浮点数(无符号值:2.2250738585072014E-308~1.7976931348623157E+308 有符号值:-1.7976931348623157E+308~2.2250738585072014E-308)

·double

double数据类型是双精度浮点数(无符号值:2.2250738585072014E-308~1.7976931348623157E+308 有符号值:-1.7976931348623157E+308~2.2250738585072014E-308)

·float

float数据类型是单精度浮点数(无符号值:1.175494351E-38~3.402823466E+38 有符号值:-3.402823466E+38~-1.175494351E-38)

浮点型比如:double(M,D) M存储数值的个数,D小数的位数

 

  • 字符串数据类型

·char

char数据类型为MySQL提供了固定的长度,支持最大255个字符,如果插入的字符不足占用length指定的长度,剩余部分用空格填充

·varchar

varchar数据类型是MySQL的可变长度,支持长度为65536个字符,会保留尾部的空白部分

·longblob(longtext非二进制)

longblob数据类型是MySQL以二进制字符串表示形式,支持长度4294967259个字符

·mediumblob(mediumtext非二进制)

mediumblob数据类型是MySQL二进制字符串表示形式,支持长度16777215个字符

·blob(text非二进制)

blob数据类型是MySQL二进制字符串表示形式,支持长度65535个字符

·tinyblob(tinytext非二进制)

tinyblob数据类型是MySQL二进制字符串表示形式,支持长度255个字符

MySQL属性

  • primary key

主键,只能出现一次

创建自动增加主键

create table xiu (
    id smallint not null auto_increment
)

创建单字段主键

create table xiu (
    id varchar(255),
    primary key(id)
)

创建多字段主键

create table xiu (
    id varchar(255) not null,
    age  varchar(255) not null,
    primary key(id,age)
)
  • not null

不能为空

  • auto_increment

自动增长

  • binary

区分大小写

  • unique

只能出现一次,null可以多次出现

  • national

确保使用默认字符集

  • default

确保没有任何值可用的情况下,赋予某个常量值

  • zerofill

前导,用0填充

  • unsigned

设置负号

操作数据库

  • 查看数据库
show databases;//查看所有数据库
  • 添加数据库
create database xiu;//添加名为xiu的数据库
  • 删除数据库
drop database xiu;//删除名为xiu的数据库
  • 默认数据库
use xiu;//默认为xiu数据库

操作数据库表

  •  添加表
create table xiu (
    id tinyint primary key auto_increment,//主键,自增
    name varchar(20) not null,//不能为空
    age int not null//不能为空
)
  • 查询表
show create table xiu;//查询xiu表
  • 重命名
rename table xiu to kang;//将xiu表重命名为kang表
  • 删除表
drop table xiu;//删除xiu表

操作数据库字段

  • 添加一个列
alter table xiu add column kang varchar(10);
  • 修改一个列
alter table xiu change kang kang varchar(10) not null;
  • 删除一个列
alter table xiu drop birdate;
  • 重命名一个列
alter table kang change age sex varchar(10);

数据的操作

  • 添加一条数据
insert into xiu values(1,"name","age");
  • 指定字段添加值
insert into xiu (name,age) values ("name","age");//因为主键是自增,所以可以不用添加id字段
  • 查看数据
select * from xiu;//*代表查询所有字段,也可以指定字段查询
  • 修改数据
update xiu set name="user" where id=1;//将id=1的数据的name字段的值修改为"user"
  • 删除数据
delete from xiu where id = 1;//删除id为1的数据

 添加查询

  • 字段与值相等

=

 (查询xiu表的数据,条件为name="user")

select * from xiu where name = "user";
  • 同时满足两个条件

and

(查询xiu表的数据,同时满足这两个条件name="user",age=18)

select * from xiu where name="user" and age=18;
  • 满足任意条件
or
(查询xiu表的数据,满足其中一个条件name="user",age=18)
select * from xiu where name="user" or age=18;
  • 该字段与值相等
in
(查询xiu表的数据,满足其中任一条件age=24,age=18)
select * from xiu where age in(24,18);
  • 该字段与值不相等

not in

(查询xiu表的数据,不满足其中任一条件age=24,age=18)

select * from xiu where age not in(24,18);
  • 该字段满足是该值,主要判断空值
 is
(查询xiu表的数据,条件为age=null)
select * from xiu where age is null;
  • 该字段不满足该值 is not 值

 (查询xiu表的数据,条件为age!=null)

select * from xiu where age is not null;
  • 模糊查询需要通配符

like

‘_’:表示占一位 

‘%’:表示占零位或者多位

 (查询xiu表的数据,条件为name值的第二个字符为s)

select * from xiu where name like("_s%");
  • 模仿查询,需要通配符‘_’‘%’

not like

 (查询xiu表的数据,条件为name值的第二个字符不为s)

select * from xiu where name not like("_s%");
  • 范围查询

between

 (查询xiu表的数据,条件为age的值的范围是10-30的数)

select * from xiu where age between 10 and 30;
  • 去除重复

distnct

(查询xiu表的name字段,条件为删除重复的name值)

select distinct name from xiu;
排序
  • 升序 

order by ... asc

 (查询xiu表的数据,按照age从小到大排序)

select * from xiu order by age asc;
  • 降序

order by ... desc

 (查询xiu表的数据,按照age从大到小排序)

select * from xiu order by age desc;
截取
limit
(查询xiu表的数据,获取第一条数据开始的三条的数据)
select * from xiu limit 0,3;
聚合函数
  • avg平均值
  • max最大值
  • min最小值
  • sum和
  • count(空值不计数)数据条数
(获取xiu表中name字段有多少条数据)
select count(name) from xiu;
分组
group by
where后不能直接添加聚合函数
 (将xiu表按字段age进行分组,然后获取每个组最大的id)
select max(id) from xiu group by age;
过滤
having
(获取每个组最大的id,且id不能为2)
select * from xiu group by age having id != 2;
 
  • where与having的区别
where在group by之前,针对每一行的数据
having在group by之后,针对每一组的数据
 
外键约束
 ··········································································续写中。。。。。。。
foreign key(从表字段)references 主表名(主表主键)
 
外键的查询
show create table 表名
 
外键的删除
alter table 表名 drop foreign key 外键名
 
外键的添加
alter table 表名 add foreign key(从表字段)references 主表名(主表主键)
 
主表更新
on update
 
主表删除
on delete
 
允许的级联
cascade 如果主表被更新或删除,那么从表也会执行相应操作
set null如果表名删除或更新,那么从表变成null
restrict拒绝主表的相关操作 默认值
 
⊙⊙别名
as 名
as可以省略
 
⊙⊙多表
select a.字段1,b.字段2 from 表名1 as a,表名2 as b
 
⊙⊙子查询
 
按位置分
where型
from型
exists型 了解(判断是否存在)
 
按结果分
一个值 :标量子查询 可以使用 = < > !=
 
一列值 :列子查询
in满足集合中任意一个条件即可
not in不满足集合中的条件
=any相当于in
<any小于集合中的最大值
>any大于集合中的最小值
!=any不等于集合中的值
=all等于集合中的所有值
<all小于集合中的最小值
>all大于集合中的最大值
!=all相当于not in
 
一行值
需要加(字段1,字段2...)字段个数与子语句查询的结果相关
 
多行多列
需要为该表添加别名
 
⊙⊙内置函数
 
数学方法
floor(x):返回x的整数部分
round(x,y)四舍五入,y,保留了的小数位数。
字符串
length(str):求出字符串的长度,以字节为单位。
 
substring(str,b,e):b下标(从1开始)。b:开始位置 e:截取个数
substr(str,x):从下标x开始截取。
 
lower(str):转小写
 
upper(str):转大写
trim(str):去除所有空格
ltrim(str):去除左边的空格
 
rtrim(str):去除出右边空格
时间日期
now():当前系统时间
date_format();时间的格式化
%Y:四位数的年 %y:二位数的年
%M:英文月份   %m:数字的月份
dayname():返回当前日期的星期几
dayofweek():返回当月中,每周的第几天,结果为1-7,从周日开始算。
dayofyear():返回一年中的第多少天。
dayofmonth():一个月中的第多少天
datediff(date1,date2): 计算两个日期之间的时间差,单位是天。
 
⊙⊙连接查询 :多表的操作
 
内连接  inner join(inne可以省略)
on    表示连接时就判断,不生成笛卡尔集
        左表名 inner join 右表名 on 条件
where 表示先生成笛卡尔集,在进行过滤
        左表名 inner join 右表名 where 条件
using 前提 拥有相同的字段名
        左表名 inner join 右表名 using(字段)
 
外链接  outer join(outer可以省略)外连接必须添加条件
左连接  没有连接成功,左表数据保留,右表用空值代替
        左表名 left outer join 右表名 on/using 条件
右连接  没有连接成功,左表用空值代替,右表数据保留
        左表名 right outer join 右表名 on/using 条件
 
全连接 full join
mysql暂不支持
可以使用   (左连接)union(右连接)  实现相同效果
 
自然连接 natural join
natural join 有相同字段,类似于using
natural left join 有相同字段,类似于左连接的using
natural right join 有相同字段,类似于右连接的using
 
笛卡尔连接
连接时没有添加条件
一张表的数据与另外一张表的所有数据连接

MySQL数据类型/属性/增删改查(14)

标签:dom   数据库   user   nio   字符集   查询   日期类型   inner   表示   

原文地址:https://www.cnblogs.com/xiukang/p/8569966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!