SQL 语句大全实例教程
SQL(结构化查询语言)是一种强大的语言,用于与关系型数据库进行交互。掌握 SQL 语句至关重要,因为它使您可以高效地管理和操作数据。本文提供了一份 SQL 语句大全,พร้อมตัวอย่างเพื่อ帮助您入门。
SQL 语句大全实例教程
创建和删除数据库
Create database: CREATE DATABASE my_database; Drop database: DROP DATABASE my_database;
创建和删除表
Create table: CREATE TABLE my_table (id INT PRIMARY KEY, name VARCHAR(255)); Drop table: DROP TABLE my_table;
插入数据
Insert single row: INSERT INTO my_table (id, name) VALUES (1, 'John'); Insert multiple rows: INSERT INTO my_table (id, name) VALUES (2, 'Jane'), (3, 'Bob');
选择数据
Select all columns: SELECT FROM my_table; Select specific columns: SELECT id, name FROM my_table; Filter data: SELECT FROM my_table WHERE id > 2;
更新数据
Update single row: UPDATE my_table SET name = 'John Doe' WHERE id = 1; Update multiple rows: UPDATE my_table SET name = name || ' Doe' WHERE id > 2;
删除数据
Delete single row: DELETE FROM my_table WHERE id = 1; Delete multiple rows: DELETE FROM my_table WHERE id > 2;
其他常见语句
Alter table: ALTER TABLE my_table ADD COLUMN age INT; Join tables: SELECT FROM my_table t1 JOIN my_table t2 ON t1.id = t2.id; Group and aggregate: SELECT name, COUNT() AS count FROM my_table GROUP BY name;
实例教程
1. 创建数据库和表:
```sql CREATE DATABASE my_database; CREATE TABLE my_table (id INT PRIMARY KEY, name VARCHAR(255)); ```
2. 插入数据:
```sql INSERT INTO my_table (id, name) VALUES (1, 'John'), (2, 'Jane'); ```
3. 选择数据:
```sql SELECT FROM my_table; ```
4. 过滤数据:
```sql SELECT FROM my_table WHERE id > 1; ```
5. 更新数据:
```sql UPDATE my_table SET name = 'John Doe' WHERE id = 1; ```
6. 删除数据:
```sql DELETE FROM my_table WHERE id = 2; ```
7. 连接表:
```sql SELECT FROM my_table t1 JOIN my_table t2 ON t1.id = t2.id; ```
8. 分组和汇总:
```sql SELECT name, COUNT() AS count FROM my_table GROUP BY name; ```
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系 836084111@qq.com 删除。