1. 程式人生 > >Mysql資料庫——sql基本語句

Mysql資料庫——sql基本語句

SQL基本語句

1.登入退出及快捷鍵:

(1)快捷鍵: ————快速回到行首 ctrl + a

————回到行末 ctrl + e

————清屏 ctrl + l

————結束 ctrl + c + 空格

(2)連線資料庫: ————顯示密碼 mysql -uroot -pmysql

————不顯示密碼 mysql -uroot -p 輸入密碼

(3)退出資料庫: quit/exit/ctrl + d

(4)部分命令: ————顯示資料庫版本 select version();

————顯示時間 select now();

2.資料庫操作:

(1)建立: ————建立資料庫 create database 資料庫名 charset=utf8;

(2)使用: ————使用資料庫 use 資料庫名

(3)檢視: ————檢視當前使用的資料庫 select database();

————檢視所有資料庫 show databases;

————檢視建立資料庫語句 show create database 資料庫名;

(4)刪除: ————刪除資料庫 drop database 資料庫名;

(5)資料庫的備份與恢復(瞭解): ————備份 mysqldump -uroot -p 資料庫名 > xxx.sql

————恢復 mysql -uroot -p 新資料庫名 < xxx.sql

3.表結構操作:

(1)檢視: ————檢視當前資料庫中所有表 show tables;

————查看錶結構 desc 表名

————查看錶建立語句 show create table 表名;

(2)建立: ————建立表 create table 表名( 欄位名稱 資料型別 約束條件, …….. …….. ); (auto_increment:自動增長)

(3)修改: ————新增欄位 alter table 表名 add 欄位名 型別 約束;

————修改欄位(不重新命名) alter table 表名 modify 欄位名 型別 約束;

————修改欄位(重新命名) alter table 表名 change 舊欄位名 新欄位名 型別 約束;

————刪除欄位 alter table 表名 drop 欄位名;

(4)刪除: ————刪除表:drop table 表名;

4.表資料操作:

(1)檢視: ————檢視所有欄位 select * from 表名;

————檢視指定欄位 select 欄位名,欄位名 from 表名;

————按條件檢視 select * from 表名 where 條件;

————為欄位起別名檢視 select 欄位名 as 別名 from 表名;

(2)增加: ————全列插入 insert into 表名 values(記錄);

————部分插入 insert into 表名(欄位名,欄位名) values(記錄);

————多行插入 insert into 表名values(記錄1),(記錄2)…;

(3)刪除: ————物理刪除 delete from 表名 where 條件;

————邏輯刪除 alter table 表名 add 刪除資訊欄位名 bit dafault 0; update 表名 set 刪除資訊欄位名=1 where 條件;

(4)修改: ————全部修改 update 表名 set 修改資訊;

————按條件修改 update 表名 set 修改資訊 where 條件;