1. MySQL命令
1.1 显示所有的数据库
显示所有的数据库:show databases;
1.2 显示所有的字符集
显示所有的字符集:show character set;
1.3 切换数据库
切换数据库:use school;
1.4 显示所有表
显示所有表:show tables;
1.5 获取帮助
获取帮助:help。
2. SQL
SQL:Structured Query Language。
DDL:Data Definition Language - create / drop / alter。
2.1 创建数据库并指定默认的字符集
创建数据库并指定默认的字符集:create database school default character set utf8mb4;
MySQL8 默认的字符集是 utf8mb4
MySQL5 默认的字符集是 latin1
2.2 删除数据库
删除数据库: drop database if exists school;
2.3 创建二维表
创建二维表:
create table tb_student
(
stu_id int not null comment '学号',
stu_name varchar(10) not null comment '姓名',
stu_sex boolean default 1 comment '性别',
stu_birth date comment '出生日期',
primary key (stu_id)
) engine=innodb comment '学生表';
2.4 数据类型
2.4.1 整数
整数:integer / int / tinyint / smallint / bigint
4 1 2 8
-2^31 ~ 2^31-1
int unsigned —> 0 ~ 2^32-1
2.4.2 小数
小数:decimal(10,4)
2.4.3 日期
日期:date / time / datetime
2.4.4 文本
文本:varchar
2.5 查看表结构
查看表结构:desc tb_student;
2.6 删除二维表
删除二维表: drop table if exists tb_student;
2.4 修改二维表
2.4.1 添加一个列
alter table tb_student add column stu_addr varchar(100) default ‘’;
alter table tb_student add column stu_tel char(11) not null;
2.4.2 删除一个列
alter table tb_student drop column stu_tel;
alter table tb_student drop column stu_id;
2.4.3 修改一个列
alter table tb_student modify column stu_sex char(1) default ‘M’;
alter table tb_student change column stu_sex stu_gender boolean default 1;
2.4.4 添加约束条件
alter table tb_student add constraint primary key (stu_id);
2.4.5 修改表的名字
alter table tb_student rename to …;
3. 作业
-
学院表(编号、名称、介绍、成立日期、网址)
create table tb_college ( college_id int not null comment '学院编号', college_name varchar(10) not null comment '学院名称', college_introduction varchar(255) comment '学院介绍', college_birth date comment '成立日期', college_url varchar(20) comment '网址' ) engine=innodb comment '学院表'; alter table tb_college add constraint primary key(college_id);
-
老师表(工号、姓名、性别、职称、出生日期)
create table tb_teacher
(
teacher_id int not null comment '工号',
teacher_name varchar(10) not null comment '姓名',
teacher_gender boolean default 1 comment '性别',
teacher_title varchar(10) comment '职称',
teacher_birth date comment '出生日期',
primary key (teacher_id)
) engine=innodb comment '老师表';
- 课程表(编号、名称、学分、开课学期)
create table tb_course
(
course_id int not null comment '编号',
course_name varchar(10) not null comment '名称',
course_credit int comment '学分',
course_semester int comment '开课学期',
primary key (course_id)
) engine=innodb comment '课程表';