史上最簡單MySQL教程詳解(基礎篇)之SQL語句以及預留關鍵字介紹

SQL簡單介紹

前面咱們已經介紹過,經過編寫命令的方式實現對數據庫的操做,這個以字符串的形式存在的命令被稱爲查詢(Query).。當咱們編寫這些Query的時候,這些語句就被稱爲SQL語句。SQL表明結構化查詢語言(Structured Query Language)。SQL是用於訪問數據庫的標準化語言。SQL語句主要分爲如下三種類型的語句:mysql

數據操做語句 (Data Manipulation Language,DML)

這個就是咱們接下來將主要介紹的重點了,主要包括了【SELECT】,【INSERT】,【UPDATE】,【DELETE】語句。web

數據定義語句 (Data Definition Language ,DDL)

這個前面咱們已經接觸很是多的對於表,索引,視圖等使用的【CREATE】,【DROP】,【ALTER】等語句。sql

數據控制語句 (Data Control Language ,DCL)

這個就是咱們前面在用戶管理時接觸到的【GRANT】,【REVOKE】;還有咱們後面高級教程中事物處理即將使用到的【COMMIT】,【BEGIN】,【COMMIT】,【ROLLBACK】等語句。數據庫

這裏只須要對於SQL的分類有個大概的瞭解就能夠了。不須要你們深刻了解。svg

SQL語句使用規則(監視器)

規則一:SQL語句必須有結束符

其實在前面咱們已經對於在監視器中敲SQL命令y有了一個比較深入的感受了。一般的SQL語句是必須以【;】做爲語句的結束符,若是在輸入SQL語句的時候忘記輸入告終束符,監視器會認爲用戶還沒有輸入完成,會自動跳到下一行繼續等待用戶輸入,這個時候只須要將結尾符補上便可。固然,結尾不單單隻有【;】還有【\G】這種形式,具體的使用狀況前面咱們也已經進行了舉例說明。server

規則二:保留關鍵字不區分大小寫關鍵字

保留關鍵字是SQL事先定義好了的,如咱們以前已經提到的【SELECT】,【ALTER】等就屬於關鍵字的範圍。在SQL語句中這些關鍵字是不區分大小寫的。可是爲了讓SQL語句更加方便閱讀和理解,咱們一般:以大寫的形式表達預留的關鍵字,以小寫的形式表示數據庫,表,和列名。同時,當咱們在建立表,列時,儘可能要避免咱們的命名與保留字重複或者衝突,以避免形成不可預知的錯誤,這樣錯誤的排查將變得十分的困難,我也將在後面貼出MySQL5.7版本的預留關鍵字,你們能夠大體瞭解一下。xml

規則三:可自由地加入空白或者換行符

一個SQL語句能夠做爲一行來編寫,可是當咱們編寫比較複雜的SQL語句的時候,就會發現變得異常的麻煩,因此SQL就加入了這一特色。可是須要注意的是在一個關鍵字的中間加入空格或者換行符是不容許的
例如:教程

mysql> sel
-> ect * from user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘sel
ect * from user’ at line 1索引

規則四:使用【- -】或者【/*…*/】註釋

【- -】主要是用於單行註釋,【/*…*/】主要是進行多行註釋ip

MySQL5.7的保留關鍵字一覽表

關鍵字 關鍵字 關鍵字
ACCESSIBLE (R) ACCOUNT ACTION
ADD (R) AFTER AGAINST
AGGREGATE ALGORITHM ALL (R)
ALTER (R) ALWAYS ANALYSE
ANALYZE (R) AND (R) ANY
AS (R) ASC (R) ASCII
ASENSITIVE (R) AT AUTOEXTEND_SIZE
AUTO_INCREMENT AVG AVG_ROW_LENGTH
BACKUP BEFORE (R) BEGIN
BETWEEN (R) BIGINT (R) BINARY (R)
BINLOG BIT BLOB (R)
BLOCK BOOL BOOLEAN
BOTH (R) BTREE BY (R)
BYTE CACHE CALL (R)
CASCADE (R) CASCADED CASE (R)
CATALOG_NAME CHAIN CHANGE (R)
CHANGED CHANNEL[C] CHAR (R)
CHARACTER (R) CHARSET CHECK (R)
CHECKSUM CIPHER CLASS_ORIGIN
CLIENT CLOSE COALESCE
CODE COLLATE (R) COLLATION
COLUMN (R) COLUMNS COLUMN_FORMAT
COLUMN_NAME COMMENT COMMIT
COMMITTED COMPACT COMPLETION
COMPRESSED COMPRESSION並[d] CONCURRENT
CONDITION (R) CONNECTION CONSISTENT
CONSTRAINT (R) CONSTRAINT_CATALOG CONSTRAINT_NAME
1CONSTRAINT_SCHEMA CONTAINS CONTEXT
CONTINUE (R) CONVERT (R) CPU
CREATE (R) CROSS (R) CUBE
CURRENT CURRENT_DATE (R) CURRENT_TIME (R)
CURRENT_TIMESTAMP (R) CURRENT_USER (R) CURSOR (R)
CURSOR_NAME DATA DATABASE (R)
DATABASES (R) DATAFILE DATE
DATETIME DAY DAY_HOUR (R)
DAY_MICROSECOND (R) DAY_MINUTE (R) DAY_SECOND (R)
DEALLOCATE DEC (R) DECIMAL (R)
DECLARE (R) DEFAULT (R) DEFAULT_AUTH
DEFINER DELAYED (R)
DELETE (R) DESC (R) DESCRIBE (R)
DES_KEY_FILE DETERMINISTIC (R) DIAGNOSTICS
DIRECTORY DISABLE DISCARD
DISK DISTINCT (R) DISTINCTROW (R)
DIV (R) DO DOUBLE (R)
DROP (R) DUAL (R) DUMPFILE
DUPLICATE DYNAMIC EACH (R)
ELSE (R) ELSEIF (R) ENABLE
ENCLOSED (R) ENCRYPTION[e] END
ENDS ENGINE ENGINES
ENUM ERROR ERRORS
ESCAPE ESCAPED (R) EVENT
EVENTS EVERY EXCHANGE
EXECUTE EXISTS (R) EXIT (R)
EXPANSION EXPIRE EXPLAIN (R)
EXPORT EXTENDED EXTENT_SIZE
FALSE (R) FAST FAULTS
FETCH (R) FIELDS FILE
FILE_BLOCK_SIZE[F] FILTER[G] FIRST
FIXED FLOAT (R) FLOAT4 (R)
FLOAT8 (R) FLUSH FOLLOWS[H]
FOR (R) FORCE (R) FOREIGN (R)
FORMAT FOUND FROM (R)
FULL FULLTEXT (R) FUNCTION
GENERAL GENERATED[i](R) GEOMETRY
GEOMETRYCOLLECTION GET (R) GET_FORMAT
GLOBAL GRANT (R) GRANTS
GROUP (R) GROUP_REPLICATION[j] HANDLER
HASH HAVING (R) HELP
HIGH_PRIORITY (R) HOST HOSTS
HOUR HOUR_MICROSECOND (R) HOUR_MINUTE (R)
HOUR_SECOND (R) IDENTIFIED IF (R)
IGNORE (R) IGNORE_SERVER_IDS IMPORT
IN (R) INDEX (R) INDEXES
INFILE (R) INITIAL_SIZE INNER (R)
INOUT (R) INSENSITIVE (R) INSERT (R)
INSERT_METHOD INSTALL INSTANCE[K]
INT (R) INT1 (R) INT2 (R)
INT3 (R) INT4 (R) INT8 (R)
INTEGER (R) INTERVAL (R) INTO (R)
INVOKER IO IO_AFTER_GTIDS (R)
IO_BEFORE_GTIDS (R) IO_THREAD IPC
IS (R) ISOLATION ISSUER
ITERATE (R) JOIN (R) JSON[1]
KEY (R) KEYS (R) KEY_BLOCK_SIZE
KILL (R) LANGUAGE LAST
LEADING (R) LEAVE (R) LEAVES
LEFT (R) LESS LEVEL
LIKE (R) LIMIT (R) LINEAR (R)
LINES (R) LINESTRING LIST
LOAD (R) LOCAL LOCALTIME (R)
LOCALTIMESTAMP (R) LOCK (R) LOCKS
LOGFILE LOGS LONG (R)
LONGBLOB (R) LONGTEXT (R) LOOP (R)
LOW_PRIORITY (R) MASTER MASTER_AUTO_POSITION
MASTER_BIND (R) MASTER_CONNECT_RETRY MASTER_DELAY
MASTER_HEARTBEAT_PERIOD MASTER_HOST MASTER_LOG_FILE
MASTER_LOG_POS MASTER_PASSWORD MASTER_PORT
MASTER_RETRY_COUNT MASTER_SERVER_ID MASTER_SSL
MASTER_SSL_CA MASTER_SSL_CAPATH MASTER_SSL_CERT
MASTER_SSL_CIPHER MASTER_SSL_CRL MASTER_SSL_CRLPATH
MASTER_SSL_KEY MASTER_SSL_VERIFY_SERVER_CERT (R) MASTER_TLS_VERSION
MASTER_USER MATCH (R) MAXVALUE (R)
MAX_CONNECTIONS_PER_HOUR MAX_QUERIES_PER_HOUR MAX_ROWS
MAX_SIZE MAX_STATEMENT_TIME[n] MAX_UPDATES_PER_HOUR
MAX_USER_CONNECTIONS MEDIUM MEDIUMBLOB (R)
MEDIUMINT (R) MEDIUMTEXT (R) MEMORY
MERGE MESSAGE_TEXT MICROSECOND
MIDDLEINT (R) MIGRATE MINUTE
MINUTE_MICROSECOND (R) MINUTE_SECOND (R) MIN_ROWS
MOD (R) MODE MODIFIES (R)
MODIFY MONTH MULTILINESTRING
MULTIPOINT MULTIPOLYGON MUTEX
MYSQL_ERRNO NAME NAMES
NATIONAL NATURAL (R) NCHAR
NDB NDBCLUSTER NEVER[O]
NEW NEXT NO
NODEGROUP NONBLOCKING[P] NONE
NOT (R) NO_WAIT NO_WRITE_TO_BINLOG (R)
NULL (R) NUMBER NUMERIC (R)
NVARCHAR OFFSET OLD_PASSWORD[Q]
ON (R) ONE ONLY
OPEN OPTIMIZE (R) OPTIMIZER_COSTS[r](R)
OPTION (R) OPTIONALLY (R) OPTIONS
OR (R) ORDER (R) OUT (R)
OUTER (R) OUTFILE (R) OWNER
PACK_KEYS PAGE PARSER
PARSE_GCOL_EXPR[S] PARTIAL PARTITION (R)
PARTITIONING PARTITIONS PASSWORD
PHASE PLUGIN PLUGINS
PLUGIN_DIR POINT POLYGON
PORT PRECEDES[T] PRECISION (R)
PREPARE PRESERVE PREV
PRIMARY (R) PRIVILEGES PROCEDURE (R)
PROCESSLIST PROFILE PROFILES
PROXY PURGE (R) QUARTER
QUERY QUICK RANGE(R)
READ (R) READS (R) READ_ONLY
READ_WRITE (R) REAL (R) REBUILD
RECOVER REDOFILE REDO_BUFFER_SIZE
REDUNDANT REFERENCES (R) REGEXP (R)
RELAY RELAYLOG RELAY_LOG_FILE
RELAY_LOG_POS RELAY_THREAD RELEASE (R)
RELOAD REMOVE RENAME (R)
REORGANIZE REPAIR REPEAT (R)
REPEATABLE REPLACE (R) REPLICATE_DO_DB[U]
REPLICATE_DO_TABLE[V] REPLICATE_IGNORE_DB[W] REPLICATE_IGNORE_TABLE[X]
REPLICATE_REWRITE_DB[Y] REPLICATE_WILD_DO_TABLE[Z] REPLICATE_WILD_IGNORE_TABLE[AA]
REPLICATION REQUIRE (R) RESET
RESIGNAL (R) RESTORE RESTRICT (R)
RESUME RETURN (R) RETURNED_SQLSTATE
RETURNS REVERSE REVOKE (R)
RIGHT (R) RLIKE (R) ROLLBACK
ROLLUP ROTATE[AB] ROUTINE
ROW ROWS ROW_COUNT
ROW_FORMAT RTREE SAVEPOINT
SCHEDULE SCHEMA (R) SCHEMAS (R)
SCHEMA_NAME SECOND SECOND_MICROSECOND (R)
SECURITY SELECT (R) SENSITIVE (R)
SEPARATOR (R) SERIAL SERIALIZABLE
SERVER SESSION SET (R)
SHARE SHOW (R) SHUTDOWN
SIGNAL (R) SIGNED SIMPLE
SLAVE SLOW SMALLINT (R)
SNAPSHOT SOCKET SOME
SONAME SOUNDS SOURCE
SPATIAL (R) SPECIFIC (R) SQL (R)
SQLEXCEPTION (R) SQLSTATE (R) SQLWARNING (R)
SQL_AFTER_GTIDS SQL_AFTER_MTS_GAPS SQL_BEFORE_GTIDS
SQL_BIG_RESULT (R) SQL_BUFFER_RESULT SQL_CACHE
SQL_CALC_FOUND_ROWS (R) SQL_NO_CACHE SQL_SMALL_RESULT (R)
SQL_THREAD SQL_TSI_DAY SQL_TSI_HOUR
SQL_TSI_MINUTE SQL_TSI_MONTH SQL_TSI_QUARTER
SQL_TSI_SECOND SQL_TSI_WEEK SQL_TSI_YEAR
SSL (R) STACKED START
STARTING (R) STARTS STATS_AUTO_RECALC
STATS_PERSISTENT STATS_SAMPLE_PAGES STATUS
STOP STORAGE STORED[ac](R)
STRAIGHT_JOIN (R) STRING SUBCLASS_ORIGIN
SUBJECT SUBPARTITION SUBPARTITIONS
SUPER SUSPEND SWAPS
SWITCHES TABLE (R) TABLES
TABLESPACE TABLE_CHECKSUM TABLE_NAME
TEMPORARY TEMPTABLE TERMINATED (R)
TEXT THAN THEN (R)
TIME TIMESTAMP TIMESTAMPADD
TIMESTAMPDIFF TINYBLOB (R) TINYINT (R)
TINYTEXT (R) TO (R) TRAILING (R)
TRANSACTION TRIGGER (R) TRIGGERS
TRUE (R) TRUNCATE TYPE
TYPES UNCOMMITTED UNDEFINED
UNDO (R) UNDOFILE UNDO_BUFFER_SIZE
UNICODE UNINSTALL UNION (R)
UNIQUE (R) UNKNOWN UNLOCK (R)
UNSIGNED (R) UNTIL UPDATE (R)
UPGRADE USAGE (R) USE (R)
USER USER_RESOURCES USE_FRM
USING (R) UTC_DATE (R) UTC_TIME (R)
UTC_TIMESTAMP (R) VALIDATION VALUE
VALUES (R) VARBINARY (R) VARCHAR (R)
VARCHARACTER (R) VARIABLES VARYING (R)
VIEW VIRTUAL[ae](R) WAIT
WARNINGS WEEK WEIGHT_STRING
WHEN (R) WHERE (R) WHILE (R)
WITH (R) WITHOUT[AF] WORK
WRAPPER WRITE (R) X509
XA XID[AG] XML
XOR (R) YEAR YEAR_MONTH (R)
ZEROFILL (R)
相關文章
相關標籤/搜索