在介紹這些SQL語言以前,先羅列一下mysql的經常使用數據類型和數據類型修飾,供查詢參考mysql 後面的帶數字表示此類型的字段長度正則表達式 |
數值型:sql
TINYINT 1 ,SMALLINT 2,MEDIUMINT 3 ,INT 4,BIGINT 8,DECIMAL,FLOAT 4,DOUBLE 8,BIT數據庫
字符串型ide
CHAR,VARCHAR,BINARY,VBINARY,TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOG,TINYTEXT,TEXT,MEDIUMTEXT,LONGTEXT,EMUM,SETui
日期時間型spa
date,time,datetime,timestamp排序
數據限定修飾:索引
NOT NULL,NULL,DEFAULT,AUTO_INCREMENT,UNSIGNED,PRIMARY KEY,UNIQUE KEY,FOREIGN KEYip
CHARACTER SET #ps:SHOW CHARACTER SET 顯示當前數據庫所支持的全部字符集
COLLATION #ps:SHOW COLLATION 顯示所支持的全部排序規則
如下是SQL的每種DDL,DCL,DML語言
DDL
----Data Definition Language 數據庫定義語言如 create procedure之類
建立數據庫
CREATE DATABASE [IF NOT EXISTS] DBNAME [CHARACTER SET 'CHAR_NAME'] [COLLATE 'COLL_NAME'] |
修改:ALTER 刪除:DROP
建立一張新表
CRTATE TABLE [IF NOT EXISTS] TBNAME(col_name col_definition,...) |
EXAMPLE:
mysql>CREATE TABLE students(Id INT PRIMARY KEY NOT NULL AUTO_INCREMENT UNSIGNED,Name CHAR(20) UNIQUE KEY NOT NULL,Age TINYINT UNSIGNED INDEX,Gender CHAR(1) NOT NULL) [ENGINE={MyISAM | InnoDB }];
也能夠這樣寫(區別在於單獨定義主鍵,惟一鍵和索引):
mysql>CREATE TABLE students(Id INT NOT NULL AUTO_INCREMENT UNSIGNED,Name CHAR(20) NOT NULL,Age TINYINT UNSIGNED,Gender CHAR(1) NOT NULL,PRIMARY KEY(id),UNIQUE KEY(name),INDEX(age))
查詢出一張表的數據後建立新表(字段定義會丟失,數據會保留)
CREATE TABLE TBNAME SELECT... |
EXAMPLE:
mysql>CREATE TABLE test SELECT * FROM students WHERE Id>5;
以一張表的格式定義,建立一張新的空表
CREATE TABLE TBNAME1 LIKE TBNAME2 |
修改表:
ALTER TABLE tb_name MODIFY #修改字段定義 CHANGE #能夠修改字段名和字段定義 ADD DROP |
EXAMPLE:
給表添加字段
mysql>ALTER TABLE students ADD (course VARCHAR(100),teacher CHAR(20));
添加唯一鍵
mysql>ALTER TABLE students ADD UNIQUE KEY Name;
修改字段:
修改course字段爲Course字段,並放在Name字段以後(修改字段須要帶上新的字段的定義)ps:MODIFY只能修改字段定義
mysql>ALTER TABLE students CHANGE course Course VARCHAR(100) [AFTER Name];
重命名錶名
mysql>ALTER TABLE students RENAME TO stu; mysql>RENAME TABLE stu TO students;
添加一個外鍵約束
ALTER TABLE students ADD FOREIGN KEY foreign _cid (CID) REFERENCES course (CID);
建立索引
CREATE INDEX index_name ON TABLE (col_name[(length)] [ASC|DESC]) [USING {BTREE|HASH}]; |
刪除索引
DROP INDEX index_name ON TBNAME; |
查看錶狀態:SHOW STATUS LIKE 'TBNAME';
查看錶的索引:SHOW INDEXES FROM TBNAME;
DML
----Data Manipulation Language 數據操縱語言如insert,delete,update,select(插入、刪除、修改、檢索)
插入修改數據
#若是每一個字段都有值,不須要寫字段名稱,每組值用,隔開
mysql>INSERT INTO tb_name (col1,col2) VALUES ('STRING',NUM),('STRING',NUM); mysql>INSERT INTO tb_name SET col1='string',col2='string'; mysql>INSERT INTO tb_name (col1,col2,col3) SELECT...; |
EXAMPLE:
mysql>INSERT INTO students (Name,Gender,teacher) VALUE ('lujunyi','M','mage'),('wusong','M','zhuima'); mysql>INSERT INTO students SET Name='lujunyi',Gender='M',tearcher='zhuima';
更新數據
mysql>UPDATE tb_name SET column=value WHERE column=value; mysql>UPDATE students SET Course='mysql' WHERE Name='lujunyi';
替換數據:
和UPDATE使用方式同樣,只要將UPDATE換成REPLACE便可
刪除數據
mysql>DELETE FROM tb_name WHERE conditions; |
mysql>DELETE FROM students WHERE Course='mysql';
清空表:將會重置計數器
mysql>TRUNCATE tb_name |
查詢數據
單表查詢:
mysql>SELECT [DISTINCT] column FROM tb_name WHERE CONDITION; |
EXAMPLE:
#基本投影查詢
mysql>SELECT Name,teacher FROM students WHERE Name='wusong';
#重複的結果只顯示一次
mysql>SELECT DISTINCT Gender FROM students;
#組合條件,可使用AND,OR,NOT,XOR組合多個條件
mysql>SELECT * FROM students WHERE Age>20 AND Gender='M';
#使用BETWEEN...AND...篩選出年齡介於20-25之間的數據
mysql>SELECT * FROM students WHERE Age BETWEEN 20 AND 25;
#查詢Name以Y開頭的的數據,%表示任意長度的任意字符,_表示任意單個字符
mysql>SELECT * FROM student WHERE Name LIKE 'Y%';
#使用正則表達式匹配查詢,關鍵詞爲RLINK或者REGEXP
mysql> SELECT * FROM students WHERE Name RLINK '^[MNY].*$';
#使用IN關鍵詞,將條件限定在一個列表中。用IS關鍵詞,表示條件是否爲空(IS NULL 或者 IS NOT NULL)
mysql>SELECT * FROM students WHERE Age IN (20,22,24);
#將查詢的結果進行排序
mysql>SELECT * FROM students ORDER BY Name {ASC|DESC};
#查詢結果別名顯示
mysql>SELECT Name AS Stu_Name FROM students;
#LIMIT限定查詢結果的條數,LIMIT 2,3表示偏移2條數據後,取3條數據
mysql>SELECT * FROM students LIMIT 2;
#求平均數:AVG(),最大值:MAX() 最小值MIN() 數量:COUNT() 求和:SUM()
mysql>SELECT AVG(age) FROM students;
#分組GROUP BY
mysql>SELECT Age, Gender FROM students GROUP BY Gender;
#別名:AS
mysql>SELECT COUNT(Age) AS Num,Age FROM students GROUP BY Age;
#過濾:HAVING
mysql>SELECT COUNT(Age) AS Num,Age FROM students GROUP BY Age HAVING Num>2;
多表查詢:
#指定已哪一個字段鏈接2張表
mysql>SELECT students.Name,courses.Cname FROM students,courses WHERE students.CID1 = courses.CID;
#鏈接時指定別名
mysql>SELECT students.Name,courses.Cname FROM students,courses WHERE students.CID1 = courses.CID;
#左外鏈接...LEFT JOIN...ON...
mysql>SELECT s.Name,c.Cname FROM students AS s LEFT JOIN courses AS c ON s.CID1=c.CID;
#右外鏈接...RIGHT JOIN...ON...
mysql>SELECT s.Name,c.Cname FROM students AS s RIGHT JOIN courses AS c ON s.CID1=c.CID;
子查詢
#查詢年齡大於平均年齡的數據
mysql>SELECT * FROM students WHERE Age > (SELECT AVG(Age) FROM students);
#在FROM中使用子查詢
mysql>SELECT Name,Age FROM (SELECT * FROM students WHERE CID IN (2,3)) AS t WHERE Age>20;
#聯合查詢
mysql>(SELECT Name,Age FROM students) UNION (SELECT Tname,Age FROM tutors);
建立視圖
CREATE VIEW VIEW_NAME AS SELECT.... |
DCL
----Data Control Language 數據庫控制語言
如grant,deny,revoke等,只有管理員纔有這樣的權限。
建立用戶
mysql>CREATE USER 'USERNAME'@'HOST' IDENTIFIED BY 'PASSWORD' |
刪除用戶
mysql>DROP USER 'USERNAME'@'HOSHOST支持通配符 _:任意單個字符 %:任意多個字符 |
受權
mysql>GRANT pri1,pri2...ON DB_NAME.TB_NAME TO 'USERNAME'@'HOST' [IDENTIFIED BY 'PASSWORD'] |
取消受權
mysql>REVOKE pri1,pri2...ON DB_NAME.TB_NAME FROM 'USERNAME'@'HOST'; |
查看受權
mysql>SHOW GRANTS FOR 'USERNAME'@'HOST'; |
EXAMPLE:
mysql>CREATE USER 'lujunyi'@'%' IDENTIFIED BY '123456'; mysql>SHOW GRANTS FOR 'lujunyi'@'%'; mysql>GRANT ALL PRIVILEGES ON testdb.* TO 'lujunyi'@'%';