数据库的数据修改
数据库的数据修改包括插入、修改和删除等操作。这些操作是管理和维护数据库中数据的基本手段。
5.1 插入 (INSERT)
插入操作用于在表中添加新记录。可以插入单行或多行数据。
基本语法:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
示例:
向orders
表中插入一条新记录:INSERT INTO orders (item_id, amount, unit_price, price, description, ts) VALUES (1, 10, 15.50, 155.00, 'First order', NOW());
插入多行:
INSERT INTO orders (item_id, amount, unit_price, price, description, ts) VALUES (1, 10, 15.50, 155.00, 'First order', NOW()), (2, 5, 25.00, 125.00, 'Second order', NOW());
5.2 修改 (UPDATE)
修改操作用于更新表中现有记录的值。可以根据特定条件更新一条或多条记录。
基本语法:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
示例:
更新orders
表中item_id
为 1 的记录的amount
和price
:UPDATE orders SET amount = 20, price = 310.00 WHERE item_id = 1;
更新多条记录:
UPDATE orders SET amount = amount + 5 WHERE unit_price > 20.00;
5.3 删除 (DELETE)
删除操作用于从表中删除一条或多条记录。需要小心使用 DELETE
操作,以免误删数据。
基本语法:
DELETE FROM table_name WHERE condition;
示例:
从orders
表中删除item_id
为 2 的记录:DELETE FROM orders WHERE item_id = 2;
删除所有记录(小心使用):
DELETE FROM orders;
带条件的删除:
DELETE FROM orders WHERE ts < '2024-01-01';
举例1
假设我们有一个名为 employees
的表,结构如下:
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);
插入数据:
INSERT INTO employees (name, position, salary, hire_date) VALUES ('John Doe', 'Manager', 75000.00, '2024-01-15');
修改数据:
UPDATE employees SET salary = 80000.00 WHERE name = 'John Doe';
删除数据:
DELETE FROM employees WHERE id = 1;
create table book(
id int primary key auto_increment,
title varchar(200) not null,
description varchar(1000) default '',
price decimal(12, 4),
isbn char(16) not null,
publish_at date not null
);
create unique index idx_book_isbn on book(isbn);
表结构
id int primary key auto_increment:
id
列是整数类型,作为主键,且是自增的。每次插入新记录时,数据库会自动为id
列分配一个唯一的、递增的整数值。
title varchar(200) not null:
title
列是可变长度的字符串类型,最大长度为 200 字符,不能为空。
description varchar(1000) default:
description
列是可变长度的字符串类型,最大长度为 1000 字符,默认值为空字符串。
price decimal(12,4:
price
列是具有 12 位数字和 4 位小数的十进制数。
isbn char(16) not null:
isbn
列是固定长度的字符串类型,长度为 16 字符,不能为空。ISBN 是书籍的国际标准书号。
publish_at date not null:
publish_at
列是日期类型,不能为空,用于存储书籍的出版日期。
create unique index idx_book_isbn on book(isbn:
- 在
isbn
列上创建唯一索引,确保每本书的 ISBN 在数据库中是唯一的。
- 在
insert into book(title, price, isbn, publish_at)
select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1';
insert into book(title, price, isbn, publish_at)
select 'a other book title', 35.4, 'xx-xxxx-xxxx', '2019-12-1';
插入第一条记录:
insert into book(title, price, isbn, publish_at)
select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1';
title
:'a book title',符合varchar(200) not null
的定义。price
:25.4,符合decimal(12, 4)
的定义。isbn
:'xx-xxxx-xxxx',符合char(16) not null
的定义。publish_at
:'2019-12-1',符合date not null
的定义。
举例2
create table employee
(
id serial primary key,
name varchar(64),
dept varchar(64),
salary decimal(12, 4)
);
表结构解释
id serial primary key:
id
列是自动递增的整数类型 (serial
),作为主键,保证每条记录都有一个唯一标识。
name varchar(64:
name
列是可变长度的字符串类型,最大长度为 64 字符。
dept varchar(64:
dept
列是可变长度的字符串类型,最大长度为 64 字符。
salary decimal(12, 4:
salary
列是具有 12 位数字和 4 位小数的十进制数。
update employee
set salary = salary + 1000
where dept = 'sale' and name = 'Dora Muk';
update employee:
- 更新
employee
表中的记录。
- 更新
set salary = salary + 1000:
- 将
salary
列的当前值增加 1000。
- 将
where dept = 'sale' and name = 'Dora Muk:
- 只更新那些
dept
列的值为sale
且name
列的值为Dora Muk
的记录。通过这个条件,确保只修改特定员工的工资。
- 只更新那些
举例3
create table orders (
id int primary key auto_increment,
item_id int,
amount int,
unit_price decimal(12, 4),
price decimal(12, 4),
description varchar(2000),
ts timestamp default now(),
deal bool default false
);
delete from orders
where deal;
表结构
id int primary key auto_increment:
id
列是整数类型,作为主键,且是自增的。每次插入新记录时,数据库会自动为id
列分配一个唯一的、递增的整数值。
item_id int:
item_id
列是整数类型,唯一标识符。
amount int:
amount
列是整数类型。
unit_price decimal(12, 4:
unit_price
列是具有 12 位数字和 4 位小数的十进制数。
price decimal(12, 4:
price
列是具有 12 位数字和 4 位小数的十进制数。
description varchar(2000:
description
列是可变长度的字符串类型,最大长度为 2000 字符。
ts timestamp default now:
ts
列是时间戳类型,默认值为当前时间。每次插入新记录时,ts
列会自动记录插入时间。
deal bool default false:
deal
列是布尔类型,默认值为false
。
delete from orders
where deal;
delete from orders:
- 从
orders
表中删除记录。
- 从
where deal:
- 只删除那些
deal
列的值为true
的记录。where deal
是where deal = true
的简写。
- 只删除那些
本文由mdnice多平台发布
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。