Скачиваний:
36
Добавлен:
28.06.2014
Размер:
19.26 Кб
Скачать

Лабораторная работа № 8.

Целостность БД.

Выполнили:

Ахремцев Я.В. Стрельцов А.А.

Проверила:

Сидорова Н.П.

  1. Создание таблицы:

create table cir1

(nomer integer check (nomer between 1 and 700000),

fio char (40) not null check (fio not like '%.%' or fio not like '%-%'),

d_rogd char(2) check (d_rogd between '01' and '31'),

m_rogd char(2) check (m_rogd between '01' and '12'),

g_rogd char(2) check (g_rogd between '00' and '99'),

pol char(1) check (pol like 'М' or pol like 'Ж'),

sem_polog_k char(1) check (sem_polog_k in ('0', '1', '2', '3')),

spec char(6) check (spec between '000000' and '190046'),

kat_obuch_k char(2) check (kat_obuch_k between '01' and '17'),

vuz_k integer check (vuz_k between 128955 and 8199999),

mp char(2) check (mp between '01' and '12'),

gp char(2) check (gp between '00' and '99'),

mo char(2) check (mo between '01' and '12'),

gok char(2) check (gok between '00' and '99'))

  1. Добавление правил целостности

alter table cir1

add constraint rules check

(((g_rogd<gp) and (gp<gok)) or

(kat_obuch_k in ('07', '08','09') and

spec not like '00%') or

(not(kat_obuch_k in ('07', '08','09')) and

spec like '00%') or

(pol='Ж' and

fio like '%А %[А-Я] %[А-Я]') or

(pol='М' and

fio not like '%А %[А-Я] %[А-Я]'))

  1. Примеры работы правил целостности

а) Корректный запрос:

insert cir1 values

(106002,’Деточкин Титомир Вячеславович ','15','05', '64',

'M', '1','000050','05', '128957','05','78','05','88')

Go

б) Некорректный запрос(Ошибка в первом столбце):

insert cir1 values

(0, ’Деточкин Титомир Вячеславович ','15','05', '64',

'M', '1','000050','05', '128957','05','78','05','88')

go

Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the CHECK constraint "CK__cir1__nomer__7E6CC920". The conflict occurred in database "StreltsovAlA", table "dbo.cir1", column 'nomer'.

The statement has been terminated.

в) Некорректный запрос(специальность и категория специальности заданы неверно):

insert cir1 values

(1, ’Деточкин Титомир Вячеславович ','15','05', '64',

'Ì', '1','000050','07', '128957','05','78','05','88')

Go

Msg 547, Level 16, State 0, Line 1

The INSERT statement conflicted with the CHECK constraint "rules1". The conflict occurred in database "StreltsovAlA", table "dbo.cir1".

The statement has been terminated.

  1. Добавление правила

create rule rule1 as (@number > 200 and @number < 500000);

Прикрепление правила к таблице:

EXEC sp_bindrule 'rule1', 'cir1.[nomer]'

а) Корректный запрос:

insert cir1 values

(201,'Деточкин Титомир Вячеславович ','15','05', '64',

'М', '1','000050','05', '128957','05','78','05','88')

Go

(1 row(s) affected)

б) Некорректный запрос:

insert cir1 values

(199,'Деточкин Титомир Вячеславович ','15','05', '64',

'M', '1','000050','05', '128957','05','78','05','88')

Go

Msg 513, Level 16, State 0, Line 1

A column insert or update conflicts with a rule imposed by a previous CREATE RULE statement. The statement was terminated. The conflict occurred in database 'ZakharovDanS', table 'dbo.cir1', column 'nomer'.

The statement has been terminated.

Соседние файлы в папке Ахремцев, Стрельцов