MySQL之——表

 表

我们之前介绍过表(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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coke_3.2.2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值