登錄數據庫:mysql
mysql -u用戶名 -p密碼 -P端口 -h數據庫地址
sql
修改mysql提示符:(僅本次鏈接有效)數據庫
方法一:執行mysql -uroot -proot -prompt \h 結果爲:localhost 方法二:進入mysql後,執行PROMPT \u@\h \d> 結果爲:root@localhost(數據庫名)>
參數說明:服務器
\D | 完整日期 |
\d | 當前數據庫 |
\h | 服務器名稱(域名/IP) |
\u | 系統當前用戶 |
庫級操做:編碼
SELECT USER();//顯示當前用戶 SELECT DATABASE();//顯示打開的數據庫
//建立數據庫 //CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name; CREATE DATABASE hehe; //查看已有的數據庫信息(好比編碼等) SHOW CREATE DATABASE hehe; //顯示數據庫 //SHOW {DATABASES | SCHEMA} [LIKE 'Ppattern' | WHERE expr] SHOW DATABASES; //修改數據庫(好比編碼等) //ALTER {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name; //刪除數據庫 //DROP {DATABASE | SCHEMA} [IF NOT EXISTS] db_name DROP DATABASE hehe;
表級操做:spa
//建立表 //列名 數據類型 [數值型是否有符號] [是否可NULL] [默認值] [惟一約束] [自動遞增,1開始] [主鍵,一張表只能有一個] //列名 數據類型 [UNSGINED] [NULL | NOT NULL] [DEFAULT value] [UNIQUE KEY] [AUTO_INCREMENT] [PREMARY KEY] CREATE TABLE table_name( username varchar(20), number int(11) unsgined, sex ENUM(1,2,3) DEFAULT 3, ); //查看錶 SELECT TABLES [FROM 數據庫名]; //查看錶結構 SHOW COLUMNS FROM table_name; //修改表名 方法一:ALTER TABLE table_name RENAME new_name 方法二:RENAME TABLE table_name TO new_name [, table_name2 TO new_name2,...] //添加列 ALTER TABLE table_name ADD password varchar(20) NOT NULL AFTER {username | FIRST | LAST}; //刪除列,並增長 ALTER TABLE table_name DROP password, DROP username, ADD id; //修改列定義 ALTER TABLE table_name MODIFY username varchar(50) NOT NULL; //修改列名和定義 ALTER TBALE table_name CHANGE old_col_name new_col_name [定義] [FIRST | AFTER col_name] //列操做總結:ALTER TABLE table_name [操做],[操做]...
增、刪、改、查:code
=========================增========================= //INSERT [INTO] table_name [(列名1,列名2,...)] VALUE(第一條),(第二條),...; INSERT table_name (username, password, age) VALUE ("zhangsan", "123456", 12),("lisi", "123456", 18); //INSERT [INTO] table_name SET username="wangwu",password="123456";只能插入單條 //INSERT [INTO] table_name [(列名1,列名2,...)] SELECT... =========================刪========================= //DELETE FROM table_name [WHERE ...] =========================改========================= //UPDATE table_name SET username="zhangsan",password="654321",...[WHERE ...]; =========================查========================= SELECT 列名 FROM 表名, where條件, group by分組條件, HAVING二級條件, order by排序, LIMIT條件
SELECT col_name[,col_name1,col_name2,...] [AS new_col_name]排序
[域名
FROM table_nameit
[WHERE where_condition]
[GROUP BY {col_name | position} [ASE | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
]
SET NAMES gbk;//select結果用gbk編碼顯示,不會改變數據庫編碼方式 SELECT * FROM hehe\G;//以表格的形式顯示結果
子查詢:
SELECT * FROM hehe WHERE id = (SELECT id FROM xixi WHERE name = "zhangsan");
假如子查詢中的(...)返回結果查過一行,則須要用關鍵字ANY、SOME、ALL三者之一來修飾。
SELECT * FROM hehe WHERE price = ANY (SELECT price FROM xixi WHERE name = "goods");
ANY | SOME | ALL | |
>、>= | >min | >min | >max |
<、<= | <max | <max | <min |
= | 任意值 | 任意值 | |
!= | 任意值 |
其中=和!=能夠用IN和NOT IN代替:
SELECT * FROM hehe WHERE price IN (SELECT price FROM xixi WHERE name = "goods");
使用子查詢結果 //INSERT table_name (col_name1,col_name2,...) SELECT ...
鏈接:
語法:... [LEFT | RIGHT] JOIN 表名 ON 鏈接條件 ...
內鏈接【JOIN】
外鏈接:左外鏈接【LEFT JOIN】、右外鏈接【RIGHT JOIN】、全鏈接【無】
自身鏈接【內鏈接自身table便可】
鏈接的條件能夠使用ON或者WHERE關鍵字,可是通常使用ON來定義鏈接條件,WHERE來定義篩選條件。
//雙錶鏈接,顯式 SELECT col_name [AS resultId] FROM table_name1 AS t1 JOIN table_name2 AS t2 ON t1.id = t2.id WHERE... UPDATE table_name1 AS a JOIN table_name2 AS b ON a.id = b.number SET a.name = b.name; DELETE t1 FROM table_name1 AS t1 LEFT JOIN (SELECT語句...也可嵌套鏈接和子查詢和WHERE篩選) //雙錶鏈接,隱式,內鏈接能夠省略JOIN,左外鏈接和右外鏈接不可省略。 SELECT col_name [AS resultId] FROM table_name1 AS t1, table_name2 AS t2 ON t1.id = t2.id WHERE... //雙表外鏈接,把內鏈接的JOIN改爲LEFT JOIN或者RIGHT JOIN便可。 //多表鏈接 SELECT col_name FROM table_name1 AS t1 JOIN table_name2 AS t2 ON t1.id = t2.number JOIN table_name3 AS t2 ON t1.pid = t3.number [...]
mysql有4種數據類型(整形、浮點型、字符型、日期時間型)。