DDL语句

目录

SQL:

1:DDL:数据定义语言:

2:DDL:表操作:

约束:

4:表操作:


SQL:

就是数据库的查询语言,定义了一套标准。

1:DDL:数据定义语言:


    1:查询数据库:show databases;
    2:创建数据库: create database db02;
    如果你不知道db02是否存在(如果数据库存在系统会报错),你可以使用:create database if not exists db02;
    如果db02存在了,这条语句也不会报错
    3:使用数据库(切换数据库)
    use db01;
    显示当前数据库:select database();
    4:删除数据库:
    drop database db02;
    drop database if exists db02;:表示如果这个数据库存在,我们再删除,不存在也不会报错

2:DDL:表操作:


    创建表(没约束):
    create table tb_user(
    id       int         null comment 'ID',
    username varchar(20) null comment '用户名',
    name     varchar(10) null comment '姓名',
    age      int         null comment '年龄',
    gender   char        null comment '性别'
    )comment '用户表案例01';
    
    创建表(有约束):
create table tb_user2
(
    id       int               not null comment 'ID'
        primary key,
    username varchar(20)       not null comment '用户名',
    name     varchar(10)       not null comment '姓名',
    age      int               null comment '年龄',
    gender   char default '男' null comment '性别',
    constraint username
        unique (username)
)
    comment '用户表案例01';


约束:


非空约束:not null
唯一约束:unique
主键约束:primary key  auto_incremen:自增
默认约束:default


3:表操作中的元素类型:数值类型,字符串类型,日期时间类型
见文件:MySQL数据类型。

4:表操作:

show tables :查看当前数据库下的表
desc tb_emp:查看指定表的结构
show create table tb_emp:查看建表语句


对表的字段操作和对表重命名:
-- DDL:修改表结构
/*添加字段:alter table 表名  add  字段名  类型(长度)  [comment  注释]  [约束];
修改字段类型:alter table 表名 modify  字段名  新数据类型(长度);
修改字段名和字段类型:alter table 表名 change  旧字段名  新字段名  类型 (长度)  [comment 注释]  [约束];
删除字段:alter table 表名 drop  column 字段名;
修改表名: rename table 表名 to  新表名;*/

-- 添加字段
alter table tb_emp add qq varchar(20) comment 'qq号';
-- 修改字段类型
alter table tb_emp modify qq varchar(30) not null ;
-- 修改字段名和字段类型
alter table tb_emp change qq wechat varchar(20) comment '微信号';
-- 删除字段
alter table tb_emp drop column wechat;
-- 修改表名
rename table tb_emp to tb_emp05;


删除表 drop table 表名 [if exit] 
删除表之后表中的数据也会一起删除
 

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值