數(shù)據(jù)庫的創(chuàng)建、刪除、顯示
創(chuàng)建數(shù)據(jù)庫:
? ? ? ? create database + 庫名;
刪除數(shù)據(jù)庫:
? ? ? ? drop database + 庫名;
顯示數(shù)據(jù)庫:
? ? ? ? show database;
數(shù)據(jù)庫表的創(chuàng)建增刪改查
創(chuàng)建表
????????create table 表名(
??????????????? 字段名 數(shù)據(jù)類型? 屬性/約束(自增,非空)? 索引(主鍵,唯一)? 注釋,... ...? );
? ? ? ? ? ? ? ? ? 添加外鍵:constrants 外鍵名 foreign key(字段) references(主表字段)
? ? ? ? ? ? ? ? 表類型---engine:默認(rèn)innoDB
刪除表
????????????drop table 表名;
修改表
? ? ? ? ? alter table 表名 modify 字段名 字段屬性;
? ? ? ?? alter table 表名 change 原字段名 新字段名 字段屬性;
刪除列名
? ? ? ? alter table 表名 drop 字段名;
修改表名
? ? ? ? alter table 舊表名 rename to 新表名;
顯示表結(jié)構(gòu)
? ? ? ? dese 表名 ; ? ? ??
顯示表名稱
? ? ? ? show tables;
切換數(shù)據(jù)庫
? ? ? ? use 數(shù)據(jù)庫名;
清空表數(shù)據(jù)
? ? ? ? truncate 表名
數(shù)據(jù)庫內(nèi)容增刪改查
? ? ? ? 增:insert into 表名(列名)values(列值);
? ? ? ? 刪:delete from 表名;
? ? ? ? 改:update 表名 set 列名 = 列值,列名 = 列值? where 條件;
? ? ? ? 查:select * from 表名 where 條件;
? ? ? ? ? ? ? ? ? ? 條件:分組:group by ... having ...
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 排序:order by ... desc/asc
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 截取條數(shù):limit
創(chuàng)建索引
? ? ? ? 普通:alter table 表名 add index/key ('列名');
創(chuàng)建外鍵
? ? ? ? alter table 從表名 add constraint 外鍵名 foreign key (‘從表名’)references 主表名(‘主表列’);
刪除索引
? ? ? ? alter table 表名 drop foreign key 索引名 ? ? ? ?
顯示索引
? ? ? ? show index/constraints from table
創(chuàng)建事務(wù)
? ? ? ? ? ? set autocommit = 0;
? ? ? ? ? ? start construction;
? ? ? ? ? ? sql語句
? ? ? ? ? ? commit;
? ? ? ? ? ? rollback;
? ? ? ? ? ? set autocommit = 1;
? ? ? ? ? ? ? ??
????????