SQLTeam.com | Weblogs | Forums

How do i fix this DDL script?

sql2012

#1

How do fix the following problems:

Msg 1769, Level 16, State 1, Line 3
Foreign key 'FK_KLANT_REF_klant' references invalid column 'klant_id' in referencing table 'klant'.
Msg 1750, Level 16, State 0, Line 3
Could not create constraint. See previous errors.
Msg 1778, Level 16, State 0, Line 3
Column 'verhuur.verhuur_id' is not the same data type as referencing column 'verzekering.verhuur_ID' in foreign key 'FK_VERZEKERING_REF_VERHUUR'.
Msg 1750, Level 16, State 0, Line 3
Could not create constraint. See previous errors.
Msg 4902, Level 16, State 1, Line 2
Cannot find the object "schade" because it does not exist or you do not have permissions.
Msg 102, Level 15, State 1, Line 37
Incorrect syntax near 'fietsSoort'.

ddl insert script


use master
go

if object_id('biker2') is not null
drop database biker2
go

/==============================================================/
/* Database: biker2 /
/
==============================================================*/
create database biker2
go

use biker2
go

/==============================================================/
/* Table: rol /
/
==============================================================*/
CREATE TABLE rol(
rol_id int not null,
rol_naam varchar(20) not null,
constraint PK_ROL primary key (rol_id)
)
go

/==============================================================/
/* Table: medewerker /
/
==============================================================*/
CREATE TABLE medewerker (
medewerker_id int not null,
loginnaam varchar(20) null,
[password] varchar(255) null,
voornaam numeric(2) not null,
achternaam numeric(2) not null,
gebDatum date not null,
constraint PK_MEDEWERKER primary key (medewerker_id)
)
go

/==============================================================/
/* Table: klant /
/
==============================================================*/
CREATE TABLE klant (
klant_id int not null,
voornaam varchar (50) not null,
achternaam varchar (50) not null,
adres varchar (50) not null,
postcode varchar (6) not null,
plaats varchar (30) not null,
emailadres varchar (20) not null,
gebDatum varchar (10) not null,
geslacht varchar (5) not null,
username varchar (40) not null,
[password] varchar (255) not null, -- 255 ivm hashing password
telnr numeric (13) not null,
constraint PK_KLANT primary key (klant_id)
constraint AK_KLANT unique (plaats)
)
go

/==============================================================/
/* Table: verhuur /
/
==============================================================*/
CREATE TABLE verhuur (
verhuur_id int not null,
startdatum date not null,
einddatum date not null,
betaaldateum date not null,
verhuurstatus int not null,
boekings_status int not null,
constraint PK_VERHUUR primary key (verhuur_id)

)
go

/==============================================================/
/* Table: fietstype /
/
==============================================================*/
CREATE TABLE fietstype (
fietstype int not null,
fiets_id int not null,
constraint PK_STUK primary key (fiets_id)
)
go

/==============================================================/
/* Table: accessoiretype /
/
==============================================================*/
CREATE TABLE acccesoiretype (
accessoire_type_id int not null,
helmen varchar not null,
kinderzitjes varchar not null,
fietstassen varchar not null,
constraint PK_ACCESSOIRETYPE primary key (accessoire_type_id),
)
go

/==============================================================/
/* Table: verzekering /
/
==============================================================*/
CREATE TABLE verzekering (
verhuur_id varchar(14) not null,
polisnr varchar(7) not null,
startdatum varchar(7) not null,
einddatum date not null,
verzekeringsmaatschappij varchar (15) not null
constraint PK_VERZEKERING primary key (verhuur_id)
)
go

/==============================================================/
/* Table: verhuurfiets /
/
==============================================================*/
CREATE TABLE verhuurfiets (
verhuur_id int not null,
fiets_id int not null,
constraint PK_VERHUURFIETS primary key (verhuur_id),

)
go

/==============================================================/
/* Table: fiets /
/
==============================================================*/
CREATE TABLE fiets (
fiets_id int not null,
fietsType int not null,
fietsSoort int not null,
constraint PK_fiets primary key (fiets_id),
)
go

/==============================================================/
/* Table: fietstype /
/
==============================================================*/
CREATE TABLE fietstype (
fietstype varchar (15) not null,
fietstypeid int not null,
fiets_id int not null,
constraint PK_STUK primary key (fiets_id),
)
go

/* toevoegen foreignkeys met ALTER TABLE */
alter table klant
add constraint FK_KLANT_REF_klant foreign key (klant_id)
references klant (klant_id)
-- on update cascade
go

alter table verzekering
add constraint FK_VERZEKERING_REF_VERHUUR foreign key (verhuur_ID)
references verhuur (verhuur_ID)
-- on update cascade
go

alter table schade
add constraint FK_SCHADE_REF_VERZEKERING foreign key (verzekering_ID)
references verzekering (verzekering_ID)
-- on update cascade
go

/* Toevoegen voorbeeldpopulatie */

INSERT INTO klant (emailadres, [password]) VALUES ('martin.degraaf@hotmail.com', 'Hjkre%3k4lKm#1')
INSERT INTO klant (emailadres, [password]) VALUES ('boris.maas@gmail.com', 'B3@jskKlMna32')
INSERT INTO klant (emailadres, [password]) VALUES ('K.leest@yahoo.com', 'J^7@wEtYnBkLlrT')

INSERT INTO klant (voornaam, achternaam, gebDatum, geslacht, telnr) VALUES ('Martin', 'van de Graaf', '1984-11-20', 'Man', '31612445849')
INSERT INTO klant (voornaam, achternaam, gebDatum, geslacht, telnr) VALUES ('Boris', 'Maas', '1963-04-13', 'Man', '31612114549')
INSERT INTO klant (voornaam, achternaam, gebDatum, geslacht, telnr) VALUES ('Karin', 'Leest', '1959-03-10', 'Vrouw', '31612445479')

INSERT INTO adres (straatnaam_huisnummer, postcode, woonplaats) VALUES ('De breestraat 32', '6042ER', 'Roermond')
INSERT INTO adres (straatnaam_huisnummer, postcode, woonplaats) VALUES ('Mauritius 43', '6043NJ', 'Roermond')
INSERT INTO adres (straatnaam_huisnummer, postcode, woonplaats) VALUES ('Kuikstraat 12', '6100ER', 'Echt')

INSERT INTO verhuur (startdatum, einddatum, verhuurstatus, polisnr) VALUES ('2016-10-09', '2016-10-11' ,'1', 'acs127845127')
INSERT INTO verhuur (startdatum, einddatum, verhuurstatus, polisnr) VALUES ('2016-10-09', '2016-10-13' ,'1', 'azb144578264')
INSERT INTO verhuur (startdatum, einddatum, verhuurstatus, polisnr) VALUES ('2016-10-10', '2016-10-12' ,'1', 'art157113514')

INSERT INTO verzekering (verzekeringsmaatschappij) VALUES ('Unive')
INSERT INTO verzekering (verzekeringsmaatschappij) VALUES ('Allianz')
INSERT INTO verzekering (verzekeringsmaatschappij) VALUES ('Unigarant')

INSERT INTO schade (schadeDatum, schadeCommentaar) VALUES ('2016-10-11' , 'De achterband was lek bij binnenkomst')
INSERT INTO schade (schadeDatum, schadeCommentaar) VALUES ('2016-10-13' , 'De ketting was gebroken')
INSERT INTO schade (schadeDatum, schadeCommentaar) VALUES ('2016-10-12' , 'De voor lamp was stuk')

INSERT INTO accessoire (accessoires) VALUES ('1', '2', '3')

INSERT INTO fiets (fietsSoort, fietsType) VALUES ('2', '1')
(fietsSoort, fietsType) VALUES ('1', '1')
(fietsSoort, fietsType) VALUES ('1', '2');


#2

Looks like a FK pointing to itself. Het zou een probleem kunnen zijn. Maybe you mean to alter a different table?


#3

FK_KLANT_REF_klant_id
use this hope this helps you.


#4

http://superuser.com/questions/40097/what-is-a-ddl-script-in-the-context-of-databases


#5

DDL is an acronym for Data Definition Language. It's the SQL syntax to create, alter or drop objects.