欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL数据库操作、MySQL数据类型以及SQL Date数据类型讲解

程序员文章站 2023-08-26 19:09:03
sql基础知识 sql 访问和处理数据库 对大小写不敏感! 每条 sql 命令的末端使用分号 文本值, 单引号来环绕 数值,请不要使用引号 rdbms - 关系数据库管理系统(...

sql基础知识

sql 访问和处理数据库
对大小写不敏感!
每条 sql 命令的末端使用分号

文本值, 单引号来环绕
数值,请不要使用引号

rdbms - 关系数据库管理系统(relational database management system)

where 关键字无法与合计函数一起使用

数据操作语言 (dml) 和 数据定义语言 (ddl)

    # 数据操作语言 (dml)
    select - 从数据库表中获取数据
    update - 更新数据库表中的数据
    delete - 从数据库表中删除数据
    insert into - 向数据库表中插入数据

    # 数据定义语言 (ddl)
    create database - 创建新数据库
    alter database - 修改数据库
    create table - 创建新表
    alter table - 变更(改变)数据库表
    drop table - 删除表
    create index - 创建索引(搜索键)
    drop index - 删除索引

order by 默认升序排序, 降序 desc

-- 通配符
- % 替代一个或多个字符
- _ 仅替代一个字符
- [charlist]    字符列中的任何单一字符
- [^charlist]或者[!charlist]不在字符列中的任何单一字符

-- sql 约束
not null
unique
primary key
foreign key
check
default

mysql 数据类型

在 mysql 中,有三种主要的类型:文本、数字和日期/时间类型。

-- text 类型:

数据类型  描述
char(size)  固定长度字符串 最多 255 个字符。
varchar(size) 可变长度的字符串 最多 255 个字符。

注释:如果值的长度大于 255,则被转换为 text 类型。

tinytext  存放最大长度为 255 个字符的字符串。
text  存放最大长度为 65,535 个字符的字符串。
blob  用于 blobs (binary large objects)。存放最多 65,535 字节的数据。
mediumtext  存放最大长度为 16,777,215 个字符的字符串。
mediumblob  用于 blobs (binary large objects)。存放最多 16,777,215 字节的数据。
longtext  存放最大长度为 4,294,967,295 个字符的字符串。
longblob  用于 blobs (binary large objects)。存放最多 4,294,967,295 字节的数据。
enum(x,y,z,etc.)  
允许你输入可能值的列表。可以在 enum 列表中列出最大 65535 个值。如果列表中不存在插入的值,则插入空值。

注释:这些值是按照你输入的顺序存储的。

可以按照此格式输入可能的值:enum('x','y','z')

set 与 enum 类似,set 最多只能包含 64 个列表项,不过 set 可存储一个以上的值。

-- number 类型:

数据类型  描述
tinyint(size) -128 到 127 常规。0 到 255 无符号*。在括号中规定最大位数。
smallint(size)  -32768 到 32767 常规。0 到 65535 无符号*。在括号中规定最大位数。
mediumint(size) -8388608 到 8388607 普通。0 to 16777215 无符号*。在括号中规定最大位数。
int(size) -2147483648 到 2147483647 常规。0 到 4294967295 无符号*。在括号中规定最大位数。
bigint(size)  -9223372036854775808 到 9223372036854775807 常规。0 到 18446744073709551615 无符号*。在括号中规定最大位数。
float(size,d) 带有浮动小数点的小数字。在括号中规定最大位数。在 d 参数中规定小数点右侧的最大位数。
double(size,d)  带有浮动小数点的大数字。在括号中规定最大位数。在 d 参数中规定小数点右侧的最大位数。
decimal(size,d) 作为字符串存储的 double 类型,允许固定的小数点。
* 这些整数类型拥有额外的选项 unsigned。通常,整数可以是负数或正数。如果添加 unsigned 属性,那么范围将从 0 开始,而不是某个负数。

-- date 类型:

数据类型  描述
date()  
日期。格式:yyyy-mm-dd

注释:支持的范围是从 '1000-01-01' 到 '9999-12-31'

datetime()  
*日期和时间的组合。格式:yyyy-mm-dd hh:mm:ss

注释:支持的范围是从 '1000-01-01 00:00:00' 到 '9999-12-31 23:59:59'

timestamp() 
*时间戳。timestamp 值使用 unix 纪元('1970-01-01 00:00:00' utc) 至今的描述来存储。格式:yyyy-mm-dd hh:mm:ss

注释:支持的范围是从 '1970-01-01 00:00:01' utc 到 '2038-01-09 03:14:07' utc

time()  时间。格式:hh:mm:ss 注释:支持的范围是从 '-838:59:59' 到 '838:59:59'
year()  
2 位或 4 位格式的年。

注释:4 位格式所允许的值:1901 到 2155。2 位格式所允许的值:70 到 69,表示从 1970 到 2069。

* 即便 datetime 和 timestamp 返回相同的格式,它们的工作方式很不同。在 insert 或 update 查询中,timestamp 自动把自身设置为当前的日期和时间。timestamp 也接受不同的格式,比如 yyyymmddhhmmss、yymmddhhmmss、yyyymmdd 或 yymmdd。

sql date 数据类型

mysql 使用下列数据类型在数据库中存储日期或日期/时间值:

date - 格式 yyyy-mm-dd
datetime - 格式: yyyy-mm-dd hh:mm:ss
timestamp - 格式: yyyy-mm-dd hh:mm:ss
year - 格式 yyyy 或 yy

mysql date 函数
下面的表格列出了 mysql 中最重要的内建日期函数:

函数  描述
now() 返回当前的日期和时间
curdate() 返回当前的日期
curtime() 返回当前的时间
date()  提取日期或日期/时间表达式的日期部分
extract() 返回日期/时间按的单独部分
date_add()  给日期添加指定的时间间隔
date_sub()  从日期减去指定的时间间隔
datediff()  返回两个日期之间的天数
date_format() 用不同的格式显示日期/时间

代码示例

$ mysql -uroot -p
> show databases;
> use demo;
> show tables;

-- 创建表
create table persons(
id int auto_increment primary key,
name varchar(32),
age int,
city varchar(32));

-- 删除表
> drop table persons;

-- 插入数据
insert into persons(name, age, city) value('张三', 20, '北京');
insert into persons(name, age, city) value('李四', 21, '上海');
insert into persons(name, age, city) value('赵倩', 24, '北京');
insert into persons(name, age, city) value('孙力', 23, '天津');
insert into persons(name, age, city) value('周五', 22, '深圳');
insert into persons(name, age, city) value('郑旺', 20, '重庆');

insert into persons(name, age, city) value('刘颖', 20, '北京');
insert into persons(name, age, city) value('王大', 20, '天津');
insert into persons(name, age, city) value('张开', 20, '深圳');
insert into persons(name, age, city) value('留取', 20, '重庆');

-- 查询所有
select * from persons;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  1 | 张三   |   20 | 北京   |
|  2 | 李四   |   21 | 上海   |
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
|  5 | 周五   |   22 | 深圳   |
|  6 | 郑旺   |   20 | 重庆   |
+----+--------+------+--------+

-- 查询部分字段
mysql> select name from persons;
+--------+
| name   |
+--------+
| 张三   |
| 李四   |
| 赵倩   |
| 孙力   |
| 周五   |
| 郑旺   |
+--------+

mysql> select age from persons;
+------+
| age  |
+------+
|   20 |
|   21 |
|   24 |
|   23 |
|   22 |
|   20 |
+------+

-- 去重
mysql> select distinct age from persons;
+------+
| age  |
+------+
|   20 |
|   21 |
|   24 |
|   23 |
|   22 |
+------+

-- where条件
mysql> select * from persons where age >22;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
+----+--------+------+--------+


mysql> select * from persons where name='赵倩';
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  3 | 赵倩   |   24 | 北京   |
+----+--------+------+--------+

-- 多条件查询
mysql> select * from persons where age=20 and city='北京';
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  1 | 张三   |   20 | 北京   |
+----+--------+------+--------+

mysql> select * from persons where (age=20 or age =24) and city='北京';
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  1 | 张三   |   20 | 北京   |
|  3 | 赵倩   |   24 | 北京   |
+----+--------+------+--------+

-- 排序,默认从小到大
mysql> select * from persons order by age;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  1 | 张三   |   20 | 北京   |
|  6 | 郑旺   |   20 | 重庆   |
|  2 | 李四   |   21 | 上海   |
|  5 | 周五   |   22 | 深圳   |
|  4 | 孙力   |   23 | 天津   |
|  3 | 赵倩   |   24 | 北京   |
+----+--------+------+--------+

-- 多字段排序
mysql> select * from persons order by age, id desc;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  6 | 郑旺   |   20 | 重庆   |
|  1 | 张三   |   20 | 北京   |
|  2 | 李四   |   21 | 上海   |
|  5 | 周五   |   22 | 深圳   |
|  4 | 孙力   |   23 | 天津   |
|  3 | 赵倩   |   24 | 北京   |
+----+--------+------+--------+

-- 更新数据
mysql> update persons set name='张大拿' where name='张三';
mysql> select * from persons;
+----+-----------+------+--------+
| id | name      | age  | city   |
+----+-----------+------+--------+
|  1 | 张大拿    |   20 | 北京   |
|  2 | 李四      |   21 | 上海   |
|  3 | 赵倩      |   24 | 北京   |
|  4 | 孙力      |   23 | 天津   |
|  5 | 周五      |   22 | 深圳   |
|  6 | 郑旺      |   20 | 重庆   |
+----+-----------+------+--------+

-- 删除数据,慎重!!!
mysql> delete from persons where name='张大拿';
mysql> select * from persons;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
|  5 | 周五   |   22 | 深圳   |
|  6 | 郑旺   |   20 | 重庆   |
+----+--------+------+--------+

-- 设置查询数量
mysql> select * from persons limit 2;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
|  3 | 赵倩   |   24 | 北京   |
+----+--------+------+--------+

-- 模糊查询
mysql> select * from persons where city like '上%';
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
+----+--------+------+--------+

mysql> select * from persons where city not like '上%';
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
|  5 | 周五   |   22 | 深圳   |
|  6 | 郑旺   |   20 | 重庆   |
+----+--------+------+--------+

-- 成员条件查询
mysql> select * from persons where name in ('赵倩', '周五');
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  3 | 赵倩   |   24 | 北京   |
|  5 | 周五   |   22 | 深圳   |
+----+--------+------+--------+

mysql> select * from persons where name not in ('赵倩', '周五');
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
|  4 | 孙力   |   23 | 天津   |
|  6 | 郑旺   |   20 | 重庆   |
+----+--------+------+--------+

-- 范围查询
mysql> select * from persons where age between 22 and 24;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
|  5 | 周五   |   22 | 深圳   |
+----+--------+------+--------+

mysql> select * from persons where age not between 22 and 24;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
|  6 | 郑旺   |   20 | 重庆   |
+----+--------+------+--------+

-- 创建citys表
create table citys(
id int auto_increment primary key,                                       
city varchar(32),
address varchar(64));

insert into citys(city, address) values('北京', '北京市');
insert into citys(city, address) values('上海', '上海市');
insert into citys(city, address) values('深圳', '广东省');
insert into citys(city, address) values('成都', '四川省');
insert into citys(city, address) values('大连', '辽宁省');
insert into citys(city, address) values('天津', '天津市');
insert into citys(city, address) values('广州', '广东省');


mysql> select * from citys;
+----+--------+-----------+
| id | city   | address   |
+----+--------+-----------+
|  1 | 北京   | 北京市    |
|  2 | 北京   | 北京市    |
|  3 | 上海   | 上海市    |
|  4 | 深圳   | 广东省    |
|  5 | 成都   | 四川省    |
|  6 | 大连   | 辽宁省    |
|  7 | 天津   | 天津市    |
|  8 | 广州   | 广东省    |
+----+--------+-----------+

-- 修改表名
mysql> alter table cards rename to citys;

-- 别名
mysql> select p.name,p.city,c.address from persons as p, citys as c where p.city=c.city;
+--------+--------+-----------+
| name   | city   | address   |
+--------+--------+-----------+
| 赵倩   | 北京   | 北京市    |
| 赵倩   | 北京   | 北京市    |
| 李四   | 上海   | 上海市    |
| 周五   | 深圳   | 广东省    |
| 孙力   | 天津   | 天津市    |
+--------+--------+-----------+

-- 内连接
mysql> select persons.name, persons.city, citys.address from persons inner join citys on persons.city=citys.city;
+--------+--------+-----------+
| name   | city   | address   |
+--------+--------+-----------+
| 赵倩   | 北京   | 北京市    |
| 赵倩   | 北京   | 北京市    |
| 李四   | 上海   | 上海市    |
| 周五   | 深圳   | 广东省    |
| 孙力   | 天津   | 天津市    |
+--------+--------+-----------+

-- 左连接
mysql> select persons.name, persons.city, citys.address from persons left join citys on persons.city=citys.city;
+--------+--------+-----------+
| name   | city   | address   |
+--------+--------+-----------+
| 赵倩   | 北京   | 北京市    |
| 赵倩   | 北京   | 北京市    |
| 李四   | 上海   | 上海市    |
| 周五   | 深圳   | 广东省    |
| 孙力   | 天津   | 天津市    |
| 郑旺   | 重庆   | null      |
+--------+--------+-----------+

-- 右连接
mysql> select persons.name, persons.city, citys.address from persons right join citys on persons.city=citys.city;
+--------+--------+-----------+
| name   | city   | address   |
+--------+--------+-----------+
| 李四   | 上海   | 上海市    |
| 赵倩   | 北京   | 北京市    |
| 赵倩   | 北京   | 北京市    |
| 孙力   | 天津   | 天津市    |
| 周五   | 深圳   | 广东省    |
| null   | null   | 四川省    |
| null   | null   | 辽宁省    |
| null   | null   | 广东省    |
+--------+--------+-----------+

#  mysql 不支持full join

create table names(
id int auto_increment primary key,
name varchar(32));

insert into names(name) values('李四');
insert into names(name) values('张林');
insert into names(name) values('斗舞');
insert into names(name) values('名天');
insert into names(name) values('高兴');
insert into names(name) values('王二');

mysql> select * from names;
+----+--------+
| id | name   |
+----+--------+
|  1 | 李四   |
|  2 | 张林   |
|  3 | 斗舞   |
|  4 | 名天   |
|  5 | 高兴   |
|  6 | 王二   |
+----+--------+

-- 合并结果集
mysql> select name from persons union select name from names;
+--------+
| name   |
+--------+
| 李四   |
| 赵倩   |
| 孙力   |
| 周五   |
| 郑旺   |
| 张林   |
| 斗舞   |
| 名天   |
| 高兴   |
| 王二   |
+--------+

-- 列出所有值
mysql> select name from persons union all select name from names;
+--------+
| name   |
+--------+
| 李四   |
| 赵倩   |
| 孙力   |
| 周五   |
| 郑旺   |
| 李四   |
| 张林   |
| 斗舞   |
| 名天   |
| 高兴   |
| 王二   |
+--------+

# mysql 不支持select inot

mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
| age   | int(11)     | yes  |     | null    |                |
| city  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 添加约束unique
mysql> alter table persons add unique(name);

mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  | uni | null    |                |
| age   | int(11)     | yes  |     | null    |                |
| city  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 删除约束unique
mysql> alter table persons drop index name;

mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
| age   | int(11)     | yes  |     | null    |                |
| city  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

--  删除primary key
mysql> alter table persons drop primary key;

mysql> show create table persons \g
create table: create table `persons` (
  `id` int(11) not null auto_increment,
  `name` varchar(32) default null,
  `age` int(11) default null,
  `city` varchar(32) default null,
  unique key `id` (`id`)
) engine=innodb auto_increment=7 default charset=utf8

-- 添加primary key
mysql> alter table persons add primary key(id);

mysql> show create table persons \g
create table: create table `persons` (
  `id` int(11) not null auto_increment,
  `name` varchar(32) default null,
  `age` int(11) default null,
  `city` varchar(32) default null,
  primary key (`id`),
  unique key `id` (`id`)
) engine=innodb auto_increment=7 default charset=utf8

-- 外键
create table familys(
id int primary key auto_increment,
person_id int,
menber_num int,
foreign key (person_id) references persons(id));

mysql> show create table familys \g
create table: create table `familys` (
  `id` int(11) not null auto_increment,
  `person_id` int(11) default null,
  `menber_num` int(11) default null,
  primary key (`id`),
  key `person_id` (`person_id`),
  constraint `familys_ibfk_1` foreign key (`person_id`) references `persons` (`id`)
) engine=innodb default charset=utf8


-- 删除外键
mysql> alter table familys drop foreign key familys_ibfk_1;

-- 添加外键
mysql> alter table familys add foreign key(person_id) references persons(id);

-- check约束 innodb中不支持check约束
create table student(
id int,
age int,
check(age>0 and age<100));

mysql> show create table student\g
create table: create table `student` (
  `id` int(11) default null,
  `age` int(11) default null
) engine=innodb default charset=utf8

-- 默认值default
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
| age   | int(11)     | yes  |     | null    |                |
| city  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 添加默认值
mysql> alter table persons alter age set default 0;

mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
| age   | int(11)     | yes  |     | 0       |                |
| city  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 删除默认值
mysql> alter table persons alter age drop default;
mysql> desc persons;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
| age   | int(11)     | yes  |     | null    |                |
| city  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 创建索引
mysql> create index person_index on persons(name);

mysql> show create table persons\g
create table: create table `persons` (
  `id` int(11) not null auto_increment,
  `name` varchar(32) default null,
  `age` int(11),
  `city` varchar(32) default null,
  primary key (`id`),
  unique key `id` (`id`),
  key `person_index` (`name`)
) engine=innodb auto_increment=7 default charset=utf8

-- 删除索引
mysql> alter table persons drop index person_index;

mysql> show create table persons\g
create table: create table `persons` (
  `id` int(11) not null auto_increment,
  `name` varchar(32) default null,
  `age` int(11),
  `city` varchar(32) default null,
  primary key (`id`),
  unique key `id` (`id`)
) engine=innodb auto_increment=7 default charset=utf8


-- 创建数据库
mysql> create database data1;

mysql> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| data1              |
| demo               |
| mydata             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

-- 删除数据库
mysql> drop database data1;

mysql> show databases;
+--------------------+
| database           |
+--------------------+
| information_schema |
| demo               |
| mydata             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

-- 删除表格
mysql> show tables;
+----------------+
| tables_in_demo |
+----------------+
| citys          |
| familys        |
| mytable        |
| names          |
| persons        |
| student        |
+----------------+

mysql> drop table student;

mysql> show tables;
+----------------+
| tables_in_demo |
+----------------+
| citys          |
| familys        |
| mytable        |
| names          |
| persons        |
+----------------+

-- 清空数据,保留表结构
mysql> select * from names;
+----+--------+
| id | name   |
+----+--------+
|  1 | 李四   |
|  2 | 张林   |
|  3 | 斗舞   |
|  4 | 名天   |
|  5 | 高兴   |
|  6 | 王二   |
+----+--------+

mysql> truncate table names;

mysql> select * from names;
empty set (0.00 sec)

mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+


-- 添加列
mysql> alter table names add alias varchar(32);

mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
| alias | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 删除列
mysql> alter table names drop column alias;

mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(32) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 修改列属性
mysql> alter table names modify column name varchar(64);

mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(64) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 自增删除auto_incremen
mysql> alter table names modify column id int;
mysql> desc names;
+-------+-------------+------+-----+---------+-------+
| field | type        | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | no   | pri | null    |       |
| name  | varchar(64) | yes  |     | null    |       |
+-------+-------------+------+-----+---------+-------+

-- 添加自增属性
mysql> alter table names modify id int auto_increment;

mysql> desc names;
+-------+-------------+------+-----+---------+----------------+
| field | type        | null | key | default | extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | no   | pri | null    | auto_increment |
| name  | varchar(64) | yes  |     | null    |                |
+-------+-------------+------+-----+---------+----------------+

-- 添加时间字段
mysql> alter table names add column birthday date;

mysql> desc names;
+----------+-------------+------+-----+---------+----------------+
| field    | type        | null | key | default | extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | no   | pri | null    | auto_increment |
| name     | varchar(64) | yes  |     | null    |                |
| birthday | date        | yes  |     | null    |                |
+----------+-------------+------+-----+---------+----------------+

mysql> insert into names(name, birthday) values('tom', '2018-2-12');

mysql> select * from names;
+----+------+------------+
| id | name | birthday   |
+----+------+------------+
|  1 | tom  | 2018-02-12 |
+----+------+------------+




-- null查询
mysql> select * from names;
+----+------+------------+
| id | name | birthday   |
+----+------+------------+
|  1 | tom  | 2018-02-12 |
|  2 | jack | null       |
|  3 | jimi | null       |
+----+------+------------+

mysql> select * from names where birthday is null;
+----+------+----------+
| id | name | birthday |
+----+------+----------+
|  2 | jack | null     |
|  3 | jimi | null     |
+----+------+----------+

-- 函数
-- 平均值
mysql> select * from persons;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
|  5 | 周五   |   22 | 深圳   |
|  6 | 郑旺   |   20 | 重庆   |
+----+--------+------+--------+
5 rows in set (0.00 sec)

mysql> select avg(age) from persons;
+----------+
| avg(age) |
+----------+
|  22.0000 |
+----------+


-- 计数
mysql> select count(age) from persons;
+------------+
| count(age) |
+------------+
|          5 |
+------------+

-- 取第一个
mysql> select * from persons limit 1;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
+----+--------+------+--------+


-- max min sum
mysql> select min(age) from persons;
+----------+
| min(age) |
+----------+
|       20 |
+----------+
1 row in set (0.00 sec)

mysql> select max(age) from persons;
+----------+
| max(age) |
+----------+
|       24 |
+----------+
1 row in set (0.00 sec)

mysql> select sum(age) from persons;
+----------+
| sum(age) |
+----------+
|      110 |
+----------+

-- 分组

mysql> select * from persons;
+----+--------+------+--------+
| id | name   | age  | city   |
+----+--------+------+--------+
|  2 | 李四   |   21 | 上海   |
|  3 | 赵倩   |   24 | 北京   |
|  4 | 孙力   |   23 | 天津   |
|  5 | 周五   |   22 | 深圳   |
|  6 | 郑旺   |   20 | 重庆   |
|  7 | 刘颖   |   20 | 北京   |
|  8 | 王大   |   20 | 天津   |
|  9 | 张开   |   20 | 深圳   |
| 10 | 留取   |   20 | 重庆   |
+----+--------+------+--------+

mysql> select city,sum(age) from persons group by city;
+--------+----------+
| city   | sum(age) |
+--------+----------+
| 上海   |       21 |
| 北京   |       44 |
| 天津   |       43 |
| 深圳   |       42 |
| 重庆   |       40 |
+--------+----------+

mysql> select city, sum(age) from persons group by city having sum(age)>40;
+--------+----------+
| city   | sum(age) |
+--------+----------+
| 北京   |       44 |
| 天津   |       43 |
| 深圳   |       42 |
+--------+----------+

-- ucase转换为大写 
mysql> select * from names;
+----+------+------------+
| id | name | birthday   |
+----+------+------------+
|  1 | tom  | 2018-02-12 |
|  2 | jack | null       |
|  3 | jimi | null       |
+----+------+------------+
3 rows in set (0.00 sec)

mysql> select ucase(name), birthday from names;
+-------------+------------+
| ucase(name) | birthday   |
+-------------+------------+
| tom         | 2018-02-12 |
| jack        | null       |
| jimi        | null       |
+-------------+------------+

-- lcase 转小写
mysql> select lcase(name), birthday from names;
+-------------+------------+
| lcase(name) | birthday   |
+-------------+------------+
| tom         | 2018-02-12 |
| jack        | null       |
| jimi        | null       |
+-------------+------------+

-- mid截取字符
mysql> select mid(name, 1, 2), birthday from names;
+-----------------+------------+
| mid(name, 1, 2) | birthday   |
+-----------------+------------+
| to              | 2018-02-12 |
| ja              | null       |
| ji              | null       |
+-----------------+------------+

-- round保留小数位
mysql> select avg(age) from persons;
+----------+
| avg(age) |
+----------+
|  21.1111 |
+----------+
1 row in set (0.00 sec)

mysql> select round(avg(age),1) from persons;
+-------------------+
| round(avg(age),1) |
+-------------------+
|              21.1 |
+-------------------+

-- now() 返回当前时间
mysql> select name,now() from names;
+------+---------------------+
| name | now()               |
+------+---------------------+
| tom  | 2018-05-03 10:49:09 |
| jack | 2018-05-03 10:49:09 |
| jimi | 2018-05-03 10:49:09 |
+------+---------------------+