add all the required constraints

in addition I've fixed some minor bugs.
This commit is contained in:
Andreas Zweili 2017-06-26 21:18:21 +02:00
parent 1d4128e8f9
commit 7ba52b07ce
2 changed files with 116 additions and 12 deletions

Binary file not shown.

View File

@ -6,6 +6,8 @@
-- Datum: 2017-06-05
-- Server Version: SQL Server 2016
-- create table
use marketdb;
if not exists (select * from sysobjects where name='commercials')
CREATE TABLE commercials (
@ -24,16 +26,16 @@ if not exists (select * from sysobjects where name='member_status')
);
use marketdb;
if not exists (select * from sysobjects where name='salutation')
CREATE TABLE salutation (
if not exists (select * from sysobjects where name='salutations')
CREATE TABLE salutations (
salutation_id int not null,
salutation_name varchar(50) not null,
constraint salutation_pk primary key (salutation_id)
);
use marketdb;
if not exists (select * from sysobjects where name='subscription')
CREATE TABLE subscription (
if not exists (select * from sysobjects where name='subscriptions')
CREATE TABLE subscriptions (
subscription_id int not null,
max_rented_locations int not null,
subscription_price money not null,
@ -43,8 +45,8 @@ if not exists (select * from sysobjects where name='subscription')
);
use marketdb;
if not exists (select * from sysobjects where name='subscption_order')
CREATE TABLE subscption_order (
if not exists (select * from sysobjects where name='subscption_orders')
CREATE TABLE subscption_orders (
subscription_order_id int not null,
running_subscription bit,
subscription_order_date date not null,
@ -73,17 +75,18 @@ if not exists (select * from sysobjects where name='persons')
firstname varchar(50) not null,
lastname varchar(50) not null,
date_of_birth date not null,
email_adress varchar(50) not null,
email_address varchar(50) not null,
streetname varchar(50) not null,
streetnumber int,
city_id int not null,
country_id int not null,
constraint person_pk primary key (person_id)
constraint person_pk primary key (person_id),
constraint constraint_email_address unique (email_address)
);
use marketdb;
if not exists (select * from sysobjects where name='trial_period')
CREATE TABLE trial_period (
if not exists (select * from sysobjects where name='trial_periods')
CREATE TABLE trial_periods (
trial_period_id int not null,
duration_in_seconds int not null,
constraint trial_period_pk primary key (trial_period_id)
@ -95,8 +98,8 @@ if not exists (select * from sysobjects where name='quality_checks')
quality_check_id int not null,
check_passed bit,
due_date date not null,
checking_member int not null,
checked_member int not null,
checking_member_id int not null,
checked_member_id int not null,
constraint quality_check_pk primary key (quality_check_id)
);
@ -148,3 +151,104 @@ if not exists (select * from sysobjects where name='rent_prices')
rent_price money not null,
constraint rent_price_pk primary key (rent_price_id)
);
-- Add constraints
use marketdb;
IF OBJECT_ID('dbo.[fk_com_member_id]', 'F') IS NULL
ALTER TABLE commercials ADD CONSTRAINT fk_com_member_id
FOREIGN KEY (member_id) REFERENCES members (member_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_mem_status_id]', 'F') IS NULL
ALTER TABLE members ADD CONSTRAINT fk_mem_status_id
FOREIGN KEY (member_status_id)
REFERENCES member_status (member_status_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_per_salutation_id]', 'F') IS NULL
ALTER TABLE persons ADD CONSTRAINT fk_per_salutation_id
FOREIGN KEY (salutation_id) REFERENCES salutations (salutation_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_per_city_id]', 'F') IS NULL
ALTER TABLE persons ADD CONSTRAINT fk_per_city_id
FOREIGN KEY (city_id) REFERENCES cities (city_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_per_country_id]', 'F') IS NULL
ALTER TABLE persons ADD CONSTRAINT fk_per_country_id
FOREIGN KEY (country_id) REFERENCES countries (country_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_sor_subscribtion_id]', 'F') IS NULL
ALTER TABLE subscption_orders ADD CONSTRAINT fk_sor_subscribtion_id
FOREIGN KEY (subscription_id)
REFERENCES subscriptions (subscription_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_sor_member_id]', 'F') IS NULL
ALTER TABLE subscption_orders ADD CONSTRAINT fk_sor_member_id
FOREIGN KEY (member_id) REFERENCES members (member_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_sor_location_id]', 'F') IS NULL
ALTER TABLE subscption_orders ADD CONSTRAINT fk_sor_location_id
FOREIGN KEY (location_id) REFERENCES locations (location_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_mem_person_id]', 'F') IS NULL
ALTER TABLE members ADD CONSTRAINT fk_mem_person_id
FOREIGN KEY (person_id) REFERENCES persons (person_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_qch_checking_member_id]', 'F') IS NULL
ALTER TABLE quality_checks ADD CONSTRAINT fk_qch_checking_member_id
FOREIGN KEY (checking_member_id) REFERENCES members (member_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_qch_checked_member_id]', 'F') IS NULL
ALTER TABLE quality_checks ADD CONSTRAINT fk_qch_checked_member_id
FOREIGN KEY (checked_member_id) REFERENCES members (member_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_ren_member_id]', 'F') IS NULL
ALTER TABLE rents ADD CONSTRAINT fk_ren_member_id
FOREIGN KEY (member_id) REFERENCES members (member_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_ren_rent_price_id]', 'F') IS NULL
ALTER TABLE rents ADD CONSTRAINT fk_ren_rent_price_id
FOREIGN KEY (rent_price_id) REFERENCES rent_prices (rent_price_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_rpr_location_id]', 'F') IS NULL
ALTER TABLE rent_prices ADD CONSTRAINT fk_rpr_location_id
FOREIGN KEY (location_id) REFERENCES locations (location_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_loc_city_id]', 'F') IS NULL
ALTER TABLE locations ADD CONSTRAINT fk_loc_city_id
FOREIGN KEY (city_id) REFERENCES cities (city_id);
go
use marketdb;
IF OBJECT_ID('dbo.[fk_loc_country_id]', 'F') IS NULL
ALTER TABLE locations ADD CONSTRAINT fk_loc_country_id
FOREIGN KEY (country_id) REFERENCES countries (country_id);
go