SQL数据库操作指令

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 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 |
+------+---------------------+

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
24天前
|
SQL 机器学习/深度学习 人工智能
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
本文系统性地阐述了自然语言转SQL(NL2SQL) 技术如何让非技术背景的业务分析师实现数据自助查询,从而提升数据驱动决策的效率与准确性。
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
|
2月前
|
SQL 缓存 监控
SqlRest让SQL秒变Http API,还支持20+数据库(含国产数据库)
杭州奥零数据科技有限公司成立于2023年,专注于数据中台业务,维护开源项目AllData并提供商业版解决方案。AllData提供数据集成、存储、开发、治理及BI展示等一站式服务,支持AI大模型应用,助力企业高效利用数据价值。
|
2月前
|
SQL 存储 数据库
SQL Server Management Studio (SSMS) 21 - 微软数据库管理工具
SQL Server Management Studio (SSMS) 21 - 微软数据库管理工具
441 0
|
2月前
|
SQL XML Java
配置Spring框架以连接SQL Server数据库
最后,需要集成Spring配置到应用中,这通常在 `main`方法或者Spring Boot的应用配置类中通过加载XML配置或使用注解来实现。
226 0
|
5月前
|
SQL 关系型数据库 MySQL
大数据新视界--大数据大厂之MySQL数据库课程设计:MySQL 数据库 SQL 语句调优方法详解(2-1)
本文深入介绍 MySQL 数据库 SQL 语句调优方法。涵盖分析查询执行计划,如使用 EXPLAIN 命令及理解关键指标;优化查询语句结构,包括避免子查询、减少函数使用、合理用索引列及避免 “OR”。还介绍了索引类型知识,如 B 树索引、哈希索引等。结合与 MySQL 数据库课程设计相关文章,强调 SQL 语句调优重要性。为提升数据库性能提供实用方法,适合数据库管理员和开发人员。
|
11月前
|
SQL 开发框架 .NET
ASP.NET连接SQL数据库:详细步骤与最佳实践指南ali01n.xinmi1009fan.com
随着Web开发技术的不断进步,ASP.NET已成为一种非常流行的Web应用程序开发框架。在ASP.NET项目中,我们经常需要与数据库进行交互,特别是SQL数据库。本文将详细介绍如何在ASP.NET项目中连接SQL数据库,并提供最佳实践指南以确保开发过程的稳定性和效率。一、准备工作在开始之前,请确保您
677 3
|
6月前
|
SQL 数据库连接 Linux
数据库编程:在PHP环境下使用SQL Server的方法。
看看你吧,就像一个调皮的小丑鱼在一片广阔的数据库海洋中游弋,一路上吞下大小数据如同海中的珍珠。不管有多少难关,只要记住这个流程,剩下的就只是探索未知的乐趣,沉浸在这个充满挑战的数据库海洋中。
135 16
|
6月前
|
SQL 关系型数据库 MySQL
如何优化SQL查询以提高数据库性能?
这篇文章以生动的比喻介绍了优化SQL查询的重要性及方法。它首先将未优化的SQL查询比作在自助餐厅贪多嚼不烂的行为,强调了只获取必要数据的必要性。接着,文章详细讲解了四种优化策略:**精简选择**(避免使用`SELECT *`)、**专业筛选**(利用`WHERE`缩小范围)、**高效联接**(索引和限制数据量)以及**使用索引**(加速搜索)。此外,还探讨了如何避免N+1查询问题、使用分页限制结果、理解执行计划以及定期维护数据库健康。通过这些技巧,可以显著提升数据库性能,让查询更高效流畅。
|
7月前
|
SQL 存储 关系型数据库
【SQL技术】不同数据库引擎 SQL 优化方案剖析
不同数据库系统(MySQL、PostgreSQL、Doris、Hive)的SQL优化策略。存储引擎特点、SQL执行流程及常见操作(如条件查询、排序、聚合函数)的优化方法。针对各数据库,索引使用、分区裁剪、谓词下推等技术,并提供了具体的SQL示例。通用的SQL调优技巧,如避免使用`COUNT(DISTINCT)`、减少小文件问题、慎重使用`SELECT *`等。通过合理选择和应用这些优化策略,可以显著提升数据库查询性能和系统稳定性。
238 9
|
8月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
835 6

热门文章

最新文章