Mysql的基礎命令
一,資料庫:
查詢資料庫:show database;
查詢所在庫:select database;
指定查詢庫:show database 資料庫名稱;
建立資料庫:create database 資料庫名稱;
刪除資料庫:drop database 資料庫名稱; (永久性刪除)
選擇資料庫:use/USE 資料庫名;(不區分大小寫)
二,資料表:
查詢所有表:show tables;
查詢指定表:show table 表名;
建立資料表:create table 表名(欄位 型別(主鍵自增:auto_increment primary key,欄位 varchar(20) not null))charset=utf8;
刪除資料表:drop table 表名;
修改原表名:alter table 原表名 rename 新表名;
三,表字段:
新增表字段:alter table 表名 add 要新增的欄位 帶型別;
刪除表字段:alter table 表名 drop 要刪除的欄位名;
修改表字段:alter table 表名 change 原欄位名 新欄位名 型別 not null;
查詢表字段:desc 表名;
開頭填欄位:alter table 表名 要新增的欄位帶型別 frist;(一般為id主鍵型別很少用到)
指定填欄位:alter table 表名 add 要新增的欄位名加型別 after 要在哪個欄位後面新增就寫哪個欄位名
四,表資訊:
查詢表資訊:select * from 表名;
新增表資訊:insert into 表名(欄位1,欄位2)values(值1,值2);
直接填資訊:insert into 表名 values(值1,值2);
修改表資訊:update 表名 set 欄位名=‘要修改的資訊’ where 條件 以id為目標=資訊-1;
刪除表資訊:delete 表名 where 條件 id = 1;
清空表資訊:delete 表名;(清空表內所有資訊保留欄位)
刪除資料表:drop 表名;(直接刪除資料表)
五,資料庫編碼:
檢視庫編碼:show variables like ‘character_set_database’;
查看錶編碼:show create table 表名;
建庫設編碼:create table 表名(欄位1,欄位2)charset=utf8;
修改庫編碼:alter database 資料庫名稱 character set utf8;
修改表編碼:alter table 表名 character set utf8;
修改列編碼:alter table 表名 change 欄位名 欄位名 型別 character set utf8 not null;
六,表主鍵:
刪除自增id:alter table 表名 drop id;
新增自增id:alter table 表名 add id int auto_increment primary key first;
主鍵自增id:alter table 表名 add change id int not null auto_increment primary key;
主鍵自增id:alter table 表名 add column id int auto_increment not null primary key (id);
清空資料 並將欄位恢復至1重新開始計數:truncate table 表名;
刪除表約束:alter table 表名 drop primary key;
有自增的情況下,需要先刪除自增,之後才能刪除主鍵約束;
七,表外來鍵:
新增表外來鍵:alter table 表名 constraint 外來鍵名 foreign key 欄位名 references 外表表名 欄位名;
刪除表外來鍵:alter table 表名 drop foreign key 外來鍵名;
八,總結:
查詢:show database/table;(show 可以查詢資料庫 和 表)
建立 :create database/table;(create 可以建立資料庫 和 表)
查表:select 查詢表結構資訊 desc 查詢表結構
修改:alter table (條件)
刪除:drop 與 delete 永久刪除與 清空資訊, 慎用;
九,內容不是很全,沒有 確定 約束 , 表關聯; 有不對的請指正;