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 表名;