-- 单行注释
/*
多行注释
*/
- 创建一个数据库
create database 数据库名字;
-- 如果有就执行 没有不执行 不报错
create database if not exists 数据库名;
- 删除数据库
drop database 数据库名;
-- 如果有就执行 没有不执行 不报错
drop database if not exists 数据库名
- 查看当前数据库管理系统下有那些数据库
show databases;
- 切换数据库
use 数据库;
MySQL数据库中国常见的数据类型
-
数字类型
- 整数类型 : tinyint , smallint , int/integer
- 浮点类型 : float , double
- decimal
-
字符串类型(必须设hi字符串的运行的最大长度)
- char : 定长字符串
- varchar : 可变长度字符串
- text/longtext : 适合存储大字符串 , 在使用的时候不需要指定长度
-
日期类型
- data : 日期 , 用来存储年月日
- datatime : 用来存储年月日 , 时分秒 , 微秒
- time : 时间 , 用来存储分秒
-
布尔类型
- boolean : 使用 1 和 0 代表 true/false
-
Blob类型(流的存储)
- blob
- longblob
MySQL数据库中国常见的数据类型
-
数字类型
- 整数类型 : tinyint , smallint , int/integer
- 浮点类型 : float , double
- decimal
-
字符串类型(必须设hi字符串的运行的最大长度)
- char : 定长字符串
- varchar : 可变长度字符串
- text/longtext : 适合存储大字符串 , 在使用的时候不需要指定长度
-
日期类型
- data : 日期 , 用来存储年月日
- datatime : 用来存储年月日 , 时分秒 , 微秒
- time : 时间 , 用来存储分秒
-
布尔类型
- boolean : 使用 1 和 0 代表 true/false
-
Blob类型(流的存储)
- blob
- longblob
表的基本操作
表(table)使用进行数据存储的容器,表由字段(column) 和 记录 组成
表的创建
-- 选中表
use 表名;
create table 表名(
字段名 字段类型 [约束] [注释],
字段名 字段类型 [约束] [注释],
字段名 字段类型 [约束] [注释],
字段名 字段类型 [约束] [注释],
...
);
-- 示例
create table t_student(
name varchar(50) comment '姓名',
sex char(1) comment '性别',
birth date comment '出生日期',
stuNo varchar(50) comment '学号',
tel varchar(11) comment '手机号',
create_time datetime comment '入学时间'
);
表的删除
drop table 表名;
修改表的结构 alter
- 新增字段 add
alter table 表名 add [column] 字段名 字段类型 [约束] [注释];
-- 添加一个身份证号字段
alter table t_student add column card char(18) comment '身份证号';
- 修改字段类型 modify
alter table 表名 modify 字段名 字段类型 [注释];
-- 修改身份证号的 类型为 varchar
alter table t_student modify card varchar(18) comment '身份证号';
- 修改字段类型 change
alter table 表名 change oldColumn newColumn 字段类型 [注释];
-- 将身份证号 card 字段修改为
alter table t_student change card cardNo varchar(18) comment '身份证号'
- 删除字段名 drop
alter table 表名 drop column 字段名;
-- 删除字段名
alter table t_student drop column tel;
- 修改表名 rename
alter table 表名 rename to 新表名;
-- 修改 t_student为 student
alter table t_student rename to student;
rename 表名 to 新表名;
-- 修改 student为 t_student
rename table student to t_student;
- 查看表结构
desc 表名;
desc t_student;
describe 表名;
查看当下库所有的表
show tables;
show full tables;
-- 查看建表结构
show create table 表名;
show create table t_student;