1. 程式人生 > >SQL語言學習文件MySQL/5.7

SQL語言學習文件MySQL/5.7

MySQL/5.7
如何下載? Ubuntu下: sudo apt-get install mysql-server
如何連線? mysql -u root -p 輸入密碼
清除資料庫/慎重
DROP DATABASE test;


建立資料庫
CREATE DATABASE test;


顯示已有資料庫
SHOW DATABASES;


選定操作資料庫
USE test;


MySQL資料型別
TINYINT		1位元組			小整數值
INT		4位元組			大整數值
BIGINT		8位元組			極大整數值
FLOAT		4位元組			單精度
DOUBLE		8位元組			雙精度
DATE		YYYY-MM-DD		日期值
TIME		HH:MM:SS		時間值
YEAR		YYYY			年份值
DATETIME	YYYY-MM-DD HH:MM:SS	混合日期和時間值	insert into tweets values('2017-03-02 15:22:22');
TIMESTAMP	YYYYMMDDHHMMSS		時間戳
CHAR		0~255			定長CHAR(20)/20為長度
VARCHAR		0-65535			變長VARCHAR(20)/20為限制長度
TINYBLOB	0~255			小二進位制
TINYTEXT	0~255			小文字
BLOB		0~65535			二進位制
TEXT		0~65535			文字
MEDIUMBLOB	0-16777215		中二進位制
MEDIUMTEXT	0-16777215		中文字
LONGBLOB	0-4294967295		長二進位制
LONGTEXT	0-4294967295		長文字


刪除表/慎重
DROP TABLE students;


表的重新命名(不影響資料)/慎重
RENAME TABLE students TO new_name;


建立表/學生資訊/考試成績/雜貨店
CREATE TABLE students (
	id INT AUTO_INCREMENT PRIMARY KEY,
	first_name CHAR(20),
	last_name CHAR(20),
	email TEXT,
	phone VARCHAR(40),
	birthdate DATE,
	friend_id INT);
CREATE TABLE student_grades (
	student_id INT,
	test TEXT,
	grade INT);
CREATE TABLE groceries (
	id INT AUTO_INCREMENT PRIMARY KEY,
	name TEXT,
	quantity INT);


檢視資料庫所有表
SHOW TABLES;


查看錶結構
DESCRIBE students;
SHOW COLUMNS FROM students;


列的增加(不影響資料)
ALTER TABLE students ADD body TEXT;


列的重新定義(不影響資料)
ALTER TABLE students CHANGE COLUMN body health CHAR(20);


設定或刪除列的預設值
ALTER TABLE students ALTER COLUMN health SET DEFAULT 'OK';
ALTER TABLE students ALTER COLUMN health DROP DEFAULT;


指定刪除某行內容/慎重
DELETE FROM students WHERE id = 3;


加入資訊行/沒有標明的列必須要DEFAULT或者自增
INSERT INTO students (first_name,last_name,email,phone,birthdate,friend_id)
	VALUES ('Peter','Rabbit','
[email protected]
','555-6666','2002-06-24',2); INSERT INTO students (first_name,last_name,email,phone,birthdate,friend_id) VALUES ('Alice','Wonderland','[email protected]','555-4444','2002-07-04',1); INSERT INTO students (first_name,last_name,email,phone,birthdate,friend_id) VALUES ('Aladdin','Lampland','
[email protected]
','555-4443','2001-05-10',4); INSERT INTO students (first_name,last_name,email,phone,birthdate,friend_id) VALUES ('Simba','Kingston','[email protected]','555-1111','2001-12-24',3); INSERT INTO student_grades (student_id,test,grade) VALUES (1,'Nutrition',95); INSERT INTO student_grades (student_id,test,grade) VALUES (2,'Nutrition',92); INSERT INTO student_grades (student_id,test,grade) VALUES (1,'Chemistry',85); INSERT INTO student_grades (student_id,test,grade) VALUES (2,'Chemistry',95); INSERT INTO groceries VALUES (1,"Banana",4); INSERT INTO groceries VALUES (2,"Apple",1); INSERT INTO groceries VALUES (3,"Peach",2); 查看錶中所有資訊/檢視組合表資訊 SELECT * FROM students; SELECT * FROM students,student_grades; 查看錶中指定列的資訊 SELECT first_name,last_name,phone FROM students; 按照某個列排序查看錶中資訊/ASC升序DESC降序 SELECT * FROM groceries ORDER BY quantity ASC; 按照某列排序查看錶中資訊,附帶限定資訊/AND/OR SELECT * FROM groceries WHERE quantity > 1 AND quantity < 5 ORDER BY quantity DESC; 選定指定字串值的表中資訊/IN也可以NOT IN SELECT * FROM groceries WHERE name = "Banana" OR name="Apple" OR name="xxx"; SELECT * FROM groceries WHERE name IN ("Banana","Apple","xxx"); 選定某列的總和/也可以MIN,MAX/按照分組求和 SELECT SUM(quantity) FROM groceries; SELECT name,SUM(quantity) FROM groceries GROUP BY name; 巢狀選定 SELECT * FROM students WHERE id IN (SELECT student_id FROM student_grades); 按字串正則表示式選定 SELECT * FROM students WHERE phone LIKE "%444%"; 選定時列自定義標題AS,分組統計和 SELECT student_id,SUM(grade) AS total_grades FROM student_grades GROUP BY student_id; 按照id分組統計和,並從結果中選定滿足某個限定結果,GROUP後面才用HAVING SELECT student_id,SUM(grade) AS total_grades FROM student_grades GROUP BY student_id HAVING total_grades > 180; 使用GROUP分組後會自動記錄下該分組的資料行個數,用COUNT(*)即可獲得 SELECT student_id FROM student_grades GROUP BY student_id HAVING COUNT(*) >= 2; 使用CASE來為選定結果添加註釋 SELECT student_id,test, CASE WHEN grade >= 95 THEN "very good" WHEN grade >= 90 THEN "good" ELSE "ok" END AS "remark" FROM student_grades; 不同表中按照某值匹配檢視/後者效率更高並且可以再加個WHERE SELECT * FROM students,student_grades WHERE student_grades.student_id = students.id; SELECT * FROM students JOIN student_grades ON student_grades.student_id = students.id WHERE grade > 90; 不同表中按照某值匹配檢視,若無匹配也顯示 SELECT * FROM students LEFT OUTER JOIN student_grades ON student_grades.student_id = students.id; 更新某行中的某列資訊 UPDATE students SET email='
[email protected]
' WHERE id = 4; 在同一張表中交叉查詢/自我JOIN SELECT students.first_name,students.last_name,friend.email AS friend_email FROM students JOIN students friend ON students.friend_id = friend.id; 在多個不同表中交叉查詢/要把SELECT id,quantity FROM groceries;中的兩列數字全用對應id學生的姓氏代替 SELECT a.first_name,b.first_name FROM groceries JOIN students a ON groceries.id = a.id JOIN students b ON groceries.quantity = b.id; //**[email protected]**//