Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
БД-ЛР-1.docx
Скачиваний:
1
Добавлен:
19.09.2019
Размер:
799.47 Кб
Скачать

МИНИСТЕРСТВО ОБРАЗОВАНИЯ РОССИЙСКОЙ ФЕДЕРАЦИИ

КОСТРОМСКОЙ ГОСУДАРСТВЕННЫЙ ТЕХНОЛОГИЧЕСКИЙ УНИВЕРСИТЕТ

КАФЕДРА АМТ

Лабораторная работа №1 проектирование реляционных бд и их реализация в PostgreSql

Выполнили: студенты группы 08-А-1 «Б» М.И. Рубликов А.О. Шальнов Проверил: В.Н. Ершов

Кострома 2012

Задание 1: создать базу данных «Электрооборудование».

ER-диаграмма:

SQL-скрипт:

create table ceh

(

id_ceh serial primary key,

nazv char(20) not null,

kol_smen int not null constraint ogr1

check((kol_smen>0)and (kol_smen<=3))

);

create table stanok

(

id_st serial primary key,

naim char(20) not null,

kol_ch int not null constraint ogr2

check((kol_ch>=6)and (kol_ch<=8))

);

create table c_s

(

id_c_s serial primary key,

id_ceh int not null references ceh,

id_st int not null references stanok

);

create table ed

(

tip serial primary key,

mosh int not null constraint ogr3

check(mosh>0),

tok int not null constraint org4

check(tok>0),

kpt real not null

);

create table privod

(

kz real not null constraint ogr5

check((kz>0)and(kz<=1)),

Id_st int not null references stanok,

tip_ed int not null references ed

);

alter table privod add constraint pk1

primary key (id_st,tip_ed);

Результат выполнения запросов:

Таблица «Цех»

Таблица «Станок»

Таблица «ЭД»

Таблица «Ц_С»

Таблица «Привод»

Задание 2: Создать базу данных «Университет», используя в качестве задания экзаменационно-зачётную ведомость.

ER-диаграмма: см. в приложении.

SQL-скрипт:

Создание исходной БД:

create table facultet

(

id_f serial primary key,

name_f char(40) not null

);

create table gruppa

(

id_g serial primary key,

name_g char(40) not null,

spec char(40) not null,

kurs int not null

);

create table f_g

(

id_f int not null references facultet,

id_g int not null references gruppa,

constraint pk2 primary key (id_f, id_g)

);

create table vivod

(

id_v serial primary key,

otl int not null,

hor int not null,

ud int not null,

neud int not null,

na int not null,

data_ekz date not null

);

create table ekzamen

(

id_e serial primary key,

disc char(40) not null,

data_otm date not null,

reg_n char(40) not null,

prepod char(40) not null,

aud char(40) not null,

otm char(40) not null

);

create table e_v

(

id_e_v serial primary key,

id_e int not null references ekzamen,

id_v int not null references vivod

);

create table semestr

(

id_s serial primary key,

ser int not null

);

create table sem_ekz

(

id_s_e serial primary key,

id_s int not null references semestr,

id_e int not null references ekzamen

);

create table student

(

Id_st serial primary key,

st char(40) not null,

reg_n char(40) not null,

Id_g int not null references gruppa

);

create table st_ekz

(

id_st_e serial primary key,

id_st int not null references student,

id_e int not null references ekzamen

);

Заполнение исходной БД:

insert into facultet (name_f)

values ('FAST');

insert into facultet (name_f)

values ('UIN');

insert into facultet (name_f)

values ('LMF');

insert into gruppa (name_g,spec,kurs)

values ('08-A-01','AMT',4);

insert into gruppa (name_g,spec,kurs)

values ('07-D-02','Les',5);

insert into gruppa (name_g,spec,kurs)

values ('08-U-01','Yur',4);

insert into gruppa (name_g,spec,kurs)

values ('08-CA-04','SAPR',4);

insert into f_g (id_f,id_g)

values (7,9);

insert into f_g (id_f,id_g)

values (7,12);

insert into f_g (id_f,id_g)

values (8,11);

insert into f_g (id_f,id_g)

values (9,10);

insert into student (st,reg_n,id_g)

values ('Смирнов И.И.','08-A-022',9);

insert into student (st,reg_n,id_g)

values ('Иванов В.С.','08-A-028',9);

insert into student (st,reg_n,id_g)

values ('Фёдоров Ф.Ф.','07-D-010',10);

insert into student (st,reg_n,id_g)

values ('Ломагин А.В.','08-U-052',11);

insert into student (st,reg_n,id_g)

values ('Горшков А.В.','08-CA-006',12);

insert into semestr (ser)

values (1);

insert into semestr (ser)

values (2);

insert into semestr (ser)

values (3);

insert into semestr (ser)

values (4);

insert into semestr (ser)

values (5);

insert into semestr (ser)

values (6);

insert into semestr (ser)

values (7);

insert into semestr (ser)

values (8);

insert into semestr (ser)

values (9);

insert into semestr (ser)

values (10);

insert into ekzamen (disc,data_otm,reg_n,prepod,aud,otm)

values ('Вышка','2009-06-14','08-A-022','Землякова И.В.','E-415','хорошо');

insert into ekzamen (disc,data_otm,reg_n,prepod,aud,otm)

values ('Физика','2009-06-24','08-A-022','Бородин И.П.','E-226','отлично');

insert into ekzamen (disc,data_otm,reg_n,prepod,aud,otm)

values ('УСРВ','2012-06-10','08-A-028','Олоничев В.В.','Б-403','отлично');

insert into st_ekz (id_st,id_e)

values (1,4);

insert into st_ekz (id_st,id_e)

values (1,5);

insert into st_ekz (id_st,id_e)

values (2,6);

insert into sem_ekz (id_s,id_e)

values (2,4);

insert into sem_ekz (id_s,id_e)

values (2,5);

insert into sem_ekz (id_s,id_e)

values (8,6);

insert into vivod (otl,hor,ud,neud,na,data_ekz)

values (0,1,0,0,0,'2009-06-14');

insert into vivod (otl,hor,ud,neud,na,data_ekz)

values (1,0,0,0,0,'2009-06-24');

insert into vivod (otl,hor,ud,neud,na,data_ekz)

values (1,0,0,0,0,'2012-06-10');

insert into e_v (id_e,id_v)

values (4,1);

insert into e_v (id_e,id_v)

values (5,2);

insert into e_v (id_e,id_v)

values (6,3);

Результаты выполнения запросов:

Таблица «Факультет»

Таблица «Группа»

Таблица «Ф_Г»

Таблица «Выводы»

Таблица «Экзамен»

Таблица «ЭВ»

Таблица «Семестр»

Таблица «Семестр_экзамен»

Таблица «Студент»

Таблица «СЭ»

Создание «правильной» БД (исправление):

create table NEW_kafedra

(

id_k serial primary key,

name_k char(100) not null,

name_f char(100) not null

);

create table NEW_spec

(

id_sp serial primary key,

name_sp char(100) not null,

id_k int not null references NEW_kafedra

);

create table NEW_gruppa

(

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]