1)查询语句
SELECT
column_1, column_2, ...
FROM
table_1
[INNER | LEFT |RIGHT] JOIN table_2 ON conditions
WHERE
conditions
GROUP BY column_1
HAVING group_conditions
ORDER BY column_1
LIMIT offset, length;
--- 示例
mysql> SELECT lastname, firstname, jobtitle FROM employees;
+-----------+-----------+----------------------+
| lastname | firstname | jobtitle |
+-----------+-----------+----------------------+
| Murphy | Diane | President |
| Patterson | Mary | VP Sales |
| Firrelli | Jeff | VP Marketing |
| Patterson | William | Sales Manager (APAC) |
| Bondur | Gerard | Sale Manager (EMEA) |
| Bow | Anthony | Sales Manager (NA) |
| Jennings | Leslie | Sales Rep |
| Thompson | Leslie | Sales Rep |
| Firrelli | Julie | Sales Rep |
| Patterson | Steve | Sales Rep |
| Tseng | Foon Yue | Sales Rep |
| Vanauf | George | Sales Rep |
| Bondur | Loui | Sales Rep |
| Hernandez | Gerard | Sales Rep |
| Castillo | Pamela | Sales Rep |
| Bott | Larry | Sales Rep |
| Jones | Barry | Sales Rep |
| Fixter | Andy | Sales Rep |
| Marsh | Peter | Sales Rep |
| King | Tom | Sales Rep |
| Nishi | Mami | Sales Rep |
| Kato | Yoshimi | Sales Rep |
| Gerard | Martin | Sales Rep |
+-----------+-----------+----------------------+
23 rows in set
2)插入语句
INSERT INTO table(column1,column2...)
VALUES (value1,value2,...);
首先,在INSERT INTO子句之后,在括号内指定表名和逗号分隔列的列表。
然后,将括号内的相应列的逗号分隔值放在VALUES关键字之后。
USE testdb; --- 选择数据库 ---
--- 创建表 --
CREATE TABLE IF NOT EXISTS tasks (
task_id INT(11) AUTO_INCREMENT,
subject VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- 插入数据 ---
INSERT INTO tasks(subject,start_date,end_date,description)
VALUES('Learn MySQL INSERT','2017-07-21','2017-07-22','Start learning..');
3 )修改update语句
UPDATE [LOW_PRIORITY] [IGNORE] table_name
SET
column_name1 = expr1,
column_name2 = expr2,
...
WHERE
condition;
--- 修改Murphy的jobtitle ----
UPDATE employees
SET
jobtitle = 'Presidents'
WHERE
lastname = 'Murphy';
4 )删除表数据
DELETE FROM table_name
WHERE condition;
首先,指定删除数据的表(table_name)。
其次,使用条件来指定要在WHERE子句中删除的行记录。如果行匹配条件,这些行记录将被删除。
--- 删除firstname为Leslie的员工
DELETE FROM employees
WHERE
firstname = 'Leslie';
--- 删除employees表中的所有行
DELETE FROM employees;
在执行上面查询语句后,employees表中的所有行都被删除。
5 ) 创建与删除数据库
--- 创建
CREATE DATABASE [IF NOT EXISTS] database_name;
--- 查询数据库 ----
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testdb |
| yiibaidb |
+--------------------+
5 rows in set
在使用指定数据库之前,必须通过使用USE语句告诉MySQL要使用哪个数据库。
--- 使用数据库 ---
USE database_name;
--- 删除数据库 ---
DROP DATABASE [IF EXISTS] database_name;
--- 创建表---
CREATE TABLE [IF NOT EXISTS] table_name(
column_list
) engine=table_type;
首先,指定要在CREATE TABLE子句之后创建的表的名称。表名在数据库中必须是唯一的。 IF NOT EXISTS是语句的可选部分,允许您检查正在创建的表是否已存在于数据库中。 如果是这种情况,MySQL将忽略整个语句,不会创建任何新的表。 强烈建议在每个CREATE TABLE语句中使用IF NOT EXISTS来防止创建已存在的新表而产生错误。
其次,在column_list部分指定表的列表。字段的列用逗号(,)分隔。
第三,需要为engine子句中的表指定存储引擎。可以使用任何存储引擎,如:InnoDB,MyISAM,HEAP,EXAMPLE,CSV,ARCHIVE,MERGE, FEDERATED或NDBCLUSTER。如果不明确声明存储引擎,MySQL将默认使用InnoDB。
--- 设置主键 ---
PRIMARY KEY (col1,col2,...);
---- 示例:创建表tasks----
CREATE TABLE IF NOT EXISTS tasks (
task_id INT(11) NOT NULL AUTO_INCREMENT,
subject VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
) ENGINE=InnoDB;
DROP TABLE tbl_name; --- 删除表,或---
DROP TABLE IF EXISTS tbl_name;
如果要清空表中所有的记录,可以用下面两种方法:
delete from table1;
truncate table table1;
如果要删除表中部分记录,只能用delete语句
delete from table1 where ....
2017年11月21日