I have a database diagram and need to create a Database with different tables.
This is my code:
use FirmaLieferungen;
drop table liefert;
drop table rabatt;
drop table artikel;
drop table firma;
SET DATEFORMAT dmy;
create table firma (
fnr integer primary key,
name char(10),
jahrgruendung integer, -- Gründungsjahr
land char(3)
);
insert into firma values (101,'Schwer' ,1890,'A' );
insert into firma values (102,'Schmal' ,1901,'CH' );
insert into firma values (103,'Tief' ,1945,'I' );
insert into firma values (104,'Breit' ,1950,'A' );
insert into firma values (105,'Leicht' ,1945,'F' );
insert into firma values (106,'Hoch' ,1920,'CH' );
insert into firma values (107,'Hell' ,1900,'A' );
create table artikel (
fnr integer,
lfdnr integer,
bezeichnung char(10),
preis decimal(6,2),
einheit char(3),
land char(3),
primary key(fnr, lfdnr),
foreign key(fnr) references firma
);
insert into artikel values (101,1,'Schaufel' ,12.30,'Stk','A' );
insert into artikel values (101,2,'Hacke' ,15.20,'Stk','F' );
insert into artikel values (102,1,'Spaten' ,13.00,'Stk','A' );
insert into artikel values (103,1,'Schere' , 8.00,'Stk','A' );
insert into artikel values (103,2,'Messer' ,10.60,'Stk','F' );
insert into artikel values (103,3,'Schnur' , 1.10,'m' ,'D' );
insert into artikel values (105,1,'Schnur' , 0.40,'m' ,'D' );
insert into artikel values (106,1,'Hacke' ,20.70,'Stk','CH' );
insert into artikel values (106,2,'Draht' , 0.60,'m' ,'CH' );
create table liefert (
fnrvon integer,
fnran integer,
fnr integer,
lfdnr integer,
datum date,
menge decimal(8,2)
primary key(fnrvon, fnran, fnr, lfdnr, datum),
foreign key(fnr, lfdnr) references artikel,
foreign key(fnr) references firma
);
insert into liefert values (101,102,101,1,'01.02.1999', 3.00);
insert into liefert values (101,102,101,1,'02.01.2000', 2.00);
insert into liefert values (101,104,101,2,'13.02.2000', 11.00);
insert into liefert values (101,104,101,1,'24.11.1999', 19.00);
insert into liefert values (101,105,103,3,'31.03.2001', 1553.00);
insert into liefert values (102,101,102,1,'21.04.1999', 28.00);
insert into liefert values (102,101,101,1,'11.12.1999', 1.00);
insert into liefert values (102,104,101,1,'04.07.2000', 63.00);
insert into liefert values (103,101,103,3,'21.04.1999', 3.25);
insert into liefert values (103,104,101,1,'08.02.1998', 17.00);
insert into liefert values (104,102,105,1,'19.11.2001', 132.50);
insert into liefert values (104,106,101,1,'04.07.2000', 22.00);
insert into liefert values (106,102,101,1,'07.08.2002', 81.00);
insert into liefert values (106,102,106,2,'01.06.2002', 21.30);
insert into liefert values (106,104,101,1,'26.09.2001', 2.00);
create table rabatt (
fnrvon integer,
fnran integer,
prozent decimal (5,2),
primary key (fnrvon, fnran),
foreign key (fnrvon, fnran) references firma
);
insert into rabatt values (101,102, 5.25);
insert into rabatt values (102,101, 5.50);
insert into rabatt values (101,103,15.75);
insert into rabatt values (103,102, 7.50);
insert into rabatt values (102,103,10.50);
insert into rabatt values (105,106, 5.25);
insert into rabatt values (104,101, 7.50);
select * from rabatt;
select * from firma;
select * from liefert;
select * from artikel;
But there's an error in the 'rabatt' creation, it says that the last command is invalid.
foreign key (fnrvon, fnran) references firma
This is somehow wrong, but I don't know why... Is the diagram wrong? There are also two keys going from 'liefert' to 'firma' how do I do this? Please help me!
Thanks! (I'm using Microsoft SQL Server 2008)
When you reference another table, you should specify which column(s) you are referencing.
So, for example:
create table liefert (
fnrvon integer,
fnran integer,
fnr integer,
lfdnr integer,
datum date,
menge decimal(8,2)
primary key(fnrvon, fnran, fnr, lfdnr, datum),
foreign key(fnr, lfdnr) references artikel (fnr, lfdnr),
foreign key(fnr) references firma (fnr)
);
Mureinik is right, just answered 1 sec before me.
For the sake of the question I crated a Fiddle Improve it for further questions
http://sqlfiddle.com/#!6/6a1b7
Your firma table doesn't have columns named fnrvon or fnran so
foreign key (fnrvon, fnran) references firma
fails to figure out what is referenced.
You need to be explicit. Also, If the two columns are both referencing the same foreign key, then it needs to be two separate statements
foreign key (fnran) references firma (fnr)
foreign key (fnrvon) references firma (fnr)
Related
creating table and inserting data into a table and now it giving me an error
SQL Error: ORA-02291: integrity constraint (S21403051.SYS_C007300)
violated - parent key not found 02291. 00000 - "integrity
constraint (%s.%s) violated - parent key not found" *Cause:
CREATE TABLE CUSTOMER(
CUSTOMER_ID VARCHAR(10) PRIMARY KEY,
FIRST_NAME VARCHAR(10),
SURNAME VARCHAR(15),
CUSTOMER_TEL VARCHAR(12),
CUSTOMER_EMAIL VARCHAR(30)
)
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_102', 'EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_53');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_107','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_106','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1','CUST_108','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_104','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_109','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_52');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_51');
When you have defined 6 columns CUSTOMER_ID , FIRST_NAME , SURNAME ,
CUSTOMER_TEL,CUSTOMER_EMAIL and when you try to insert values, it takes in order of table definition. Instead you can try this way
Example:
INSERT INTO CUSTOMER_CRUISES
(column1, clumn2,column3)
values ('xx','xy','yz')
Yet, you can not insert duplicate value into first column as primary key is defined on it. And, it cant be null.
Hope this helps
Cause: A foreign key value has no matching primary key value.
This problem arises when you attempt to insert a record containing the Customer_ID column into the child table (CUSTOMER_CRUISES) and that this Customer_ID is not present in the parent table (CUSTOMER). When the Customer_ID (Foreign key) in CUSTOMER_CRUISES table does not get to reference the Customer_ID (primary key) in CUSTOMER table, an error is raised.
One workaround is to insert and make sure that the value is present in the CUSTOMER table first before inserting the values into the CUSTOMER_CRUISES table.
I am doing the end-of-course work of BBDD and when doing the E / R model, I have a doubt, How do I update the record of a table M: N?. I have an M: N relation, A table for each entity and also a joint with their keys as primary in that table and foreign of the other tables. What I basically want to do is that when I add the record to the main table, it is updated in the middle. My teacher tells me that a possible solution would be a PL / SQL block, but the truth is that I do not have much idea how to do it.
Being a relation M: N I will create the table "teacher", table "course" and the table "prof-curs". How do I update in prof-curs when entering a new teacher or course record? Thank you.
Attached image of my E / R model made in a day.
Example of my SQL code
create table profesor(Id_profesor int primary key identity(100,1), nombre varchar(50), apellido varchar(50) not null,password varchar(50) not null,
sueldo numeric(10,2) not null,tipo varchar(50),Id_dept int not null )
Insert into profesor (nombre, apellido, password,sueldo,tipo,Id_dept) values ('ana', 'polo', 'ana','1500','admin',3)
Insert into profesor (nombre, apellido, password,sueldo,tipo,Id_dept) values ('pepe', 'garcia', 'pepe','1000','administrativo',2)
Insert into profesor (nombre, apellido, password,sueldo,tipo,Id_dept) values ('lucia', 'perez', 'lucia','1300','profesor',1)
create table curso (Id_curso int primary key identity (10,1),
nombreCurso varchar(50) not null,
nivel varchar(4) not null)
insert into curso (nombreCurso,nivel) values ('ingles', 'A1')
insert into curso (nombreCurso,nivel) values ('ingles', 'A2')
insert into curso (nombreCurso,nivel) values ('ingles', 'b1')
insert into curso (nombreCurso,nivel) values ('frances', 'A1')
insert into curso (nombreCurso,nivel) values ('frances', 'A2')
insert into curso (nombreCurso,nivel) values ('frances', 'B1')
insert into curso (nombreCurso,nivel) values ('frances', 'A1')
create table prof_cur(Id_Profesor int not null ,Id_curso int not null,Id_idioma int not null)
Alter table prof_cur
Add Constraint Fk_prof
Foreign Key(Id_Profesor) References empleado(Id_Profesor)
Alter table prof_cur_idioma
Add Constraint Fk_cur
Foreign Key(Id_curso) References curso(Id_curso)
I am trying to create tables in oracle application express. All the tables created well except of Chessplayer and i really don't know what's the matter.
Can somebody please tell me what's wrong with the SQL below.
DROP TABLE Сhessplayer CASCADE CONSTRAINTS;
CREATE TABLE Chessplayer
(
Chessplayer_ID INTEGER NOT NULL ,
Surname CHAR (50) ,
Name CHAR (40) ,
Fathers_name CHAR (40) ,
Side CHAR (10) ,
Birth_date DATE ,
Date_of_entering DATE ,
Privelegies CLOB ,
Levels INTEGER Levels > 0 AND Levels < 6 ,
Part_ID INTEGER NOT NULL ,
Money_ID INTEGER NOT NULL
) ;
ALTER TABLE Chessplayer ADD CONSTRAINT Chessplayer_PK PRIMARY KEY ( Chessplayer_ID ) ;
INSERT INTO Chessplayer VALUES
(1,'Ivanov','Igor','Vladimirivich','WHITE', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,1,1);
INSERT INTO Chessplayer VALUES
(2,'Sokolov','Andrey','Semenovich','BLACK', to_date('20-8-1994','dd-mm-yyyy'), to_date('15-6-2014','dd-mm-yyyy'), 'SUPER SMART',1,1,1);
INSERT INTO Chessplayer VALUES
(3,'Smelev','Victor','Petrovich','WHITE', to_date('11-9-1992','dd-mm-yyyy'), to_date('21-12-2014','dd-mm-yyyy'), 'SUPER SMART',1,2,1);
INSERT INTO Chessplayer VALUES
(4,'Sidorov','Sergey','Igorevich','BLACK', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,2,1);
INSERT INTO Chessplayer VALUES
(5,'Sidorov','Sergey','Igorevich','BLACK', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,3,1);
INSERT INTO Chessplayer VALUES
(6,'Ivanov','Igor','Vladimirivich','WHITE', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,3,1);
INSERT INTO Chessplayer VALUES
(7,'Timurov','Aleksey','Geogrievich','WHITE', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,1,2);
INSERT INTO Chessplayer VALUES
(8,'Antonevich','Egor','Vasilievich','BLACK', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,1,2);
INSERT INTO Chessplayer VALUES
(9,'Stepanov','Aleksandr','Vladimirivich','WHITE', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,2,2);
INSERT INTO Chessplayer VALUES
(10,'Kireev','Vadim','Sergeevich','BLACK', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,2,2);
INSERT INTO Chessplayer VALUES
(11,'Timurov','Aleksey','Geogrievich','BLACK', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,3,2);
INSERT INTO Chessplayer VALUES
(12,'Stepanov','Aleksandr','Vladimirivich','WHITE', to_date('6-8-1993','dd-mm-yyyy'), to_date('17-1-2013','dd-mm-yyyy'), 'SUPER SMART',1,3,2);
ALTER TABLE Chessplayer ADD CONSTRAINT Chesspl_Reg_contr_FK FOREIGN KEY ( Money_ID ) REFERENCES Reg_contribution ( Reg_contr_ID ) ;
ALTER TABLE Chessplayer ADD CONSTRAINT Chessplayer_Party_FK FOREIGN KEY ( Part_ID ) REFERENCES Party ( Party_ID ) ;
And here's the errors in oracle application express:
CREATE TABLE Chessplayer ( Chessplayer_ID ORA-00907: missing right parenthesis
ALTER TABLE Chessplayer ADD CONSTRAINT Chessplayer_PK PRIMAR ORA-02260: table can have only one primary key
and for all inserts:
ORA-01722: invalid number
I would have written the table creation this way
-- Create table
create table CHESSPLAYER
(
CHESSPLAYER_ID INTEGER not null,
SURNAME CHAR(50),
NAME CHAR(40),
FATHERS_NAME CHAR(40),
SIDE CHAR(10),
BIRTH_DATE DATE,
DATE_OF_ENTERING DATE,
PRIVELEGIES CLOB,
LEVELS INTEGER,
PART_ID INTEGER not null,
MONEY_ID INTEGER not null
);
-- Create/Recreate check constraints
alter table CHESSPLAYER
add constraint CHKLEV
check (Levels > 0 AND Levels < 6);
That should solve all of your problems
I'm creating a script that creates a TABLE and I have problem with the last command which I have to do:
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek FROM
Studenci.RokUrodzenia to_date('1980','YYYY')
This is my script; what am I doing wrong?
CREATE TABLE Studenci(
NrIndeksu NUMBER(3) PRIMARY KEY,
Nazwisko VARCHAR2(16),
RokUrodzenia NUMBER(4),
Kierunek VARCHAR2(12)
);
CREATE TABLE Wykladowcy(
IdWykladowcy NUMBER(4) PRIMARY KEY,
Nazwisko VARCHAR2(16),
Stopien VARCHAR2(6),
Stanowisko VARCHAR(8)
);
CREATE TABLE Kursy(
IdKursu NUMBER(1) PRIMARY KEY,
Nazwa VARCHAR2(18),
IdWykladowcy NUMBER(4) REFERENCES Wykladowcy
);
CREATE TABLE Rejstracje(
NrIndeksu NUMBER(3) REFERENCES Studenci ,
IdKursu NUMBER(1) REFERENCES Kursy ,
Data DATE
);
CREATE UNIQUE INDEX Ind_Kursy_naz ON Kursy(Nazwa);
CREATE INDEX Ind_Studenci_naz ON Studenci(Nazwisko);
CREATE INDEX Ind_Wykladowcy_naz ON Wykladowcy(Nazwisko);
INSERT INTO Wykladowcy VALUES (1010,'Kowalski Jan', 'Dr', 'Adiunkt');
INSERT INTO Wykladowcy VALUES (1011,'Jakubowski Emil','Dr hab','Docent');
INSERT INTO Wykladowcy VALUES (1012,'Gazda Mirosław','Dr','Profesor');
INSERT INTO Kursy VALUES (1,'Bazy danych',1010);
INSERT INTO Kursy VALUES (2,'Systemy operacyjne',1012);
INSERT INTO Kursy VALUES (3,'Multimedia',1011);
INSERT INTO Kursy VALUES (4,'Sieci komputerowe',null);
INSERT INTO Studenci VALUES (101,'Kuczyńska Ewa',1980,'Bazy danych');
INSERT INTO Studenci VALUES (102,'Lubicz Robert',1985,'Multimedia');
INSERT INTO Studenci VALUES (103,'Krajewski Bogdan',1988,'Bazy danych');
INSERT INTO Studenci VALUES (104,'Lityńska Anna',1987,'Multimedia');
INSERT INTO Studenci VALUES (105,'Marzec Marcin',1982,'Multimedia');
INSERT INTO Studenci VALUES (106,'Cichaocki Rafał',1989,'Bazy danych');
INSERT INTO Rejstracje VALUES (101,1,NULL);
INSERT INTO Rejstracje VALUES (102,3,NULL);
INSERT INTO Rejstracje VALUES (104,3,NULL);
INSERT INTO Rejstracje VALUES (106,1,NULL);
INSERT INTO Rejstracje VALUES (104,2,NULL);
INSERT INTO Rejstracje VALUES (101,4,NULL);
INSERT INTO Rejstracje VALUES (103,1,NULL);
INSERT INTO Rejstracje VALUES (103,1,NULL);
INSERT INTO Rejstracje VALUES (105,1,NULL);
UPDATE Rejstracje SET Rejstracje.IdKursu=Rejstracje.IdKursu*3
WHERE Rejstracje.NrIndeksu=105;
COMMIT UPDATE
INSERT INTO Rejstracje(Data)
VALUES (Sysdate);
SELECT *FROM Kursy
WHERE Kursy.IdWykladowcy IS NULL;
SELECT *FROM Rejstracje
WHERE Rejstracje.NrIndeksu=101;
SELECT Kursy.Nazwa
FROM Kursy
ORDER BY Nazwa ASC;
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek
FROM Studenci.RokUrodzenia to_date('1980','YYYY')
From the error message it seems like a missing ;.
This SQL:
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek
FROM Studenci.RokUrodzenia to_date('1980','YYYY');
is invalid because the to_date() call can't go there like that.
You are probably looking for something like:
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek
FROM Studenci
WHERE RokUrodzenia = 1980;
given that RokUrodenzia is a NUMBER(4) column and not a DATE column. You may still have to do some work to get the MONTHS_BETWEEN() call to work since RokUrodenzia is not a DATE (as I said) and MONTHS_BETWEEN probably expects a date. You'll have to investigate how to fix that; you might use TO_DATE() there legitimately.
I have 3 tables. widgets, widget types, widget type ID. I have created direct relationship between them. How can I avoid bad data going into Widget tables based on Widget_type and Widget_sub_Type. Please see my code. There is just one small thing missing.
Create table widgets (
Widget_ID int not null primary key,
Widget_type_ID int not null,
Widget_Sub_type_ID int,
Widget_Name varchar (50)
)
Create table Widget_Type (
Widget_Type_ID int not null primary key,
)
Create table Widget_Sub_type (
Widget_Sub_Type_ID int not null primary key,
Widget_Type_ID int not null
)
---adding foregin key constraints
Alter table widgets
ADD constraint FK_Widget_Type
FOREIGN KEY (Widget_type_ID)
References Widget_Type (Widget_type_ID)
Alter table widgets
ADD constraint FK_Widget_Sub_Type
FOREIGN KEY (Widget_Sub_type_ID)
References Widget_SUB_Type (Widget_SUB_type_ID)
Alter table widget_Sub_Type
ADD Constraint FK_Widget_Type_Alter
Foreign key (widget_Type_ID)
References Widget_Type (Widget_Type_ID)
---- insert values
insert Widget_Type values (1)
insert Widget_Type values (5)
insert Widget_Sub_type values (3,1)
insert Widget_Sub_type values (4,1)
insert Widget_Sub_type values (7,5)
insert Widget_Sub_type values (9,5)
-- This will error out which is correct
insert Widget_Sub_type values (5,6)
select * from Widget_Sub_type
select * from Widget_type
--Good
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (1, 'TOY', 1, 3)
select * from widgets
--Good
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (2, 'BatMan', 5, 7)
-- How to prevenet this, 3 is not sub_type_id of type_ID 5. This is bad data, It should not be inserted.
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (3, 'Should Not', 5, 3)