使用sql进行CRUD操作(MySQL Version)

示例数据库:sakila database详细介绍

所谓CRUD操作是指对数据库的数据进行Create,Read,Update,Delete操作,下面我们一一介绍:

  1. Create

新建数据,即插入数据。

1
2
INSERT [into] table [(column1,column2,column3,...)] VALUES
(value1, value2, value3,...);
  1. Read

读取数据,即查询数据。

1
2
3
4
5
6
7
8
9
10
SELECT [options] items
[into file_details]
FROM tables
[WHERE conditions]
[GROUP BY group_type]
[HAVING where_definition]
[ORDER BY order_type]
[LIMIT limit_criteria]
[PROCEDURE proc_name(arguments)]
[lock_options];

除此之外,还有多表查询和子查询。

  1. Update

更新数据。

1
2
3
4
5
UPDATE [LOW_PRIORITY] [IGNORE] tablename
SET column1=expression1,column2=expression2,...
[WHERE condition]
[ORDER BY order_criteria]
[LIMIT number]
  1. Delete
    删除数据。
1
2
3
4
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM table
[WHERE condition]
[ORDER BY order_cols]
[LIMIT number]