Create table PEOPLE
CREATE TABLE people (
person_id INTEGER NOT NULL PRIMARY KEY,
given_name VARCHAR2(100) NOT NULL,
family_name VARCHAR2(100) NOT NULL,
title VARCHAR2(20),
birth_date DATE
)
Table created.
Create table PATIENTS
CREATE TABLE patients (
patient_id INTEGER NOT NULL PRIMARY KEY REFERENCES people (person_id),
last_admission_date DATE
)
Table created.
Create table STAFF
CREATE TABLE staff (
staff_id INTEGER NOT NULL PRIMARY KEY REFERENCES people (person_id),
hired_date DATE
)
Table created.
Insert a row into the PEOPLE table
INSERT INTO people
VALUES (1, 'Dave', 'Badger', 'Mr', date'1960-05-01')
1 row(s) inserted.
Insert a row into the PEOPLE table
INSERT INTO people
VALUES (2, 'Simon', 'Fox', 'Mr')
ORA-00947: not enough valuesMore Details: https://docs.oracle.com/error-help/db/ora-00947
Insert a row into the PEOPLE table
INSERT INTO people (person_id, given_name, family_name, title)
VALUES (2, 'Simon', 'Fox', 'Mr')
1 row(s) inserted.
Insert a row into the PEOPLE table
INSERT INTO people (person_id, given_name, family_name, title)
VALUES (3, 'Dave', 'Frog', (SELECT 'Mr' FROM dual))
1 row(s) inserted.
Insert multiple rows into the PEOPLE table
INSERT INTO people (person_id, given_name, family_name, title)
WITH names AS (
SELECT 4, 'Ruth', 'Fox', 'Mrs' FROM dual UNION ALL
SELECT 5, 'Isabelle', 'Squirrel', 'Miss' FROM dual UNION ALL
SELECT 6, 'Justin', 'Frog', 'Master' FROM dual UNION ALL
SELECT 7, 'Lisa', 'Owl', 'Dr' FROM dual
)
SELECT * FROM names
4 row(s) inserted.
Rollback previous DML operations
ROLLBACK
Statement processed.
SELECT * FROM people
no data found
Insert multiple rows into the PEOPLE table
INSERT INTO people (person_id, given_name, family_name, title)
WITH names AS (
SELECT 4, 'Ruth', 'Fox' family_name, 'Mrs' FROM dual UNION ALL
SELECT 5, 'Isabelle', 'Squirrel' family_name, 'Miss' FROM dual UNION ALL
SELECT 6, 'Justin', 'Frog' family_name, 'Master' FROM dual UNION ALL
SELECT 7, 'Lisa', 'Owl' family_name, 'Dr' FROM dual
)
SELECT * FROM names
WHERE family_name LIKE 'F%'
2 row(s) inserted.
Rollback insert operation on PEOPLE table
ROLLBACK
Statement processed.
Insert multiple rows into the PEOPLE, PATIENTS, and STAFF tables
INSERT ALL
/* Every one is a person */
INTO people (person_id, given_name, family_name, title)
VALUES (id, given_name, family_name, title)
INTO patients (patient_id, last_admission_date)
VALUES (id, admission_date)
INTO staff (staff_id, hired_date)
VALUES (id, hired_date)
WITH names AS (
SELECT 4 id, 'Ruth' given_name, 'Fox' family_name, 'Mrs' title,
NULL hired_date, DATE'2009-12-31' admission_date
FROM dual UNION ALL
SELECT 5 id, 'Isabelle' given_name, 'Squirrel' family_name, 'Miss' title ,
NULL hired_date, DATE'2014-01-01' admission_date
FROM dual UNION ALL
SELECT 6 id, 'Justin' given_name, 'Frog' family_name, 'Master' title,
NULL hired_date, DATE'2015-04-22' admission_date
FROM dual UNION ALL
SELECT 7 id, 'Lisa' given_name, 'Owl' family_name, 'Dr' title,
DATE'2015-01-01' hired_date, NULL admission_date
FROM dual
)
SELECT * FROM names
12 row(s) inserted.
Rollback insert operation on PEOPLE table
ROLLBACK
Statement processed.
Conditionally insert multiple rows into the PEOPLE, PATIENTS, and STAFF tables
INSERT ALL
/* Everyone is a person, so insert all rows into people */
WHEN 1=1 THEN
INTO people (person_id, given_name, family_name, title)
VALUES (id, given_name, family_name, title)
/* Only people with an admission date are patients */
WHEN admission_date IS NOT NULL THEN
INTO patients (patient_id, last_admission_date)
VALUES (id, admission_date)
/* Only people with a hired date are staff */
WHEN hired_date IS NOT NULL THEN
INTO staff (staff_id, hired_date)
VALUES (id, hired_date)
WITH names AS (
SELECT 4 id, 'Ruth' given_name, 'Fox' family_name, 'Mrs' title,
NULL hired_date, DATE'2009-12-31' admission_date
FROM dual UNION ALL
SELECT 5 id, 'Isabelle' given_name, 'Squirrel' family_name, 'Miss' title ,
NULL hired_date, DATE'2014-01-01' admission_date
FROM dual UNION ALL
SELECT 6 id, 'Justin' given_name, 'Frog' family_name, 'Master' title,
NULL hired_date, DATE'2015-04-22' admission_date
FROM dual UNION ALL
SELECT 7 id, 'Lisa' given_name, 'Owl' family_name, 'Dr' title,
DATE'2015-01-01' hired_date, NULL admission_date
FROM dual
)
SELECT * FROM names
8 row(s) inserted.