mysql中database, table, 字段的基本操作

70 阅读1分钟

database基本操作

1.增

create database 数据库名;

create database [if not exists]数据库名 [default charset utf8mb4 collate utf8mb4_general_ci];

2.删

drop database 数据库名;

3.改

use 数据库名; 这个命令主要是切换到需要使用的数据库。

4.查

show databases; 这个命令主要是查看有哪些数据库。

show tables; 这个命令主要是查看一个数据库下面有哪些表。

table基本操作

1.增

create table 表名 (

字段名 字段类型(长度) 约束 默认值, 字段名 字段类型(长度) 约束 默认值,

);

2.删

drop table 表名;

3.改

alter table 原表名 rename 新表名; 这个命令是修改表的名字。

4.查

desc 表名;这个命令是查看表的结构。

字段的基本操作

1.增

alter table 表名 add 字段名 字段类型 约束 默认值;(默认加在表的最后)

alter table 表名 add 字段名 字段类型 约束 默认值 first;(加在表的最前面)

alter table 表名 add 字段名 字段类型 约束 默认值 after 字段名;(加在具体的某一个字段后面)

alter table 表名 drop 字段名;

3.改

alter table 表名 modify 字段名 字段类型;(这个命令用于修改字段类型)

alter table 表名 change 原字段 新字段 字段类型;(这个命令用于修改字段的名字)