MySql基本操作語句整理

資料庫 DATABASE:

創建
CREATTE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name ;
刪除:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name;
顯示:
SHOW{ DATABASES| SCHEMAS} [LIKE 'pattern'|WHERE expr];
選擇:
USE database_name ;

數據表 TABLE :

創建:
CREATE TABLE [IF NOT EXISTS] table_name( column_name data_type, ...) //()內為庫裡面每一列的名稱
刪除:
DROP TABLE [IF EXISTS] table_name;
顯示:
SHOW TABLES [ FROM db_name] [ LIKE 'pattern'||EHERE expr];

列 COLUMN:

添加:
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST|AFTER col_name]
//FIRST 添加到最前端, AFTER col_name ,跟在col_name 後邊,省略該項,則加在最後面;
刪除:
ALTER TABLE tbl_name DROP [COLUMN] col_name;

記錄 ROW:

添加:
INSERT [INTO]tbl_name [(col_name,...)] VALUES(val,..)
刪除:
DELETE FROM tbl_name [WHERE where_condition]
修改/更新:
UPDATA[LOW_PRIORITY][IGNORE ] table_reference SET col_name1={expr1|DEFAULT} [,col_name2={expr2|DEFAULT}]...
[WHERE where_condition]
例子: UPDATE users SET age=18 WHERE age=17;
顯示:
SELECT expr, ...FROM tal_name;
SELECT * FROM tal_name; //查看所有
查詢:
SELECT select_expr[, select_expr...]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name|position}[ASC|DESC],...]
[HAVING where_condition]
[ORDER BY {col_name|expr|position} [ASC|DESC], ...]
[LIMIT {[offset,]row_count|row_count OFFSET offset}]
]
//例如: SELECT name FROM users WHERE id=3;

其他:

約束條件:NOT NULL ; PRIMARY KEY; UNIQUE KEY; DEFAULT; FOREIGN KEY
其他常用關鍵字:AUTO_INCREMENT

You May Also Like