8.4
Consider a database schema with a relation Emp whose attributes are as shown below, with types specified for multivalued attributes.
Emp = (ename, ChildrenSet multiset(Children), SkillSet multiset(Skills))
Children = (name, birthday)
Skills = (type, ExamSet setof(Exams))
Exams = (year, city)Define the above schema in SQL, using the SQL Server table type syntax from Section 8.2.1.1 to declare multiset attributes.
Using the SQL Server table type syntax from Section 8.2.1.1.
CREATE TYPE children AS TABLE (
VARCHAR(30),
name DATE
birthday
);
CREATE TYPE exams AS TABLE (
year INTEGER,
VARCHAR(30)
city
);
CREATE TYPE skills AS TABLE (
type VARCHAR(30),
examset exams
);
CREATE TABLE emp (
VARCHAR(30),
ename
childrenset children,
skillset skills );
To be honest I haven’t tried the above code on an instance of SQL Server.
The following is a similar code to the one above, but for Postgresql.
CREATE TYPE children AS (
VARCHAR(30),
name DATE
birthday
);
CREATE TYPE exams AS (
year INTEGER,
VARCHAR(30)
city
);
CREATE TYPE skills AS (
type VARCHAR(30),
examset exams[]
);
CREATE TABLE emp (
VARCHAR(30),
ename
childrenset children[],
skillset skills[] );
If you are interested to see, how we insert into emp, the following code shows for postgresql:-
INSERT INTO emp VALUES (
'Elliot Alderson',
ARRAY[
'child1', '2000-01-01')::children,
('child2', '2017-09-10')::children
(
],ARRAY[
'programming', ARRAY[
(2020,'NY')::exams,
(2021,'AA')::exams
(:skills,
]):'KCNA',ARRAY[
(2022,'NY')::exams,
(2022,'LA')::exams
(:skills
]):
] );