MYSQL 基礎(chǔ)
DDL-數(shù)據(jù)庫操作:
查詢:
查詢所有數(shù)據(jù)庫:
show databases;
查詢當(dāng)前數(shù)據(jù)庫:
select database[ if not exists] 數(shù)據(jù)庫名/[default charset 字符集] [collate 順序規(guī)則];
創(chuàng)建:
create database[if not exists] 數(shù)據(jù)庫名;
使用:
use 數(shù)據(jù)庫名;
查詢當(dāng)前數(shù)據(jù)庫所有表:
show tables;
查詢表結(jié)構(gòu):
desc 表名;
查詢指定表的建表語句:
show create table 表名;
創(chuàng)建:
create table 表名(
字段1 字段1類型[comment '字段1注釋'],
字段2?字段2類型[comment '字段2注釋'],
......
字段n?字段n類型[comment '字段n注釋']
) [comment 表注釋];
數(shù)值類型:

字符串類型:

char(數(shù)字):性能高,固定空間,未占用字符會用空格進(jìn)行補(bǔ)位。
varchar(數(shù)字):性能較低,根據(jù)存儲內(nèi)容計(jì)算當(dāng)前所占用的空間是多長
括號內(nèi)的數(shù)字代表當(dāng)前字符串能夠存儲的最大長度。
日期時間類型:

eg--》》

修改:
添加字段:
alter table 表名 add 字段名 類型(長度)[comment注釋] [約束];
eg.
alter table emp add nickname varchar(20) comment '昵稱';
修改數(shù)據(jù)類型:
alter table 表名 modify 字段名 新數(shù)據(jù)類型(長度);
修改字段名和字段類型:
alter table 表名 change 舊字段名 新字段名 類型(長度)[comment 注釋];
刪除字段:
alter table 表名 drop 字段名;
修改表名:
alter table 表名 rename to 新表名;
刪除:
刪除表:
drop table [if exists] 表名;
刪除指定表,并重新創(chuàng)建該表:
truncate table 表名;