1、在启动 MySQL 服务后,输入以下格式的命令:
mysql -h 主机名 -u 用户名 –p
2、 DDL:数据定义语言
DML:数据操作语言
DQL: 数据查询语言
DCL: 数据控制语言
TPL: 事务处理语言
# 查看DBMS支持的字符集
show charset;
# 查看DBMS支持的collation
show collation;
# 创建一个名称为mydb1的数据库。
create database mydb1;
create database if not exists mydb1;
# 创建一个使用gbk字符集的mydb2数据库。
create database if not exists mydb2 character set gbk;
# 创建一个使用gbk字符集,并带校对规则(校对集)的mydb3数据库。
create database if not exists mydb3 character set gbk collate gbk_bin;
# 查看所有的数据库
show databases;
# 查看数据库的创建语句
show create database mydb2;
# 删除前面创建的mydb3数据库
drop database mydb3;
drop database if exists mydb3;
# 把mydb2的字符集修改为utf8;
alter database mydb2 character set utf8 collate utf8_bin;
# 进入数据库
use mydb1;
数据类型
1、 整数类型
数据类型 | 占用字节 | 说明 |
TINYINT ~ byte(java) | 1 | 很小的整数 |
SMALLINT ~short(java) | 2 | 小的整数 |
MEDIUMINT | 3 | 中等大小的整数 |
INT ~int(java) | 4 | 普通大小的整数 |
BIGINT ~long(java) | 8 | 大整数 |
2.浮点数类型和定点数类型
类型名称 | 占用字节 | 说明 |
FLOAT(M,D) | 4 | 单精度浮点数 |
DOUBLE(M,D) | 8 | 双精度浮点数 |
DECIMAL(M,D) | M+2 | 定点数 |
M 称为精度,表示总共的位数,即整数位+小数位;
D 称为标度,表示小数的位数。
DECIMAL 类型不同于 FLOAT & DOUBLE,DECIMAL 实际以字符串存放,存储空间并不固定,而是由精度 M 决定的。不存在精度损失问题。~BigDecimal(java中)
3、日期与时间类型
类型名称 | 日期格式 | 占用字节 |
YEAR | YYYY (2018) | 1 |
TIME | HH:MM:SS (10:20:00) | 3 |
DATE | YYYY-MM-DD (2018-7-23) | 3 |
DATETIME | YYYY-MM-DD HH:MM:SS | 8 |
TIMESTAMP | YYYY-MM-DD HH:MM:SS | 4 |
DATETIME 和TIMESTAMP 比较:
- DATETIME 的系统默认值是 NULL, 而 TIMESTAMP 的系统默认值是当前时间 NOW();
- timestamp存储需要四个字节,它的取值范围为“1970-01-01 00:00:01” UTC ~ “2038-01-19 03:14:07” (和时区有关)datetime取值范围为“1000-01-01 00:00:00” ~ “9999-12-31 23:59:59”(和时区无关,怎么存入怎么返回,对程序员友好)
- timestamp时间是根据时区来显示的。例如,在东八区插入的timestamp类型为2009-09-30 14:21:25,在东七区显示时,时间部门就变成了13:21:25,在东九区显示时,时间部门就变成了15:21:25。
- 需要显示日期与时间,timestamp类型需要根据不同地区的时区来转换时间,但是,timestamp类型的范围太小,其最大时间为2038-01-19 11:14:07。如果插入时间的比这个大,将会数据库插入0000-00-00 00:00:00。所以需要的时间范围比较大,还是选择dateTime类型比较安全。
-
create table t_datatime ( a datetime, b timestamp ); insert into t_datatime (b) values (now()); insert into t_datatime (a) values (now()); select * from t_datatime; # 2. DATETIME 存储的时间与时区无关,而 TIMESTAMP 与时区有关。 set time_zone='+10:00'; select * from t_datatime; set time_zone='+8:00';
4.•字符串类型
类型名称 | 占用字节 | 说明 |
CHAR(M) | M, 1 <= M <= 255 | 固定长度字符串,CHAR(M),占固定M字节 |
VARCHAR(M) | L+1, L <=M, 1 <=M <=255 | 变长字符串M:最大的字符长度 |
TINYTEXT | L+1, L < 2^8 | 非常小的文本字符串 |
TEXT | L+2, L < 2^16 | 小的文本字符串 |
MEDIUMTEXT | L+3, L < 2^24 | 中等大小的文本字符串 |
LONGTEXT | L+4, L < 2^32 | 大的文本字符串 |
ENUM | 1 或者 2个字节,取决于枚举的数目,最大 65535个 | 枚举类型 |
SET | 1,2,3,4或8个字节 | 集合类型 |
枚举类型enum 只能取一个值
ENUM 类型总有一个默认值,当ENUM 列声明为NULL,则默认值为NULL。如果 ENUM 列被声明为 NOT NULL,则其默认值为列表的第一个元素。
# enum 枚举类型
use mydb1;
create table t_enum (
`gender` enum('female', 'male'),
`sex` enum('f', 'm', 'u') not null
);
insert into t_enum (gender) values ('female');#female f
insert into t_enum (sex) values ('m');#null m
select * from t_enum;
set 集合类型 可以多个值组合
# set 集合类型
create table t_set (
`level` set('a', 'b', 'c', 'd')
);
insert into t_set values (null);
insert into t_set values ('a');
insert into t_set values ('a,b,c');
insert into t_set values ('a,b,c,f');
select * from t_set;
表
1.查看数据库中有哪些表 show tables;
2、 查看表的创建语句
3、查看表的结构
4、修改表的结构
增加一列
修改字段的数据类型和约束
修改字段名称
删除一列
修改表的名称
修改表的字符集
删除表
use mydb1;
# 创建一个员工表(id, name, gender, birhtday, entry_date, job, salary, resume);
create table t_employees (
id int primary key,
`name` varchar(255) not null,
gender enum('female', 'male'),
birthday date,
entry_date date,
job varchar(255),
salary decimal(10, 2),
`resume` longtext
);
show tables;
# 查看数据库中有哪些表
show tables;
# 查看表的创建语句
show create table t_employees;
# 查看表的结构
desc t_employees;
describe t_employees;
# 修改表的结构
# 1. 增加一列
alter table t_employees add image blob;
alter table t_employees add user_name varchar(255) after id;
alter table t_employees add uid int first;
# 2. 修改字段的数据类型和约束
alter table t_employees modify resume varchar(255) not null;
# 3. 修改字段的名称
alter table t_employees change gender sex enum('femal', 'male');
# 4. 删除一列
alter table t_employees drop uid;
# 5. 修改表的名称
rename table t_employees to employees;
show tables;
# 6. 移动该表到另外一个数据库
rename table employees to mydb2.t_employees;
show tables in mydb2;
# 7 修改表的字符集
use mydb2;
alter table t_employees character set gbk collate gbk_bin;
show create table t_employees;
# 删除表
drop table t_employees;
show tables;
DML
###################################### DML ###########################################
# 插入 t_employees (id, name, gender, birhtday, entry_date, job, salary, resume);
insert into t_employees values (1, '王', 'male', '1890-1-1', '2000-02-02', '教导主任', 10000000, null);
insert into t_employees (id, name, gender) values (2, '李', 'male');
insert into t_employees (id, name, gender) values(3, '孙', 'male'),(4, '秦', 'male');
insert into employees (select * from t_employees);
select * from employees;
drop table employees;
# 修改 update
# 1. 将所有员工薪水修改为5000元。
update t_employees set salary = 5000;
# 2. 将姓名为‘李’的员工薪水修改为3000元。
update t_employees set salary = 3000 where name = '李';
select * from t_employees;
# 3. 将姓名为’lisi’的员工薪水修改为4000元, job改为ccc。
update t_employees set salary = 4000, job='ccc' where name = 'lisi';
# 4. 将‘孙’的薪水在原有基础上增加10000元。
update t_employees set salary = salary + 10000 where name = '孙';
语法总结
DDL(data definition language)数据定义语言
(1)数据库
创建:
create database 数据库名称 if not exists 数据库名称 character set 字符集 名称 collate 校对规则;
查询:
show databases;
show create database 数据库名称;
修改:
alter database 数据库名称 character set 字符集名称;
删除:
drop database 数据库名称 if exists 数据库名称;
使用数据库:use 数据库名称
(2)表
创建:
create table 表名 (
列名1 数据类型1,
列名2 数据类型2,
…
列名n 数据类型n)CHARACTER SET 字符集 COLLATE 校对规则
查询:
show tables;
desc 表名;
修改:
alter table 表名 rename to 新的表名;
rename table 表名 to 数据库名.表名;
alter table 表名 character set 字符集名称;
alter table 表名 add 列名 数据类型;
alter table 表名 change 列名 新列名 新数据类型;
alter table 表名 modify 列名 新数据类型;
删除:
alter table 表名 drop 列名;
drop table 表名;
2、DML(data manipulation language)数据操作语言
添加数据:
insert into 表名(列名1,列名2,…列名n)values(值1,值2,…值n);
insert into 表名 values(值1,值2,…值n),(值1,值2,…值n)...;
insert into 表名 (select * from 表名);
删除数据:
1)行
delete from 表名 where 条件;
2)列
update 表名 set 列名1=null;
修改数据:
update 表名 set 列名1=值1,列名2=值2,…where 条件;
# 11.2 sum(),
select sum(hp_max) from heros; -- 不统计null值
select sum(name) from heros; -- 计算时,字符串如果不能转换成整数,值为0
字符串之间可以进行比较,但是不能进行计算。
# 11.4 max()
select max(hp_max) from heros; -- 不统计null行
select max(name) from heros;--字符串可以进行比较
# 12.1 搭配聚合函数
select role_main, count(*) as `count` from heros group by role_main; --注意底层运行顺序!