创建库
创建库
create database [if not exists] 库名;
删除库
drop database [if exists] 库名;
建库通用的写法
drop database if exists 旧库名;
create database 新库名;
-- 查看当前库
SHOW DATABASES LIKE foo;
-- 删除库 如果存在时
DROP DATABASE IF EXISTS foo;
-- 创建库
CREATE DATABASE foo;
表管理
create table 表名(
字段名1 类型[(宽度)] [约束条件] [comment '字段说明'],
字段名2 类型[(宽度)] [约束条件] [comment '字段说明'],
字段名3 类型[(宽度)] [约束条件] [comment '字段说明']
)[表的一些设置];
注意:
1. 在同一张表中,字段名不能相同
2. 宽度和约束条件为可选参数,字段名和类型是必须的
3. 最后一个字段后不能加逗号
4. 类型是用来限制 字段 必须以何种数据类型来存储记录
5. 类型其实也是对字段的约束(约束字段下的记录必须为XX类型)
6. 类型后写的 约束条件 是在类型之外的 额外添加的约束
not null:标识该字段不能为空
create table test1(
a int not null comment '字段a、int型、不是null'
);
-- 插入测试数据
insert into test1 values(null);
default value:为该字段设置默认值,默认值为value
create table test2(
a int not null comment '字段a、int型、不是null',
b int not null default 0 comment 'int型、默认值0、不是null'
);
-- 插入测试数据
insert into test2(a) values(1);
primary key:标识该字段为该表的主键,可以唯⼀的标识记录,插⼊重复的会报错
-- 方法一
-- 如果表test3存在就删除
drop table if exists test3;
-- 创建test3表
create table test3(
a int not null comment 'int型、不是null' primary key
);
-- 插入测试数据
insert into test3(a) values(1);
insert in