SQL数据库操作指令

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: SQL数据库操作指令

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 |
+------+---------------------+

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3天前
|
SQL 存储 数据管理
SQL Server数据库
SQL Server数据库
16 11
|
10天前
|
SQL 安全 数据库
基于SQL Server事务日志的数据库恢复技术及实战代码详解
基于事务日志的数据库恢复技术是SQL Server中一个非常强大的功能,它能够帮助数据库管理员在数据丢失或损坏的情况下,有效地恢复数据。通过定期备份数据库和事务日志,并在需要时按照正确的步骤恢复,可以最大限度地减少数据丢失的风险。需要注意的是,恢复数据是一个需要谨慎操作的过程,建议在执行恢复操作之前,详细了解相关的操作步骤和注意事项,以确保数据的安全和完整。
26 0
|
14天前
|
前端开发 C# 设计模式
“深度剖析WPF开发中的设计模式应用:以MVVM为核心,手把手教你重构代码结构,实现软件工程的最佳实践与高效协作”
【8月更文挑战第31天】设计模式是在软件工程中解决常见问题的成熟方案。在WPF开发中,合理应用如MVC、MVVM及工厂模式等能显著提升代码质量和可维护性。本文通过具体案例,详细解析了这些模式的实际应用,特别是MVVM模式如何通过分离UI逻辑与业务逻辑,实现视图与模型的松耦合,从而优化代码结构并提高开发效率。通过示例代码展示了从模型定义、视图模型管理到视图展示的全过程,帮助读者更好地理解并应用这些模式。
30 0
|
14天前
|
SQL 数据处理 数据库
|
14天前
|
SQL 存储 调度
|
14天前
|
SQL 安全 数据库
|
14天前
|
Java 数据库连接 数据库
告别繁琐 SQL!Hibernate 入门指南带你轻松玩转 ORM,解锁高效数据库操作新姿势
【8月更文挑战第31天】Hibernate 是一款流行的 Java 持久层框架,简化了对象关系映射(ORM)过程,使开发者能以面向对象的方式进行数据持久化操作而无需直接编写 SQL 语句。本文提供 Hibernate 入门指南,介绍核心概念及示例代码,涵盖依赖引入、配置文件设置、实体类定义、工具类构建及基本 CRUD 操作。通过学习,你将掌握使用 Hibernate 简化数据持久化的技巧,为实际项目应用打下基础。
35 0
|
14天前
|
SQL 存储 监控
|
14天前
|
SQL 数据库 Java
HQL vs SQL:谁将统治数据库查询的未来?揭秘Hibernate的神秘力量!
【8月更文挑战第31天】Hibernate查询语言(HQL)是一种面向对象的查询语言,它模仿了SQL的语法,但操作对象为持久化类及其属性,而非数据库表和列。HQL具有类型安全、易于维护等优点,支持面向对象的高级特性,内置大量函数,可灵活处理查询结果。下面通过示例对比HQL与SQL,展示HQL在实际应用中的优势。例如,HQL查询“从员工表中筛选年龄大于30岁的员工”只需简单地表示为 `FROM Employee e WHERE e.age &gt; 30`,而在SQL中则需明确指定表名和列名。此外,HQL在处理关联查询时也更为直观易懂。然而,对于某些复杂的数据库操作,SQL仍有其独特优势。
25 0
|
14天前
|
Java UED 开发者
当错误遇上Struts 2:一场优雅的异常处理盛宴,如何让错误信息成为用户体验的救星?
【8月更文挑战第31天】在Web应用开发中,异常处理对确保用户体验和系统稳定性至关重要。Struts 2 提供了完善的异常处理机制,包括 `exception` 拦截器、`ActionSupport` 类以及 OGNL 表达式,帮助开发者优雅地捕获和展示错误信息。本文详细介绍了 Struts 2 的异常处理策略,涵盖拦截器配置、错误信息展示及自定义全局异常处理器的实现方法,使应用程序更加健壮和用户友好。
22 0