Auto commit

This commit is contained in:
smallkun 2025-03-14 09:56:37 +08:00
parent 1ba7f30de6
commit baf429e8a2
2 changed files with 138 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,138 @@
### 练习一
**1**
```sql
#(1)创建数据库
CREATE DATABASE test04_Market DEFAULT CHARACTER SET utf8;
USE test04_Market;
#(2)创建数据表
CREATE TABLE customers(
c_num INT(11) PRIMARY KEY AUTO_INCREMENT,
c_name VARCHAR(50),
c_contact VARCHAR(50),
c_city VARCHAR(50),
c_birth DATETIME NOT NULL
);
#(3)更改字段的位置
#AFTER/BEFORE 字段名 设置位置在指定字段前/后
ALTER TABLE 表名 MODIFY 字段名 数据类型 AFTER/BEFORE 字段名;
ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth;
#(4)更改字段的数据类型
ALTER TABLE customers MODIFY c_name VARCHAR(70);
#(5)更改字段名
ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50);
#(6)添加字段
ALTER TABLE customers ADD c_gender CHAR(1);
#(7)更改表名
ALTER TABLE customers RENAME TO customers_info;
#(8)删除字段
ALTER TABLE customers_info DROP c_city;
```
**2**
```sql
#(1)创建表时 添加外键约束并指定外键约束名称
CREATE TABLE orders(
o_num INT(11) PRIMARY KEY AUTO_INCREMENT,
o_date DATE,
c_id INT(11),
CONSTRAINT fk_cid FOREIGN KEY(c_id) REFERENCES customers_info(c_num)
);
#(2)
#删除指定外键
ALTER TABLE orders DROP FOREIGN KEY orders_ibfk_1;
#删除指定表
DROP TABLE orders;
```
---
### 练习二
```sql
#(1)创建表、插入记录
CREATE TABLE pet(
`name` VARCHAR(20) NOT NULL COMMENT '宠物名称',
`owner` VARCHAR(20) COMMENT '宠物主人',
species VARCHAR(20) NOT NULL COMMENT '种类',
sex CHAR(1) NOT NULL COMMENT '性别',
birth YEAR NOT NULL COMMENT '出生日期',
death YEAR COMMENT '死亡日期'
);
INSERT INTO pet()
VALUES
('Fluffy', 'Harold', 'cat', 'f', 2003, 2010),
('Claws', 'Gwen', 'cat', 'm', 2004,NULL ),
('Buffy', NULL, 'dog', 'f', 2009, NULL),
('Fang', 'Benny', 'dog', 'm', 2000, NULL),
('Bowser', 'Diane', 'dog', 'm', 2003, 2009),
('Chirpy', NULL, 'bird', 'f', 2008, NULL);
#(2)更新记录
UPDATE pet
SET `owner` = 'Kevin'
WHERE `name` = 'Fang';
#(3)
UPDATE pet
SET `owner` = 'Kevin'
WHERE `owner` IS NULL;
#(4)
DELETE FROM pet
WHERE death IS NOT NULL;
#(5)
TRUNCATE pet;
```
### 练习三
```sql
#1.创建数据库
CREATE DATABASE test_compay DEFAULT CHARACTER SET utf8;
#2.创建表
CREATE TABLE department(
depid INT PRIMARY KEY AUTO_INCREMENT,
depname VARCHAR(20) NOT NULL,
deinfo VARCHAR(50)
);
CREATE TABLE employee(
empid INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
sex CHAR(1) NOT NULL DEFAULT '男',
title VARCHAR(20) NOT NULL,
birthday DATE NOT NULL,
depid INT,
CONSTRAINT fk_eid_did FOREIGN KEY(depid) REFERENCES department(depid)
);
CREATE TABLE salary(
empid INT,
basesalary DECIMAL(10, 2),
titleSalary DECIMAL(10, 2),
decuction DECIMAL(10, 2)
);
#3添加外键并设置级联更新、级联删除
ALTER TABLE salary ADD CONSTRAINT fk_sal_eid FOREIGN KEY(empid)
REFERENCES employee(empid) ON UPDATE CASCADE ON DELETE CASCADE;
#4插入记录
INSERT INTO department()
VALUES
(111, '生产部', NULL),
(222, '销售部', NULL),
(333, '人事部', NULL);
INSERT INTO employee()
VALUES
(1001, '张三', '男', '高级工程师', '1975-1-1', 111),
(1002, '李四', '女', '助工', '1985-1-1', 111),
(1003, '王五', '男', '工程师', '1978-11-11', 222),
(1004, '赵六', '男', '工程师', '1999-1-1', 222);
INSERT INTO salary()
VALUES
(1001, 2200, 1100, 200),
(1002, 1200, 200, NULL),
(1003, 2900, 700, 200),
(1004, 1950, 700, 150);
```