用一个merge语句表达将student表中的数据同步到newstudent表中;在student中插入一行数据,再同步到newstudent;更改新插入同学的出生日期后再同步到newstudent;删除新学生记录后同步到newstudent四个步骤的全部同步数据操作
时间: 2024-01-22 07:19:29 浏览: 101
1. 将student表中的数据同步到newstudent表中:
```sql
MERGE INTO newstudent ns
USING student s ON (ns.id = s.id)
WHEN MATCHED THEN
UPDATE SET
ns.name = s.name,
ns.age = s.age,
ns.gender = s.gender,
ns.birthday = s.birthday
WHEN NOT MATCHED THEN
INSERT (ns.id, ns.name, ns.age, ns.gender, ns.birthday)
VALUES (s.id, s.name, s.age, s.gender, s.birthday);
```
2. 在student中插入一行数据,再同步到newstudent:
```sql
INSERT INTO student (id, name, age, gender, birthday)
VALUES (6, 'Tom', 20, 'Male', '2000-01-01');
MERGE INTO newstudent ns
USING (SELECT id, name, age, gender, birthday FROM student WHERE id = 6) s
ON (ns.id = s.id)
WHEN MATCHED THEN
UPDATE SET
ns.name = s.name,
ns.age = s.age,
ns.gender = s.gender,
ns.birthday = s.birthday
WHEN NOT MATCHED THEN
INSERT (ns.id, ns.name, ns.age, ns.gender, ns.birthday)
VALUES (s.id, s.name, s.age, s.gender, s.birthday);
```
3. 更改新插入同学的出生日期后再同步到newstudent:
```sql
UPDATE student SET birthday = '2000-02-01' WHERE id = 6;
MERGE INTO newstudent ns
USING (SELECT id, name, age, gender, birthday FROM student WHERE id = 6) s
ON (ns.id = s.id)
WHEN MATCHED THEN
UPDATE SET
ns.name = s.name,
ns.age = s.age,
ns.gender = s.gender,
ns.birthday = s.birthday
WHEN NOT MATCHED THEN
INSERT (ns.id, ns.name, ns.age, ns.gender, ns.birthday)
VALUES (s.id, s.name, s.age, s.gender, s.birthday);
```
4. 删除新学生记录后同步到newstudent:
```sql
DELETE FROM student WHERE id = 6;
DELETE FROM newstudent WHERE id = 6;
```
阅读全文