表
我们之前介绍过表(table)是在数据库中的一些数据的集合,那么存储在表中的数据也有其相对应的类型。
数据类型
- 整型
- 浮点型和定点型
- 日期和时间类型
- 字符型
DDL(数据定义语言)
DDL是数据定义语言还有DML是数据操作语言。
下面我们来学习一下如何创建表:注意逗号和分号
create table [if not exists] 表名(
字段 1 字段类型 [列级约束条件],
字段 2 字段类型 [列级约束条件],
.....
字段 n 字段类型 [列级约束条件]
);
下面我们来创建一个名为"information"的表,表中要包括名字、年龄、联系方式。
create table if not exists information(
name varchar(20);
age int;
phone_number varchar(20);
)
修改表
一般状态下,创建好的表就不应该被再次更新,因为在表的设计过程中需要花费大量时间来考虑,以便不对该表进行较大的改动
- 添加字段
alter table 表名
add column 新列名 数据类型 [约束条件] [first | alter 列名];
- 修改字段的类型
alter table 表名 modify column 列名 数据类型[约束条件];
- 修改字段的位置
alter table 表名 modify column 列名 数据类型 first | after 列名;
- 修改字段名
alter table 表名 change column 旧列名 新列名 数据类型;
- 删除字段
alter table 表名
drop column 列名;
重命名表
alter table 旧表名 rename to 新表名;
删除表
drop table [if exists] 表1[,表2,表3...]
下面做一些练习巩固一下
create table if not exists article(
id int,
title varchar(20),
author varchar(32),
content text
);
create table if not exists orderlist(
id int,
customer_id int,
Orderdate datetime,
money decimal(10,2)
);
create table if not exists produce(
p_name varchar(32),
p_price decimal(10,2),
p_have boolean,
p_desc text
);
alter table emp add column emo_bonus decimal(10,2);
alter table emp modify column emo_bonus decimal(12,2);
alter table emp change column emo_bonus bonus decimal(12,2);
alter table emp drop column bonus;