227 lines
4.6 KiB
Markdown
227 lines
4.6 KiB
Markdown
|
|
|||
|
|
|||
|
services.msc
|
|||
|
|
|||
|
## mysql无大小写区分
|
|||
|
|
|||
|
安装目录:C:\Program Files\MySQL\MySQL Server 8.0\bin
|
|||
|
|
|||
|
### 登录命令:
|
|||
|
|
|||
|
```mysql
|
|||
|
mysql -h localhost -u root -p
|
|||
|
-h 服务器地址
|
|||
|
-u 登录数据库用户名(root 管理员)
|
|||
|
-p 密码
|
|||
|
也可
|
|||
|
mysql -u root -p
|
|||
|
```
|
|||
|
|
|||
|
passWord:8520
|
|||
|
|
|||
|
### 展示已存在数据库
|
|||
|
|
|||
|
```mysql
|
|||
|
show databases;
|
|||
|
```
|
|||
|
|
|||
|
### 创建数据库
|
|||
|
|
|||
|
```mysql
|
|||
|
create database jwxt;
|
|||
|
```
|
|||
|
|
|||
|
### 改数据库支持中文字符集
|
|||
|
|
|||
|
```mysql
|
|||
|
set character_set_database='gbk';
|
|||
|
set character_set_server='gbk';
|
|||
|
```
|
|||
|
|
|||
|
### 删除数据库(不可恢复,删除之后就没了)
|
|||
|
|
|||
|
```mysql
|
|||
|
drop database 教务系统;
|
|||
|
```
|
|||
|
|
|||
|
### 判断是否执行操作
|
|||
|
|
|||
|
```mysql
|
|||
|
create database if not exists jwxt;
|
|||
|
```
|
|||
|
|
|||
|
### 进入数据库
|
|||
|
|
|||
|
```mysql
|
|||
|
use jwxt;
|
|||
|
```
|
|||
|
|
|||
|
### 字段
|
|||
|
|
|||
|
```
|
|||
|
create table(
|
|||
|
ID char(6) not null primary key,
|
|||
|
);
|
|||
|
字段名 ID
|
|||
|
数据类型 char(6)
|
|||
|
```
|
|||
|
|
|||
|
### 联合主键(设置两个以上主键):
|
|||
|
|
|||
|
```
|
|||
|
primary key( stID,kcID )
|
|||
|
```
|
|||
|
|
|||
|
### 约束条件:
|
|||
|
|
|||
|
| | |
|
|||
|
| --------------------------------------------- | -------------- |
|
|||
|
| 主键 | primary key, |
|
|||
|
| 不允许为空 | null |
|
|||
|
| 唯一(设置内容不能重复) | unique |
|
|||
|
| 外键 | |
|
|||
|
| 自增(第一个为1以后自增,一般主键设置,整数) | auto_increment |
|
|||
|
| 默认值 (后面跟默认值) | defult |
|
|||
|
|
|||
|
### 从表(自表)的外键做为主表(父表)的主键
|
|||
|
|
|||
|
```mysql
|
|||
|
foreign key (从表的外键字段名)
|
|||
|
references 主表(字段)
|
|||
|
```
|
|||
|
|
|||
|
### 给外键起名字
|
|||
|
|
|||
|
```mysql
|
|||
|
constraint 名字
|
|||
|
```
|
|||
|
|
|||
|
### 查看表
|
|||
|
|
|||
|
```mysql
|
|||
|
SHOW COLUMNS FROM 表名;
|
|||
|
describe 表名;
|
|||
|
```
|
|||
|
|
|||
|
### 创建表
|
|||
|
|
|||
|
```mysql
|
|||
|
create table salary(
|
|||
|
employeeid char(6) not null primary key,
|
|||
|
income float(8) not null,
|
|||
|
outcome float(8) not null
|
|||
|
);
|
|||
|
```
|
|||
|
|
|||
|
### 1.修改表名(rename):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table salary rename salary2
|
|||
|
salary 修改前
|
|||
|
salary2 修改后
|
|||
|
```
|
|||
|
|
|||
|
### 2.修改字段的数据类型(modify):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table employees modify name varchar(20);
|
|||
|
employees 表名
|
|||
|
name 字段名
|
|||
|
varchar(20) 数据类型
|
|||
|
```
|
|||
|
|
|||
|
### 3.修改字段名(change):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table employees change name employees_name varchar(20);
|
|||
|
employees 表名
|
|||
|
name 修改前字段名
|
|||
|
employees_name 修改后字段名
|
|||
|
varchar(20) 数据类型
|
|||
|
//数据类型不能为空,但是可以和修改前一样,也可同时修改
|
|||
|
```
|
|||
|
|
|||
|
说明:change 也可以指修改数据类型,实现和 modify 相同的效果,方法是将 sql 语句中的新字段名和旧字段名设置为相同的名称,只修改数据类型。
|
|||
|
|
|||
|
### 4.添加字段(add):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table salary add shuishou float(8) not null first;
|
|||
|
salary 添加的表名
|
|||
|
shuishou 添加的字段
|
|||
|
float(8) 添加自段的类型
|
|||
|
not null 约束条件(包括外键,主键等等)
|
|||
|
first 添加表的位置 (可选项)
|
|||
|
after 字段名 在字段名之后添加
|
|||
|
```
|
|||
|
|
|||
|
### 5.删除字段 (drop):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table salary drop shuishou;
|
|||
|
salary 表名
|
|||
|
shuishou 字段名
|
|||
|
```
|
|||
|
|
|||
|
### 6.修改字段排列顺序(modify):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table salary modify shuishou float after income;
|
|||
|
salary 表名
|
|||
|
shuishou float 需修改顺序的字段名以及数据类型
|
|||
|
income 目标字段之后(first最前面)
|
|||
|
```
|
|||
|
|
|||
|
### 7.添加外键约束(add):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table salary add foreign key (employeeid) references employees (employeeid);
|
|||
|
salary 修改表
|
|||
|
(employeeid) 字段名
|
|||
|
employees 参考主表
|
|||
|
(employeeid) 参考主表字段名
|
|||
|
```
|
|||
|
|
|||
|
### 8.添加主键约束(add):
|
|||
|
|
|||
|
```
|
|||
|
alter table salary add primary key (employeeid) references employees (employeeid);
|
|||
|
salary 修改表
|
|||
|
(employeeid) 字段名
|
|||
|
employees 参考主表
|
|||
|
(employeeid) 参考主表字段名
|
|||
|
```
|
|||
|
|
|||
|
### 9.修改表的存储引擎 (默认为InnoDB):
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table 表名 engine=更改后存储引擎名;
|
|||
|
```
|
|||
|
|
|||
|
注意:外键不能跨引擎使用
|
|||
|
|
|||
|
### 10.删除外键约束
|
|||
|
|
|||
|
```mysql
|
|||
|
alter table salary drop foreign key 外键名字;
|
|||
|
```
|
|||
|
|
|||
|
若没有命名外键名,则课可以先使用**show create table 表名**查询表的默认外键名,此命令不仅可以查询外键名,还可以查询表的结构、存储引擎、编码方式等信息
|
|||
|
|
|||
|
查询到默认外键名后,再用drop删除即可,即 alter table 表名 drop foreign key 查询到的默认外键名;
|
|||
|
|
|||
|
### 11.删除数据表
|
|||
|
|
|||
|
```mysql
|
|||
|
drop table 表名;
|
|||
|
```
|
|||
|
|
|||
|
**注意:当存在外键约束时,主表不能直接删除,需要先删除外键约束再删表**
|
|||
|
|
|||
|
### 12.清空表
|
|||
|
|
|||
|
```
|
|||
|
delete from 表名;
|
|||
|
```
|
|||
|
|
|||
|
select * from tmp3
|