一、创建相应的表格
create table 表名(
字段1 字段类型,
字段2 字段类型,
字段3 字段类型,
.....
字段4 字段类型
)
二、常见的数据类型
int:整型
double: 浮点型,例如:double(5,2)标识最多5位,其中必须有两位小数
char: 固定长度字符串类型: char(10) 'abc'
varchar: 可变长度字符串类型:varchar(10) 'abc'
text: 字符串类型;
blob: 字节类型(图片,视频,音频)
date: 日期类型 yyyy-MM-dd;
time:时间类型: 格式为:hh:mm:ss
timestamp: 时间戳了下 yyyy-MM-dd hh:mm:ss 会自动赋值
datetime: 日期时间类型 yyyy-MM-dd hh:mm:ss
三、查看当前的数据库表
show tables
四、查看当前数据库表的字段信息
desc emp;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| entry_date | date | YES | | NULL | |
| job | varchar(100) | YES | | NULL | |
| salary | double | YES | | NULL | |
| resume | varchar(100) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
五、修改表结构
alter table emp add image blob;
六、修改表的数据类型
alter table emp modify job varchar(60);
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| entry_date | date | YES | | NULL | |
| job | varchar(60) | YES | | NULL | |
| salary | double | YES | | NULL | |
| resume | varchar(100) | YES | | NULL | |
| image | blob | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
七、重命名表名
rename table emp to user
八、查看表的创建信息
show create table emp;
| emp | CREATE TABLE `emp` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`entry_date` date DEFAULT NULL,
`job` varchar(60) DEFAULT NULL,
`salary` double DEFAULT NULL,
`resume` varchar(100) DEFAULT NULL,
`image` blob
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
八、重命名表中某个字段的名字
alter table emp change name userName varchar(100);
修改前:
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | | 修改前
| gender | varchar(10) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| entry_date | date | YES | | NULL | |
| job | varchar(60) | YES | | NULL | |
| salary | double | YES | | NULL | |
| resume | varchar(100) | YES | | NULL | |
| image | blob | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
修改后
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| userName | varchar(100) | YES | | NULL | |修改后
| gender | varchar(10) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| entry_date | date | YES | | NULL | |
| job | varchar(60) | YES | | NULL | |
| salary | double | YES | | NULL | |
| resume | varchar(100) | YES | | NULL | |
| image | blob | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+