數(shù)據(jù)庫操作:
建庫:create database 庫名
刪庫:drop database 庫名
顯示庫:show databases 庫名(數(shù)據(jù)庫+s)
表創(chuàng)建:create table 表名 (字段名,數(shù)據(jù)類型,屬性,索引,注釋),(......),(......);
直接在創(chuàng)表語句下方加外鍵 :constraints 外鍵名 ?foreign key(從表字段) references (主表字段)
engine——表類型,默認inneDB ? ?charset——編碼格式,默認UTF8 ? ? calleate——校對編碼,默認是 utf8_unicode_ci
表刪除:drop table 表名
表結構修改:
alter table modify 字段 屬性
alter table change 舊字段 新字段名+屬性
添加列:alter table add 字段 屬性
刪除列:alter table drop 字段名?
修改表名:alter table 舊表名 rename 新表名
顯示名結構:desc 表名
顯示所有表:show tables
指定數(shù)據(jù)庫:use 表名
表數(shù)據(jù)修改:
添加數(shù)據(jù):insert into 表名 (字段,字段) values (值,值)
刪除數(shù)據(jù):delete from 表名 where 條件
修改數(shù)據(jù):update 表名 set 字段=值 where 條件
查詢數(shù)據(jù):select 篩選結果 from 表名 where 條件 and 條件
模糊查詢:like '%某%' ? ? ?排序:order by ? 升序 ? desc ? ?降序 ? asc ? ??
限制顯示條數(shù):limit ? 3——顯示前3條?
? ? ? ? ? ? ? ? ? ? ? ? ?limit ? ? ?3,5——去除前3條,從第4條開始,顯示5條
索引:
一,創(chuàng)建表時直接添加
二,在創(chuàng)表語句下方直接添加
三,alter table 表名 add index on 表名(字段名)
創(chuàng)建外鍵:
alter table 從表名 add constraints 外鍵名 foreign key (從表字段) references 主表名 (主表字段)
刪除鍵和索引:
alter table 表名 drop 鍵或索引類型+鍵或索引名
顯示所有索引或鍵:
show index或constraints from 表名
事務:
set autocommit=0
begin或 start transaction
rollback
commit
set autocommit=1