MySQL篇
常用操作
mysqld --console 启动MySQL服务器
mysqladmin -uroot shutdown 关闭MySQL服务器
mysql -u <user> -p 登录到数据库
select user from mysql.user; 查询用户
select host,user,authentication_string from user; 查询用户连接方式密码
基本上手操作相关语句
// 查询用户
select user from mysql.user;
执行结果:
+---------------+
| user |
+---------------+
| mysql.session |
| mysql.sys |
| root |
+---------------+
3 rows in set (0.00 sec)
// 查询用户
select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| % | maoyu | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
// 新增用户
create user maoyu@'%' identified by '123456';
// 给用户增加操作权限
// GRANT ALL(代表全部权限) ON 数据库.表 to 用户@访问方式
GRANT ALL PRIVILEGES ON maoyu.* to 'maoyu'@'%';
// 刷新权限
FLUSH PRIVILEGES;
// 查询用户和密码
select user,authentication_string from mysql.user;
+---------------+-------------------------------------------+
| user | authentication_string |
+---------------+-------------------------------------------+
| root | |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
// 查看当前拥有的数据库
show databases;
执行结果:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
// 创建数据库 create database 数据库名称
create database maoyu;
执行结果:
Query OK, 1 row affected (0.00 sec)
// 切换数据库 use 数据库
use maoyu;
执行结果:
Database changed
// 创建表 设置主键 create table 表名 (字段名 类型 primary key:设置主键 not nul:非空)
create table student (id INT primary key, name VARCHAR(10) not null,age TINYINT);
// 遍历表格
show tables;
执行结果:
+-----------------+
| Tables_in_maoyu |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
// 查看表格结构 desc 表名
desc student;
执行结果:
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
// 创建表格设置主键自增
// primary key:主键 auto_increment:自增 not null:非空
create table demo0 (id INT primary key auto_increment,name VARCHAR(10)not null);
表格结构:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
// 创建表格设置主键自增,设置默认值
// default: 默认值
create table demo1 (id INT primary key auto_increment ,
name VARCHAR(10) not null,
age TINYINT default 18);
表格结构:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| age | tinyint(4) | YES | | 18 | |
+-------+-------------+------+-----+---------+----------------+
// 删除表 drop table 表名
drop table demo0;
// 插入数据 insert into 表名 value (对应列字段的值)
insert into student values (1,'hello',15);
执行结果:
Query OK, 1 row affected (0.01 sec)
// 插入数据 insert into 表名 (字段) value (对应列字段的值)
insert into student (id,name,age) values (2,'word',16);
// 插入数据
insert into student (id,name) values (3,'phone');
// 查询表 select 列名 from 表名;
select * from student;
执行结果:
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | phone | NULL |
+----+-------+------+
3 rows in set (0.00 sec)
// 查询表
select id,name from student;
+----+-------+
| id | name |
+----+-------+
| 1 | hello |
| 2 | word |
| 3 | phone |
+----+-------+
// 更改数据 update 表名 set 列名 where 条件表达式
update student set age=17 where id=3;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | phone | 17 |
+----+-------+------+
// 删除表中一条数据 delete from 表明 where 条件表达式
delete from student where id=3;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
+----+-------+------+
MySQL提升
// 查询 + AS: 字段别名
select id as '索引',name as '名称' from student;
+------+-------+
| 索引 | 名称 |
+------+-------+
| 1 | hello |
| 2 | word |
+------+-------+
// 多表查询 student表和contact表
1. select student.id,student.name,contact.phone from student,contact;
2. select student.*,contact.phone from student,contact;
+----+-------+--------+
| id | name | phone |
+----+-------+--------+
| 1 | hello | 123456 |
| 2 | word | 123456 |
| 1 | hello | 456321 |
| 2 | word | 456321 |
+----+-------+--------+
// 多表查询 + 条件显示
select student.*,contact.phone from student,contact where student.name=contact.name;
+----+-------+------+--------+
| id | name | age | phone |
+----+-------+------+--------+
| 1 | hello | 15 | 123456 |
| 2 | word | 16 | 456321 |
+----+-------+------+--------+
// 多行插入
insert into mobile value ('sony',600,800),('samsung',900,1300);
+---------+-----------+--------------+
| name | costPrice | sellingPrice |
+---------+-----------+--------------+
| sony | 600 | 800 |
| samsung | 900 | 1300 |
+---------+-----------+--------------+
// 查询 + 别名 + 计算
select *,sellingPrice-costPrice as '盈利' from mobile;
+---------+-----------+--------------+------+
| name | costPrice | sellingPrice | 盈利 |
+---------+-----------+--------------+------+
| sony | 600 | 800 | 200 |
| samsung | 900 | 1300 | 400 |
+---------+-----------+--------------+------+
// 查询 + 别名 + 聚合函数MAX()
select max(costPrice) as '最高成本价' from mobile;
+------------+
| 最高成本价 |
+------------+
| 900 |
+------------+
// 查询 + 别名 + 聚合函数MIN()
select min(costPrice) as '最低成本价' from mobile;
+------------+
| 最低成本价 |
+------------+
| 600 |
+------------+
// 查询 + DISTINCT:去重(去除重复)
select distinct * from contact;
+-------+--------+
| name | phone |
+-------+--------+
| hello | 123456 |
| word | 456321 |
+-------+--------+
去重之前:
+-------+--------+
| name | phone |
+-------+--------+
| hello | 123456 |
| word | 456321 |
| hello | 123456 |
+-------+--------+
// 查询 + LIMIT: 显示N条
select * from student limit 2;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
+----+-------+------+
// 查询 + 从N条显示到M条
select * from student limit 2,4;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 3 | windows | 17 |
| 4 | mac | 18 |
+----+---------+------+
// 查询 + 别名 + 计算 + 常量
select *,(sellingPrice-costPrice)*0.9 as '纳税之后' from mobile;
+---------+-----------+--------------+----------+
| name | costPrice | sellingPrice | 纳税之后 |
+---------+-----------+--------------+----------+
| sony | 600 | 800 | 180.0 |
| samsung | 900 | 1300 | 360.0 |
+---------+-----------+--------------+----------+
// 查询 + 无名列
select 1+2+3;
+-------+
| 1+2+3 |
+-------+
| 6 |
+-------+
// 查询 + 无名列 + 别名
select 1+2+3 as '结果';
+------+
| 结果 |
+------+
| 6 |
+------+
// 查询 + BETWEEN: 范围查询
select * from student where id between 1 and 3;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
+----+---------+------+
// 查询 + OR: 逻辑或
select * from student where id=1 or id=2;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
+----+-------+------+
// 查询 + AND: 逻辑与
select * from student where id>1 and age>15;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
+----+---------+------+
// 查询 + NOT: 逻辑非
select * from student where not id=1;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
+----+---------+------+
// 查询 + 模糊匹配
select * from student where name like 'w%';
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
+----+---------+------+
// 查询 + 模糊匹配 + 逻辑或
select * from student where name like 'w%' or name like 'h%';
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
+----+---------+------+
// 查询 + 集合 + 条件过滤
select * from student where id in (2,3);
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
+----+---------+------+
// 查询 + 集合 + 逻辑非
select * from student where id not in (1,2);
+----+---------+------+
| id | name | age |
+----+---------+------+
| 3 | windows | 17 |
| 4 | mac | 18 |
+----+---------+------+
// 查询 + 当前时间
select now();
+---------------------+
| now() |
+---------------------+
| 2020-01-30 23:10:41 |
+---------------------+
// 查询 + 当前时间 + 格式化 + DATE_FORMAT()
select date_format(now(),'%Y-%c-%d');
+-------------------------------+
| date_format(now(),'%Y-%c-%d') |
+-------------------------------+
| 2020-1-30 |
+-------------------------------+
// 查询 + 模糊匹配 %: 由0个或多个字符组成的任意字符串
select * from student where name like 'w%';
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
+----+---------+------+
// 查询 + 模糊匹配 _: 任意单个字符
select * from student where name like 'w_rd';
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | word | 16 |
+----+------+------+
// 查询 + 模糊匹配 + 定义转义字符
select * from student where name like 'demo%' escape '#';
+----+-------+------+
| id | name | age |
+----+-------+------+
| 5 | demo% | 19 |
+----+-------+------+
// 查询 + 空值
select * from demo where name is null;
+------+------+
| id | name |
+------+------+
| 0 | NULL |
+------+------+
// 查询 + 非空值
select * from demo where name is not null;
+------+-------+
| id | name |
+------+-------+
| 1 | zhang |
+------+-------+
// 查询 + 处理空值
select *,isnull(name) as '是否为空值' from demo;
+------+-------+------------+
| id | name | 是否为空值 |
+------+-------+------------+
| 0 | NULL | 1 |
| 1 | zhang | 0 |
+------+-------+------------+
// 查询 + 升序排序 ORDER BY: 排序 字段 ASC: 升序
select * from student order by age asc;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | LiSi | 10 |
| 6 | ZhangSan | 12 |
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
+----+----------+------+
// 查询 + 降序排序 DESC: 降序
select * from student order by age desc;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | demo% | 19 |
| 4 | mac | 18 |
| 3 | windows | 17 |
| 2 | word | 16 |
| 1 | hello | 15 |
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
+----+----------+------+
// 查询 + 多列排序: 先按age排序,如果age相同则按照id排序
select * from student order by age,id;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | LiSi | 10 |
| 6 | ZhangSan | 12 |
| 1 | hello | 15 |
| 8 | WangWu | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 9 | NieFeng | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
+----+----------+------+
// 查询 + 多列排序 + 主排列与次排列
select * from student order by age asc,id desc;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | LiSi | 10 |
| 6 | ZhangSan | 12 |
| 8 | WangWu | 15 |
| 1 | hello | 15 |
| 2 | word | 16 |
| 9 | NieFeng | 17 |
| 3 | windows | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
+----+----------+------+
// 查询 + 聚合函数SUM(): 对指定列的所有非空值求和 支持类型: 数字
select sum(age) from student;
+----------+
| sum(age) |
+----------+
| 139 |
+----------+
// 查询 + 聚合函数AVG(): 对指定列中的所有非空值求平均值 支持类型:数字
select avg(age) from student;
+----------+
| avg(age) |
+----------+
| 15.4444 |
+----------+
// 查询 + 聚合函数COUNT(): 统计结果集中全部记录行的数量
select count(*) from student;
+----------+
| count(*) |
+----------+
| 9 |
+----------+
// 查询 + 聚合函数AVG() + 条件限制
select avg(age) from student where age>15;
+----------+
| avg(age) |
+----------+
| 17.4000 |
+----------+
// 查询 + 聚合函数AVG() + 条件限制 + 去除小数尾0
select cast(avg(age) as char)+0 from student where age>15;
+--------------------------+
| cast(avg(age) as char)+0 |
+--------------------------+
| 17.4 |
+--------------------------+
// 查询 + 聚合函数AVG() + 条件限制: 子查询
select * from student where (age > (select avg(age) from student ) );
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
| 9 | NieFeng | 17 |
+----+---------+------+
// 查询 + 聚合函数COUNT() + 条件限制
select count(*) from student where age>15;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
// 查询 + 聚合函数SUM():动态求值
select sum(sellingPrice - costPrice) as '盈利' from mobile;
+------+
| 盈利 |
+------+
| 600 |
+------+
// 查询 + 子查询 + 多表
select *,(select avg(student.age) from student) from contact;
+-------+--------+----------------------------------------+
| name | phone | (select avg(student.age) from student) |
+-------+--------+----------------------------------------+
| word | 456321 | 15.4444 |
| hello | 123456 | 15.4444 |
+-------+--------+----------------------------------------+
// 查询 + 子查询 + 动态求值 + 聚合函数AVG()
select * from student where age>(select avg(age) from student);
+----+---------+------+
| id | name | age |
+----+---------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
| 9 | NieFeng | 17 |
+----+---------+------+
// 查询 + 多表连接
select student.*,contact.* from contact,student;
+----+----------+------+-------+--------+
| id | name | age | name | phone |
+----+----------+------+-------+--------+
| 1 | hello | 15 | word | 456321 |
| 1 | hello | 15 | hello | 123456 |
| 2 | word | 16 | word | 456321 |
| 2 | word | 16 | hello | 123456 |
| 3 | windows | 17 | word | 456321 |
| 3 | windows | 17 | hello | 123456 |
| 4 | mac | 18 | word | 456321 |
| 4 | mac | 18 | hello | 123456 |
| 5 | demo% | 19 | word | 456321 |
| 5 | demo% | 19 | hello | 123456 |
| 6 | ZhangSan | 12 | word | 456321 |
| 6 | ZhangSan | 12 | hello | 123456 |
| 7 | LiSi | 10 | word | 456321 |
| 7 | LiSi | 10 | hello | 123456 |
| 8 | WangWu | 15 | word | 456321 |
| 8 | WangWu | 15 | hello | 123456 |
| 9 | NieFeng | 17 | word | 456321 |
| 9 | NieFeng | 17 | hello | 123456 |
+----+----------+------+-------+--------+
// 查询 + 多表连接 + 条件限制
select student.*,contact.phone from student,contact where contact.name=student.name;
+----+-------+------+--------+
| id | name | age | phone |
+----+-------+------+--------+
| 1 | hello | 15 | 123456 |
| 2 | word | 16 | 456321 |
+----+-------+------+--------+
// 查询 + 内连接
内连接: 组合两个表中的记录,返回关联字段相符的记录,也就是返回两个表的交集(阴影)部分。
select * from student inner join contact on student.name=contact.name;
+----+-------+------+-------+--------+
| id | name | age | name | phone |
+----+-------+------+-------+--------+
| 1 | hello | 15 | hello | 123456 |
| 2 | word | 16 | word | 456321 |
+----+-------+------+-------+--------+
// 查询 + 左外连接
左外连接: 左(外)连接,左表(a_table)的记录将会全部表示出来,
而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。
select * from student left join contact on student.name=contact.name;
+----+----------+------+-------+--------+
| id | name | age | name | phone |
+----+----------+------+-------+--------+
| 2 | word | 16 | word | 456321 |
| 1 | hello | 15 | hello | 123456 |
| 3 | windows | 17 | NULL | NULL |
| 4 | mac | 18 | NULL | NULL |
| 5 | demo% | 19 | NULL | NULL |
| 6 | ZhangSan | 12 | NULL | NULL |
| 7 | LiSi | 10 | NULL | NULL |
| 8 | WangWu | 15 | NULL | NULL |
| 9 | NieFeng | 17 | NULL | NULL |
+----+----------+------+-------+--------+
// 查询 + 右外连接
右外连接: 与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,
而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。
select * from student right join contact on student.name=contact.name;
+------+-------+------+-------+--------+
| id | name | age | name | phone |
+------+-------+------+-------+--------+
| 1 | hello | 15 | hello | 123456 |
| 2 | word | 16 | word | 456321 |
+------+-------+------+-------+--------+
// 查询 + 左外连接
select student.*,contact.phone from
student left join contact on student.name=contact.name;
+----+----------+------+--------+
| id | name | age | phone |
+----+----------+------+--------+
| 2 | word | 16 | 456321 |
| 1 | hello | 15 | 123456 |
| 3 | windows | 17 | NULL |
| 4 | mac | 18 | NULL |
| 5 | demo% | 19 | NULL |
| 6 | ZhangSan | 12 | NULL |
| 7 | LiSi | 10 | NULL |
| 8 | WangWu | 15 | NULL |
| 9 | NieFeng | 17 | NULL |
+----+----------+------+--------+
// 查询 + UNION: 组合查询
select * from student where id>5 union select * from student where name like 'w%';
+----+----------+------+
| id | name | age |
+----+----------+------+
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
| 8 | WangWu | 15 |
| 9 | NieFeng | 17 |
| 2 | word | 16 |
| 3 | windows | 17 |
+----+----------+------+
// 查询 + 组合查询 + 返回重复的行
select * from student where id>5 union all select * from student where name like 'w%';
+----+----------+------+
| id | name | age |
+----+----------+------+
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
| 8 | WangWu | 15 |
| 9 | NieFeng | 17 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 8 | WangWu | 15 |
+----+----------+------+
// 查询 + 组合查询 + 排序
select * from student where id>5 union
select * from student where name like 'w%' order by id asc;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | word | 16 |
| 3 | windows | 17 |
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
| 8 | WangWu | 15 |
| 9 | NieFeng | 17 |
+----+----------+------+
// 插入 + 查询 + 类型转换
insert into contact (name,phone) select name,cast(id+10000 as char) from student;
+----------+--------+
| name | phone |
+----------+--------+
| hello | 10001 |
| word | 456321 |
| hello | 123456 |
| word | 10002 |
| windows | 10003 |
| mac | 10004 |
| demo% | 10005 |
| ZhangSan | 10006 |
| LiSi | 10007 |
| WangWu | 10008 |
| NieFeng | 10009 |
+----------+--------+
// 创建表 + 拷贝
create table newTable as select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
| 8 | WangWu | 15 |
| 9 | NieFeng | 17 |
+----+----------+------+
// 更新 + 一行多列
update newTable set name='BuJingYun',age=22 where id=4;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | BuJingYun | 22 |
| 5 | demo% | 19 |
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
| 8 | WangWu | 15 |
| 9 | NieFeng | 17 |
+----+-----------+------+
// 删除 + 多行删除
delete from newTable where id in(9,8,7);
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | BuJingYun | 22 |
| 5 | demo% | 19 |
| 6 | ZhangSan | 12 |
+----+-----------+------+
// 创建视图
create view studentView as select * from student;
+-----------------+
| Tables_in_maoyu |
+-----------------+
| contact |
| demo |
| mobile |
| newtable |
| student |
| studentview |
+-----------------+
// 删除视图
drop view studentView;
+-----------------+
| Tables_in_maoyu |
+-----------------+
| contact |
| demo |
| mobile |
| newtable |
| student |
+-----------------+
// 创建视图 + 多表 + 条件限制
create view studentView as
select student.*,contact.phone from student,contact
where student.name=contact.name;
+----+----------+------+--------+
| id | name | age | phone |
+----+----------+------+--------+
| 1 | hello | 15 | 123456 |
| 2 | word | 16 | 10002 |
| 3 | windows | 17 | 10003 |
| 4 | mac | 18 | 10004 |
| 5 | demo% | 19 | 10005 |
| 6 | ZhangSan | 12 | 10006 |
| 7 | LiSi | 10 | 10007 |
| 8 | WangWu | 15 | 10008 |
| 9 | NieFeng | 17 | 10009 |
+----+----------+------+--------+
// 数据表修改 + 添加列 ALTER TABLE 表名 ADD 列名 类型
alter table demo add score int;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
// 数据表修改 + 修改列的数据类型 alter table 表名 modify column 列名 数据类型;
alter table demo modify column name char(10);
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
// 数据表修改 + 修改列名 + 修改数据类型 alter table 表名change column 旧列名 新列名 数据类型;
alter table demo change column score data int;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| data | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
// 数据表修改 + 添加主键
alter table demo add primary key(id);
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| data | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
// 数据表修改 + 删除列
alter table demo drop column data;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
// 数据表修改 + 添加自增: 自增的列必须先设为主键
alter table demo change id id int AUTO_INCREMENT;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+----------------+
// 数据表修改 + 字符修改: 支持中文
alter table 表名convert to character set utf8;
alter table student convert to character set utf8;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | hello | 15 |
| 2 | word | 16 |
| 3 | windows | 17 |
| 4 | mac | 18 |
| 5 | demo% | 19 |
| 6 | ZhangSan | 12 |
| 7 | LiSi | 10 |
| 8 | WangWu | 15 |
| 9 | NieFeng | 17 |
| 14 | 大猫鱼 | 25 |
+----+----------+------+
// 创建表 + 设置字符集: 支持中文
create table demo1(id int AUTO_INCREMENT primary key,
name char(20))character set = utf8;
有缘在更…