轉載自https://www.runoob.com/sql/sql-tutorial.html 本文只用於我的學習筆記用php
SQL 是用於訪問和處理數據庫的標準的計算機語言。html
用於從數據庫中選取數據。python
SELECT column_name,column_name FROM table_name;mysql
SELECT * FROM table_name;linux
mysql> SELECT * FROM tbl; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | +-----------+--------------+---------------+-----------------+---+ 5 rows in set (0.00 sec) mysql> mysql> mysql> SELECT runoob_title,runoob_author FROM tbl; +--------------+---------------+ | runoob_title | runoob_author | +--------------+---------------+ | learn mysql | 1111111 | | pyhon | aaa | | learn mysql | aaa | | php | 444 | | linux | bbb | +--------------+---------------+ 5 rows in set (0.00 sec) mysql>
用於返回惟一不一樣的值。web
SELECT DISTINCT column_name, column_name FROM table_name;正則表達式
mysql> SELECT DISTINCT runoob_title FROM tbl; +--------------+ | runoob_title | +--------------+ | learn mysql | | pyhon | | php | | linux | +--------------+ 4 rows in set (0.00 sec) mysql>
用於提取那些知足指定條件的記錄。sql
SELECT column_name, column_name FROM table_name WHERE column_name operator value;數據庫
mysql> SELECT * FROM tbl WHERE runoob_title = 'learn mysql'; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | +-----------+--------------+---------------+-----------------+---+ 2 rows in set (0.00 sec) mysql>
AND & OR 運算符用於基於一個以上的條件對記錄進行過濾。apache
若是第一個條件和第二個條件都成立,則 AND 運算符顯示一條記錄。
若是第一個條件和第二個條件中只要有一個成立,則 OR 運算符顯示一條記錄。
mysql> SELECT * FROM tbl WHERE runoob_title = 'learn mysql' OR runoob_author = 'aaa'; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | +-----------+--------------+---------------+-----------------+---+ 3 rows in set (0.00 sec) mysql> mysql> SELECT * FROM tbl WHERE runoob_title = 'learn mysql' AND runoob_author = 'aaa'; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 6 | learn mysql | aaa | 2018-01-10 | 0 | +-----------+--------------+---------------+-----------------+---+ 1 row in set (0.00 sec) mysql>
關鍵字用於對結果集進行排序。
SELECT column_name, column_name
FROM table_name
ORDER BY column_name, column_name ASC | DESC;
mysql> SELECT * FROM tbl ORDER BY runoob_author; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | +-----------+--------------+---------------+-----------------+---+ 5 rows in set (0.00 sec) mysql> mysql> SELECT * FROM tbl ORDER BY runoob_author DESC; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 11 | linux | bbb | 2019-01-01 | 2 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | +-----------+--------------+---------------+-----------------+---+ 5 rows in set (0.00 sec) mysql>
用於向表中插入新記錄。
INSERT INTO table_name VALUES (value1,value2,value3,...);
INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
mysql> INSERT INTO tbl VALUES ('17','mysql','3333','2017-09-05',7),('18','mysql','444','2017-09-05',8),('19','mysql','555','2017-11-05',9); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> mysql> select * from tbl; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | | 17 | mysql | 3333 | 2017-09-05 | 7 | | 18 | mysql | 444 | 2017-09-05 | 8 | | 19 | mysql | 555 | 2017-11-05 | 9 | +-----------+--------------+---------------+-----------------+---+ 8 rows in set (0.00 sec) mysql> mysql> INSERT INTO tbl ( runoob_title , runoob_author , submission_date , j) VALUES ('mysql','2222','2017-05-05',6); Query OK, 1 row affected (0.00 sec) mysql> select * from tbl; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | | 17 | mysql | 3333 | 2017-09-05 | 7 | | 18 | mysql | 444 | 2017-09-05 | 8 | | 19 | mysql | 555 | 2017-11-05 | 9 | | 20 | mysql | 2222 | 2017-05-05 | 6 | +-----------+--------------+---------------+-----------------+---+ 9 rows in set (0.00 sec) mysql>
用於更新表中的記錄。
UPDATE table_name SET column1=value1, column2=value2,... WHERE some_column=some_value;
/*在更新記錄時要格外當心!若是咱們省略了 WHERE 子句,執行代碼會將表中全部數據都更改,執行沒有 WHERE 子句的 UPDATE 要慎重,再慎重。*/
mysql> UPDATE tbl sET runoob_title = 'SQL', runoob_author = 'AAA' where runoob_id = 20; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from tbl; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | | 17 | mysql | 3333 | 2017-09-05 | 7 | | 18 | mysql | 444 | 2017-09-05 | 8 | | 19 | mysql | 555 | 2017-11-05 | 9 | | 20 | SQL | AAA | 2017-05-05 | 6 | +-----------+--------------+---------------+-----------------+---+ 9 rows in set (0.00 sec) mysql>
用於刪除表中的記錄。
DELETE FROM table_name WHERE some_column = some_value;
WHERE 子句規定哪條記錄或者哪些記錄須要刪除。若是您省略了 WHERE 子句,全部的記錄都將被刪除!
註釋:在刪除記錄時要格外當心!由於您不能重來!
mysql> DELETE FROM tbl where runoob_id = 19; Query OK, 1 row affected (0.01 sec) mysql> select * from tbl; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | | 17 | mysql | 3333 | 2017-09-05 | 7 | | 18 | mysql | 444 | 2017-09-05 | 8 | | 20 | SQL | AAA | 2017-05-05 | 6 | +-----------+--------------+---------------+-----------------+---+ 8 rows in set (0.00 sec) mysql>
SELECT TOP 子句用於規定要返回的記錄的數目。
SELECT TOP 子句對於擁有數千條記錄的大型表來講,是很是有用的。
注意:並不是全部的數據庫系統都支持 SELECT TOP 語句。 MySQL 支持 LIMIT 語句來選取指定的條數數據, Oracle 可使用 ROWNUM 來選取。
SQL Server / MS Access 語法
SELECT TOP number | percent column_name(s) FROM table_name;
MYSQL語法
SELECT column_name(s) FROM table_name LIMIT number;
Oracle語法
SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;
mysql> select * from tbl LIMIT 5; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | +-----------+--------------+---------------+-----------------+---+ 5 rows in set (0.00 sec) mysql>
LIKE 操做符用於在 WHERE 子句中搜索列中的指定模式。
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
mysql> SELECT * FROM tbl WHERE runoob_title like '%sql'; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 17 | mysql | 3333 | 2017-09-05 | 7 | | 18 | mysql | 444 | 2017-09-05 | 8 | | 20 | SQL | AAA | 2017-05-05 | 6 | +-----------+--------------+---------------+-----------------+---+ 5 rows in set (0.00 sec) mysql>
通配符與SQL LIKE操做符一塊兒使用,可用於替代字符串中的任何其餘字符
% 代替0個或多個字符
- 代替一個字符
[charlist] 字符列中的任何單一字符
[^charlist] 或 [!charlist] 不在字符列中的任何單一字符
MySQL 、SQLite 只支持 % 和 _ 通配符,不支持 [^charlist] 或 [!charlist]
MySQL 中要完成 [^charlist] 或 [!charlist] 通配符的查詢效果,須要經過正則表達式 REGEXP 來完成。
IN 操做符容許您在 WHERE 子句中規定多個值。
SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...);
mysql> SELECT * FROM tbl WHERE runoob_title IN ('sql','php','linux'); +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | | 20 | SQL | AAA | 2017-05-05 | 6 | +-----------+--------------+---------------+-----------------+---+ 3 rows in set (0.00 sec) mysql>
BETWEEN 操做符用於選取介於兩個值之間的數據範圍內的值。
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
mysql> SELECT * FROM tbl WHERE j BETWEEN 2 AND 7; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 11 | linux | bbb | 2019-01-01 | 2 | | 20 | SQL | AAA | 2017-05-05 | 6 | | 17 | mysql | 3333 | 2017-09-05 | 7 | +-----------+--------------+---------------+-----------------+---+ 3 rows in set (0.00 sec) mysql>
如需顯示不在上面實例範圍內的數據,請使用 NOT BETWEEN:
mysql> SELECT * FROM tbl WHERE j NOT BETWEEN 2 AND 7; +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 18 | mysql | 444 | 2017-09-05 | 8 | +-----------+--------------+---------------+-----------------+---+ 5 rows in set (0.00 sec) mysql>
經過使用 SQL,能夠爲表名稱或列名稱指定別名。
列的別名:
SELECT column_name AS alias_name FROM table_name;
表的別名:
SELECT column_name(s) FROM table_name AS alias_name;
mysql> SELECT runoob_title, runoob_title AS alias_tile FROM tbl; +--------------+-------------+ | runoob_title | alias_tile | +--------------+-------------+ | learn mysql | learn mysql | | pyhon | pyhon | | learn mysql | learn mysql | | php | php | | linux | linux | | mysql | mysql | | mysql | mysql | | SQL | SQL | +--------------+-------------+ 8 rows in set (0.00 sec) mysql>
mysql> SELECT F.runoob_title, L.runoob_author -> FROM tbl AS F, tb2 AS L -> WHERE F.runoob_id = L.runoob_id; +--------------+---------------+ | runoob_title | runoob_author | +--------------+---------------+ | learn mysql | keepalive | | pyhon | svn | | learn mysql | aaa | | php | 333 | +--------------+---------------+ 4 rows in set (0.00 sec) mysql>
用於把來自兩個或多個表的行結合起來
SQL JOIN 子句用於把來自兩個或多個表的行結合起來,基於這些表之間的共同字段。
在表中存在至少一個匹配時返回行。
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
或
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
註釋: INNER JOIN 和 JION是相同的。
mysql> SELECT * FROM tcount_tbl;SELECT * FROM tbl; +--------------+--------------+ | runoob_title | runoob_count | +--------------+--------------+ | learn mysql | 22 | | learn mysql | 4 | | python | 5 | | php | 5 | | zabbix | 5 | | apache | 5 | +--------------+--------------+ 6 rows in set (0.00 sec) +-----------+--------------+---------------+-----------------+---+ | runoob_id | runoob_title | runoob_author | submission_date | j | +-----------+--------------+---------------+-----------------+---+ | 2 | learn mysql | 1111111 | 2018-01-10 | 0 | | 4 | pyhon | aaa | 2018-10-10 | 0 | | 6 | learn mysql | aaa | 2018-01-10 | 0 | | 10 | php | 444 | 2018-10-10 | 0 | | 11 | linux | bbb | 2019-01-01 | 2 | | 17 | mysql | 3333 | 2017-09-05 | 7 | | 18 | mysql | 444 | 2017-09-05 | 8 | | 20 | SQL | AAA | 2017-05-05 | 6 | +-----------+--------------+---------------+-----------------+---+ 8 rows in set (0.00 sec) mysql> mysql> mysql> SELECT tbl.runoob_title, tbl.runoob_author, tbl.submission_date, tcount_tbl.runoob_count -> FROM tbl -> INNER JOIN tcount_tbl -> ON tbl.runoob_title=tcount_tbl.runoob_title; +--------------+---------------+-----------------+--------------+ | runoob_title | runoob_author | submission_date | runoob_count | +--------------+---------------+-----------------+--------------+ | learn mysql | 1111111 | 2018-01-10 | 22 | | learn mysql | 1111111 | 2018-01-10 | 4 | | learn mysql | aaa | 2018-01-10 | 22 | | learn mysql | aaa | 2018-01-10 | 4 | | php | 444 | 2018-10-10 | 5 | +--------------+---------------+-----------------+--------------+ 5 rows in set (0.00 sec) mysql>
LEFT JOIN 關鍵字從左表(table1)返回全部的行,即便右表(table2)中沒有匹配。若是右表中沒有匹配,則結果爲 NULL。
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
或
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
註釋:在某些數據庫中,LEFT JOIN稱爲LEFT OUTER JOIN。
mysql> SELECT tbl.runoob_title, tbl.runoob_author, tbl.submission_date, tcount_tbl.runoob_count -> FROM tbl -> LEFT JOIN tcount_tbl -> ON tbl.runoob_title=tcount_tbl.runoob_title; +--------------+---------------+-----------------+--------------+ | runoob_title | runoob_author | submission_date | runoob_count | +--------------+---------------+-----------------+--------------+ | learn mysql | 1111111 | 2018-01-10 | 22 | | learn mysql | 1111111 | 2018-01-10 | 4 | | pyhon | aaa | 2018-10-10 | NULL | | learn mysql | aaa | 2018-01-10 | 22 | | learn mysql | aaa | 2018-01-10 | 4 | | php | 444 | 2018-10-10 | 5 | | linux | bbb | 2019-01-01 | NULL | | mysql | 3333 | 2017-09-05 | NULL | | mysql | 444 | 2017-09-05 | NULL | | SQL | AAA | 2017-05-05 | NULL | +--------------+---------------+-----------------+--------------+ 10 rows in set (0.00 sec) mysql>
RIGHT JOIN 關鍵字從右表(table2)返回全部的行,即便左表(table1)中沒有匹配。若是左表中沒有匹配,則結果爲 NULL。
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
mysql> SELECT tbl.runoob_title, tbl.runoob_author, tbl.submission_date, tcount_tbl.runoob_count -> FROM tbl -> RIGHT JOIN tcount_tbl -> ON tbl.runoob_title=tcount_tbl.runoob_title; +--------------+---------------+-----------------+--------------+ | runoob_title | runoob_author | submission_date | runoob_count | +--------------+---------------+-----------------+--------------+ | learn mysql | 1111111 | 2018-01-10 | 22 | | learn mysql | aaa | 2018-01-10 | 22 | | learn mysql | 1111111 | 2018-01-10 | 4 | | learn mysql | aaa | 2018-01-10 | 4 | | NULL | NULL | NULL | 5 | | php | 444 | 2018-10-10 | 5 | | NULL | NULL | NULL | 5 | | NULL | NULL | NULL | 5 | +--------------+---------------+-----------------+--------------+ 8 rows in set (0.00 sec) mysql>
FULL OUTER JOIN 關鍵字只要左表(table1)和右表(table2)其中一個表中存在匹配,則返回行.
FULL OUTER JOIN 關鍵字結合了 LEFT JOIN 和 RIGHT JOIN 的結果。
SELECT column_name(s)
FROM table1
FULL OUTER JION table2
ON table1.column_name=table2.column_name;
MySQL不支持全關聯
UNION 操做符用於合併兩個或多個 SELECT 語句的結果集。
請注意,UNION 內部的每一個 SELECT 語句必須擁有相同數量的列。列也必須擁有類似的數據類型。同時,每一個 SELECT 語句中的列的順序必須相同。
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
註釋:默認地,UNION 操做符選取不一樣的值。若是容許重複的值,請使用 UNION ALL。
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
註釋:UNION 結果集中的列名老是等於 UNION 中第一個 SELECT 語句中的列名。
mysql> SELECT runoob_title FROM tbl -> UNION -> SELECT runoob_title FROM tcount_tbl -> ; +--------------+ | runoob_title | +--------------+ | learn mysql | | pyhon | | php | | linux | | mysql | | SQL | | python | | zabbix | | apache | +--------------+ 9 rows in set (0.00 sec) mysql> mysql> SELECT runoob_title FROM tbl UNION ALL SELECT runoob_title FROM tcount_tbl; +--------------+ | runoob_title | +--------------+ | learn mysql | | pyhon | | learn mysql | | php | | linux | | mysql | | mysql | | SQL | | learn mysql | | learn mysql | | python | | php | | zabbix | | apache | +--------------+ 14 rows in set (0.00 sec) mysql>
SELECT INTO 語句從一個表複製數據,而後把數據插入到另外一個新表中
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
MySQL 數據庫不支持 SELECT ... INTO 語句,但支持 INSERT INTO ... SELECT 。
固然你可使用如下語句來拷貝表結構及數據:
CREATE TABLE new_table AS SELECT * FROM old_table;
從一個表複製數據,而後把數據插入到一個已存在的表中。目標表中任何已存在的行都不受影響。
INSERT INTO table2
(column_name(s))
SELCEET column_name(s)
FROM table1;
建立數據庫。
CREATE DATABASE dbname;
mysql> CREATE DATABASE testdb; Query OK, 1 row affected (0.00 sec) mysql>
建立數據庫中的表。表由行和列組成,每一個表都必須有個表名。
CREATE TABLE table_name
(
column_name1 date_type(size),
column_name1 date_type(size),
column_name1 date_type(size),
...
);
column_name 參數規定表中列的名稱。
data_type 參數規定列的數據類型(例如 varchar、integer、decimal、date 等等)。
size 參數規定表中列的最大長度。
mysql> use testdb; Database changed mysql> mysql> mysql> CREATE TABLE testtbl -> ( -> personid int, -> lastname varchar(255), -> firstname varchar(255), -> birthday date -> ); Query OK, 0 rows affected (0.00 sec) mysql>
SQL 約束用於規定表中的數據規則。
若是存在違反約束的數據行爲,行爲會被約束終止。
約束能夠在建立表時規定(經過 CREATE TABLE 語句),或者在表建立以後規定(經過 ALTER TABLE 語句)。
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
在SQL中,有以下約束:
約束強制列不接受 NULL 值。
NOT NULL 約束強制字段始終包含值。這意味着,若是不向字段添加值,就沒法插入新記錄或者更新記錄。
mysql> CREATE TABLE test2 ( -> id int NOT NULL, -> name varchar(255) not null, -> age int -> ); Query OK, 0 rows affected (0.00 sec) mysql>
添加NOT NULL約束
mysql> ALTER TABLE test2 -> MODIFY age int NOT NULL; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> desc test2; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(255) | NO | | NULL | | | age | int(11) | NO | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
刪除NOT NULL約束
mysql> alter table test2 -> modify age int null; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc test2; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(255) | NO | | NULL | | | age | int(11) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
UNIQUE 約束惟一標識數據庫表中的每條記錄。
UNIQUE 和 PRIMARY KEY 約束均爲列或列集合提供了惟一性的保證。
PRIMARY KEY 約束擁有自動定義的 UNIQUE 約束。
請注意,每一個表能夠有多個 UNIQUE 約束,可是每一個表只能有一個 PRIMARY KEY 約束。
mysql> CREATE TABLE test3 -> ( -> id int not null, -> name varchar(20) not null, -> age int, -> unique (id) -> ); Query OK, 0 rows affected (0.00 sec) mysql> desc test3; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
建立UNIQUE約束
ALTER TABLE teable_name,
ADD UNIQUE (column_name);
撤銷UNIQUE約束
ALTER TABLE table_name,
DROP INDEX column_name;
PRIMARY KEY 約束惟一標識數據庫表中的每條記錄。
主鍵必須包含惟一的值。
主鍵列不能包含 NULL 值。
每一個表都應該有一個主鍵,而且每一個表只能有一個主鍵。
mysql> CREATE TABLE test4 -> ( -> id int not null, -> name varchar(20) not null, -> age int, -> unique (name), -> primary key (id) -> ); Query OK, 0 rows affected (0.00 sec) mysql> mysql> desc test4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | UNI | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
建立PRIMARY KEY約束
ALER TABLE table_name
ADD PRIMARY KEY (column_name);
撤銷PRIMARY KEY約束
ALTER TABLE table_name
DROP PRIMARY KEY;
一個表中的 FOREIGN KEY 指向另外一個表中的 UNIQUE KEY(惟一約束的鍵)。
mysql> CREATE TABLE test5 ( -> o_id int not null, -> teststr varchar(20), -> f_id int not null, -> PRIMARY KEY (o_id), -> FOREIGN KEY (f_id) REFERENCES test4(id) -> ); Query OK, 0 rows affected (0.00 sec) mysql> mysql> desc test5; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | o_id | int(11) | NO | PRI | NULL | | | teststr | varchar(20) | YES | | NULL | | | f_id | int(11) | NO | MUL | NULL | | +---------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> mysql>
ALTER建立表的FOREIGN KEY約束
ALTER TABLE table1_name
ADD FOREIGN KEY (P_id)
REFERENCES table2_name(p_id);
ALTER撤銷表的FOREIGN KEY約束
ALTER TABLE table_name
DROP FOREIGN KEY P_id;
CHECK 約束用於限制列中的值的範圍。
若是對單個列定義 CHECK 約束,那麼該列只容許特定的值。
若是對一個表定義 CHECK 約束,那麼此約束會基於行中其餘列的值在特定的列中對值進行限制。
mysql> create table test6 -> ( -> id int not null, -> str varchar(10), -> check (id>0) -> ); Query OK, 0 rows affected (0.00 sec) mysql>
DEFAULT 約束用於向列中插入默認值。
若是沒有規定其餘的值,那麼會將默認值添加到全部的新記錄。
mysql> create table test7 ( -> id int not null, -> name varchar(100), -> sex varchar(5) default 'M' -> ); Query OK, 0 rows affected (0.01 sec) mysql> mysql> mysql> desc test7; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(100) | YES | | NULL | | | sex | varchar(5) | YES | | M | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
CREATE INDEX 語句用於在表中建立索引。
在不讀取整個表的狀況下,索引使數據庫應用程序能夠更快地查找數據。
註釋:更新一個包含索引的表須要比更新一個沒有索引的表花費更多的時間,這是因爲索引自己也須要更新。所以,理想的作法是僅僅在經常被搜索的列(以及表)上面建立索引。
SQL CREATE INDEX 語法
在表上建立一個簡單的索引。容許使用重複的值:
CREATE INDEX index_name
ON table_name (column_name);
SQL CREATE UNIQUE INDEX 語法
在表上建立一個惟一的索引。不容許使用重複的值:惟一的索引意味着兩個行不能擁有相同的索引值。Creates a unique index on a table. Duplicate values are not allowed:
CREATE UNIQUE INDEX index_name
ON table_name (column_name);
經過使用 DROP 語句,能夠輕鬆地刪除索引、表和數據庫。
ALTER TABLE table_name DROP INDEX index_name;
DROP TABLE table_name;
DROP DATABASE database_name;
若是是僅僅須要刪除表內數據,但不刪除表自己,使用 TRUNCATE TABLE 語句。
TRUNCATE TABLE table_name;
ALTER TABLE 語句用於在已有的表中添加、刪除或修改列。
在表中添加列
ALTER TABLE table_name ADD column_name datatype;
刪除列
ALTER TABLE table_name DROP COLUMN column_name;
改變列的數據類型
ALTER TABLE table_name ALTER COLUMN column_name datatype;
Auto-increment 會在新記錄插入表中時生成一個惟一的數字,自動地建立主鍵字段的值。
mysql> CREATE TABLE test8 ( -> id int not null auto_increment, -> name varchar(50), -> PRIMARY KEY(id) -> ); Query OK, 0 rows affected (0.00 sec) mysql> desc test8; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql>
在 SQL 中,視圖是基於 SQL 語句的結果集的可視化的表。
視圖包含行和列,就像一個真實的表。視圖中的字段就是來自一個或多個數據庫中的真實的表中的字段。
您能夠向視圖添加 SQL 函數、WHERE 以及 JOIN 語句,也能夠呈現數據,就像這些數據來自於某個單一的表同樣。
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
mysql> CREATE VIEW testview AS -> SELECT name FROM test8 ; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 239319 Current database: testdb Query OK, 0 rows affected (0.01 sec) mysql> mysql> SELECT * FROM testview; +------+ | name | +------+ | zhu | | zhu | | zhu | | zhu | +------+ 4 rows in set (0.00 sec) mysql>
更新視圖
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
撤銷視圖
DROP VIEW view_name;
MySQL Date 函數
下面的表格列出了 MySQL 中最重要的內建日期函數:
SQL Date 數據類型
MySQL 使用下列數據類型在數據庫中存儲日期或日期/時間值:
NULL 值表明遺漏的未知數據。
默認地,表的列能夠存放 NULL 值。
若是表中的某個列是可選的,那麼咱們能夠在不向該列添加值的狀況下插入新記錄或更新已有的記錄。這意味着該字段將以 NULL 值保存。
NULL 值的處理方式與其餘值不一樣。
NULL 用做未知的或不適用的值的佔位符。
註釋:沒法比較 NULL 和 0;它們是不等價的。
測試 NULL 值必須使用 IS NULL 和 IS NOT NULL 操做符。
ISNULL()、NVL()、IFNULL() 和 COALESCE() 函數
--若是alexa列爲null值,則賦予0,不然,取原值 select id,name,url,ifnull(alexa,0)from websites; select id,name,url,COALESCE(alexa,0) from websites;
數據類型定義列中存放的值的種類。
數據庫表中的每一個列都要求有名稱和數據類型。SQL 開發人員必須在建立 SQL 表時決定表中的每一個列將要存儲的數據的類型。數據類型是一個標籤,是便於 SQL 瞭解每一個列指望存儲什麼類型的數據的指南,它也標識了 SQL 如何與存儲的數據進行交互。
然而,不一樣的數據庫對數據類型定義提供不一樣的選擇。
下面的表格顯示了各類不一樣的數據庫平臺上一些數據類型的通用名稱:
在 MySQL 中,有三種主要的類型:Text(文本)、Number(數字)和 Date/Time(日期/時間)類型。
注意:以上的 size 表明的並非存儲在數據庫中的具體的長度,如 int(4) 並非只能存儲4個長度的數字。
實際上int(size)所佔多少存儲空間並沒有任何關係。int(3)、int(4)、int(8) 在磁盤上都是佔用 4 btyes 的存儲空間。就是在顯示給用戶的方式有點不一樣外,int(M) 跟 int 數據類型是相同的。
例如:
一、int的值爲10 (指定zerofill)
int(9)顯示結果爲000000010 int(3)顯示結果爲010
就是顯示的長度不同而已 都是佔用四個字節的空間