SQl
DDl-数据库操作
查询
查询所有数据库
show databases;
查询当前数据库
select database();
创建
create database [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];
删除
drop database[if exists] 数据库名;
使用
use 数据库名;
DDl-表操作-查询
查询当前数据库所有表
show tables;
查询表结构
desc 表名;
查询制定表的创表语句
show create table 表名;
DDl-表操作 - 创建
create table 表名(字段1 字段1类型[comment 字段1注释],字段2 字段2类型[comment 字段2注释],字段3 字段3类型[comment 字段3注释],-----字段n 字段n类型[comment 字段n注释]
)[comment 表注释];
--注意 最有一个字段后面没有逗号,create table emp(id int comment '编号',workno varchar(10) comment '工号',name varchar(10) comment '姓名',gender char(1) comment '姓别',age tinyint unsigned comment '年龄',idcard char(18) comment '身份证号',entrydate date comment '入职时间'
)comment '员工表';
DDl--表操作-修改
添加字段
alter table 表名 add 字段名 数据类型(长度) [comment '注释'][约束];alter table emp add demoname varchar(30) comment '称呼';
修改数据类型
alter table 表名 modify 字段名 新数据类型(长度);
修改字段名和字段类型
alter table 表名 change 旧字段名 新字段名 类型(长度)[comment 注释][约束];alter table emp change nickname username varchar(30) comment '用户名';
删除字段
alter table 表名 drop 字段名;alter table emp drop username;
修改表名
alter table 表名 rename to employee;alter table emp rename to employee;
删除表
drop table [if exists] 表名;drop table if exists tb_user;
删除指定表,并重新创建该表
truncate table 表名;