MySQL学习笔记(六)--DDL

create database 【if not exists】 库名 【character set 字符集名】;
alter database 库名 character set 字符集名;
drop database 【if exists】库名;

create table 【if not exists】 表名(
字段名 字段类型 【约束】,
字段名 字段类型 【约束】,

字段名 字段类型 【约束】

修改表
alter table 表名 add column 列表 类型 【first|after 字段名】;
alter table 表名 modify column 列名 新类型 【新约束】;
alter table 表名 change column 旧列名 新列名 类型;

alter table 表名 drop column 列名;
alter table 表名 rename 【to】 新表名

删除表
drop table 【if exists】 表名;

复制表
create table 表名 like 旧表;

复制表的结构+数据
create table 表名
select 查询列表 from 旧表【where 筛选】

数据类型

  • 数值
    整形
    tinyint、smallint、mediumint 、int/integer、bigint
    1 2 3 4 8
    unsigned 无符号,只针对整形
    可以不指定长度,也可以+zerofill 指定长度,不够在左边补0 month int(2) unsigned zerofill DEFAULT NULL

浮点
decimal(M,D) – 如果省略M默认10,D默认0
float(M,D)
double(M,D)

  • 字符
    char、varchar、binary、varbinary、enum、set、text、blob
    char 固定长度,char(M),M省略则默认为1
    varchar(M),M不能省略
  • 日期
    year
    date
    time
    datetime 8
    timestamp 4 (只能保存到2038年,受时区等影响,更能反映真实时间)

约束

not null
default
primary key
unique
check【mysql中不支持】
foreign key

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值