数据库基础知识
创建和查看数据库、数据表
创建数据库
create database YGGL;
查看已存在的数据库
show databases;
调用yggl数据库,创建employees表格
先选择创建表的数据库
use yggl;
create table employees(employeeid char(6) primary key,
name char(10) not null,
education char(4),
birthdaydate date,
gender bit,
workyear int,
address varchar(40),
phonenumber char(12),
departmentid int);
查看所有的表格
show tables;
查看表的字段信息
其中包括字段名、字段类型等信息
DESC 表名;
修改数据库、数据表
修改数据库名称
alter database 数据库名称 default character set 编码方式 collate 编码方式_bin;
“数据库名称”指的是要修改的数据库
“编码方式”指的是修改后的数据库编码
修改数据表
1. 修改表名
alter table 旧表名 rename 新表名;
2. 修改字段名
alter table 表名 change 旧字段名 新字段名 新数据类型
举例:把employees表中gender字段名修改为sex 枚举型“男“”女”
alter table employees change gender sex enum(‘男’,‘女’);
3. 修改字段的数据类型
alter table 表名 modify 字段名 数据类型;
举例:修改employees表中gender字段的数据类型为枚举型“男”“女”
alter table employees modify gender enum(‘男’,‘女’); 枚举型
4. 添加字段
alter table 表名 add 新字段名 数据类型
5. 删除字段
alter table 表名 drop 字段名;
6. 修改字段的排列位置
alter table 表名 modify 字段名1 数据类型 FIRST|ALTER 字段名2
“字段名1”指的是修改位置的字段
“数据类型”指的是字段1的数据类型
“FIRST"可选参数 指的是将字段名1修改为表的第一个字段
”ALTER 字段名2" 是将字段1插入到字段2的后面
删除数据库、数据表
删除数据库
drop database 数据库名称;
删除数据表
没有被其他表关联的数据表
drop table 表名;