SQL数据操作与管理全解析
立即解锁
发布时间: 2025-08-31 01:03:03 阅读量: 8 订阅数: 11 AIGC 

### SQL 数据操作与管理全解析
#### 1. 新增列的需求与实现
当出现新需求,需要存储书籍的出版年份时,可使用`ALTER TABLE`语句。以下是相关语法:
```sql
-- Add a new column
ALTER TABLE table_name
ADD column_name datatype [column_constraint];
-- Modify a datatype of an existing column
ALTER TABLE table_name
ALTER COLUMN column_name [new_datatype];
-- Rename a column
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;
-- Add a new constraint to a column
ALTER TABLE table_name
ADD CONSTRAINT constraint_name constraint_type (column_name);
-- Modify an existing constraint
ALTER TABLE table_name
ALTER CONSTRAINT constraint_name [new_constraint];
-- Remove an existing column
ALTER TABLE table_name
DROP COLUMN column_name;
```
要为`books`表添加`publication_year`列,可执行以下代码:
```sql
-- Add publication_year to the books table
ALTER TABLE books
ADD publication_year INT;
```
#### 2. DML 数据操作语言概述
DML(Data Manipulation Language)是数据库管理的重要组成部分,它允许在数据库系统中进行数据的选择、插入、删除和更新操作。其主要目的是检索和操作关系型数据库中的数据,包含多个关键命令。
#### 3. 使用 INSERT 插入数据
`INSERT`命令用于向表中添加新数据。用户可以使用该命令无缝地将一条或多条记录插入到数据库中的特定表中。以下是`INSERT`语句的标准语法:
```sql
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
```
- `INSERT INTO`指定数据将插入的表。
- `(column1, column2, ...)`可选,用于指定数据将插入的列。若省略该部分,则假设为表中的所有列提供值。
- `VALUES`关键字表示要插入到指定列的值列表开始。
为了向之前创建的表中插入数据,可执行以下代码:
```sql
-- Inserting data into the authors table
INSERT INTO authors (author_id, author_name) VALUES
(1, 'Stephanie Mitchell'),
(2, 'Paul Turner'),
(3, 'Julia Martinez'),
(4, 'Rui Machado'),
(5, 'Thomas Brown');
-- Inserting data into the books table
INSERT INTO books (book_id, book_title,
author_id, publication_year,
rating)
VALUES
(1, 'Python Crash Course', 1, 2012, 4.5),
(2, 'Learning React', 2, 2014, 3.7),
(3, 'Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow',
3, 2017, 4.9),
(4, 'JavaScript: The Good Parts', 4, 2015, 2.8),
(5, 'Data Science for Business', 5, 2019, 4.2);
-- Inserting data into the category table
INSERT INTO category (category_id, category_name) VALUES
(1, 'Programming'),
(2, 'Machine Learning'),
(3, 'Data Science'),
(4, 'Software Engineering'),
(5, 'Algorithms'),
(6, 'Computer Science');
-- Inserting data into the book_category table
INSERT INTO book_category (book_id, category_id) VALUES
(1, 1),
(2, 1),
(3, 2),
(4, 1),
(5, 3);
```
各表的作用如下:
- `authors`表:存储作者信息,每行代表一个作者,包含`author_id`和`author_name`列。
- `books`表:存储书籍信息,每行代表一本书,包含`book_id`、`book_title`、`author_id`、`publication_year`和`rating`列,其中`author_id`与`authors`表中的`author_id`关联。
- `category`表:用于对书籍进行分类,每行代表一个类别,包含`category_id`和`category_name`列。
- `book_category`表:存储书籍与类别的关系,每行代表一种关系,包含`book_id`和`category_id`列,建立了书籍与类别之间的多对多关系。
为了查看插入的数据,可逐行执行以下代码:
```sql
select * from authors;
select * from book_category;
select * from books;
select * from category;
```
#### 4. 使用 SELECT 选择数据
`SELECT`是 SQL 中最基本的 DML 命令之一,用于从数据库中提取特定数据。执行该语句时,它会检索所需信息并将其组织成一个结构化的结果表,即结果集。以下是`SELECT`语句的简单语法:
```sql
SELECT column1, column2, ...
FROM table_name;
```
- `SELECT`部分指定从表中检索的特定列或表达式。
- `FROM`指定数据将从哪个表中检索。
#### 5. 使用 WHERE 过滤数据
`WHERE`子句是可选的,允许用户定义检索数据必须满足的条件,从而根据指定标准过滤行。以下是包含`WHERE`子句的
0
0
复制全文
相关推荐










