InterBase: How to create view with a multible tables? - sql

I am using SQL statement to create a view, but all the time I have problems with syntax.
The four tables I have are:
CREATE TABLE "BOOK_ORDER"
(
"ID" INTEGER NOT NULL,
"Open_Date" DATE NOT NULL,
"Close_Date" DATE,
"Student_ID" INTEGER NOT NULL,
"Book_ID" INTEGER NOT NULL,
CONSTRAINT "PK_ORDER_ID" PRIMARY KEY ("ID")
);
ALTER TABLE "BOOK_ORDER" ADD CONSTRAINT "FK_ORDER_BOOK" FOREIGN KEY ("Book_ID") REFERENCES BOOK ("ID") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE "BOOK_ORDER" ADD CONSTRAINT "FK_ORDER_STUDENT" FOREIGN KEY ("Student_ID") REFERENCES STUDENTS ("STUDENT_ID") ON UPDATE CASCADE ON DELETE CASCADE;
CREATE TABLE "STUDENTS"
(
"STUDENT_ID" INTEGER NOT NULL,
"STUDENT_NAME" VARCHAR(50) NOT NULL,
"TICKET" INTEGER NOT NULL,
"GROUP_ID" INTEGER NOT NULL,
CONSTRAINT "PK_StUDENTS" PRIMARY KEY ("STUDENT_ID")
);
ALTER TABLE "STUDENTS" ADD CONSTRAINT "FK_GROUP" FOREIGN KEY ("GROUP_ID") REFERENCES GROUPS ("GROUP_ID") ON UPDATE CASCADE ON DELETE CASCADE;
CREATE TABLE "GROUPS"
(
"GROUP_ID" INTEGER NOT NULL,
"TITLE" VARCHAR(50) NOT NULL,
"CURATOR" VARCHAR(50) NOT NULL,
"CURATOR_TEL" VARCHAR(20) NOT NULL,
"FAC_ID" INTEGER NOT NULL,
CONSTRAINT "PK_GROUP" PRIMARY KEY ("GROUP_ID")
);
CREATE TABLE "BOOK"
(
"ID" INTEGER NOT NULL,
"CODE" INTEGER NOT NULL,
"Name" VARCHAR(50) NOT NULL,
"Year" DATE NOT NULL,
"Publishing_Office" VARCHAR(50) NOT NULL,
"Language" INTEGER NOT NULL,
CONSTRAINT "PK_BOOK_ID" PRIMARY KEY ("ID")
);
I need to show info about order: OpenDate, CloseDate, Student, StudentGroup, Book.

You create a view with the syntax of CREATE VIEW view_name AS SELECT statement where statement is your regular SELECT statement.
Therefore, you would do this:
CREATE VIEW view_name AS
SELECT BOOK_ORDER.id, BOOK_ORDER.OpenDate, BOOK_ORDER.CloseDate, students.student_name, groups.title, book.name
FROM book_order, students, groups, book
WHERE book_order.student_id = students.student_id AND
book_order.book_id = book.id AND
students.group_id = groups.group_id
The where clause is performing a simple join by including values repeated across tables once. I cannot guarantee this will work, but it is a good start.
Also, to get data from the view, select from it like a regular table with the view as the table name.

Related

No primary or candidate keys in the referenced table?

I keep getting an error:
There are no primary or candidate keys in the referenced table 'TProducts' that match the referencing column list in the foreign key 'TProducts_TCategories_FK'."
When trying to establish a relationship between my products table and categories table. I have checked the spelling multiple times but can't seem to figure out why I keep getting this error. The rest of my tables are fine, and I used the same method.
CREATE TABLE TCategories
(
intCategoryID INTEGER NOT NULL,
strCategoryName VARCHAR(255) NOT NULL,
CONSTRAINT TCategories_PK PRIMARY KEY (intCategoryID)
)
CREATE TABLE TProducts
(
intProductID INTEGER NOT NULL,
intVendorID INTEGER NOT NULL,
intCategoryID INTEGER NOT NULL,
strProductName VARCHAR(255) NOT NULL,
monCostofProduct MONEY NOT NULL,
monRetailCost MONEY NOT NULL,
intInventory INTEGER NOT NULL,
CONSTRAINT TProducts_PK PRIMARY KEY (intProductID)
)
ALTER TABLE TProducts
ADD CONSTRAINT TProducts_TCategories_FK
FOREIGN KEY (intCategoryID) REFERENCES TProducts (intCategoryID)
You are referencing to TProducts table and not TCategories table:
ALTER TABLE TProducts ADD CONSTRAINT TProducts_TCategories_FK
FOREIGN KEY ( intCategoryID ) REFERENCES TCategories( intCategoryID )

Oracle (ORA-02270) : no matching unique or primary key for that column list

I have two tables:
CREATE TABLE Trasy_srodki_transportu(
ID_Trasy Integer NOT NULL,
ID_pojazdu Integer NOT NULL
)
/
CREATE TABLE Trasy(
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Now i want to add foreign key to Trasy_srodki_transportu referencing to Trasy table:
ALTER TABLE Trasy_srodki_transportu ADD CONSTRAINT Trasa_jest_wykorzystywana FOREIGN KEY (ID_Trasy) REFERENCES Trasy (ID_Trasy)
/
and this throws Oracle (ORA-02270) : no matching unique or primary key for this column-list error. Any suggestions how to fix this?Data modeler view
A foreign key needs to reference a key on the related table, but it's not the case in your example. Change the definition of the second table by adding a PRIMARY KEY constraint in it, as in:
CREATE TABLE Trasy (
ID_Trasy Integer PRIMARY KEY NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Alternatively, you can create a unique constraint on it, that can also serve as a key. For example:
CREATE TABLE Trasy (
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL,
CONSTRAINT uq_idtrasy UNIQUE (ID_Trasy)
)

Oracle: Constraints of keys

I have an entity within Oracle's SQL:
and I'm wondering, what does "UF" represent? I've written the SQL code but I do not know how to represent the "UF" attribute as a constraint.
CREATE TABLE entry (
entryno NUMBER(4) NOT NULL,
carndate DATE NOT NULL,
entrystarttime DATE NOT NULL,
entryfinishtime DATE NOT NULL,
entryplace NUMBER(4) NOT NULL,
charname VARCHAR2(30) NOT NULL,
compno NUMBER(4) NOT NULL,
eventypecode CHAR(3) NOT NULL,
teamname VARCHAR2(30) NOT NULL
);
ALTER TABLE entry ADD CONSTRAINT entry_pk PRIMARY KEY ( entryno,
carndate );
ALTER TABLE entry
ADD CONSTRAINT entry_charity_fk FOREIGN KEY ( charname )
REFERENCES charity ( charname );
ALTER TABLE entry
ADD CONSTRAINT entry_carnival_fk FOREIGN KEY ( carndate )
REFERENCES carnival ( carndate );
ALTER TABLE entry #
ADD CONSTRAINT entry_competitor_fk FOREIGN KEY ( compno )
REFERENCES competitor ( compno );
ALTER TABLE entry #
ADD CONSTRAINT entry_event_fk FOREIGN KEY ( carndate, eventypecode )
REFERENCES evenet ( carndate, eventypecode );
ALTER TABLE entry
ADD CONSTRAINT entry_team_fk FOREIGN KEY ( teamname, carndate )
REFERENCES team ( teamname,carndate );
ALTER TABLE entry
ADD CONSTRAINT ... ("UF")
The UF is actually two flags.
As you've recognised the F means the column is a foreign key. The U stands for Unique. Those columns form part of the compound unique key:
ALTER TABLE entry ADD CONSTRAINT entry_carnival_un UNIQUE
( carndate, compno, eventypecode );
carndate is also part of the compound primary key so really it should be flagged PFU but I guess the modelling tool didn't allow for three flags.

Creating Table (Oracle)

I am creating two tables. The first table creates with no errors, but when I try to create the SUBHEAD table, I get an error: Line 2, Missing right parenthesis. I am not sure what is wrong with this line. Below is my SQL statements:
CREATE TABLE HEAD
(Code NUMERIC(4,0) NOT NULL PRIMARY KEY,
HeadName VARCHAR(50) NOT NULL UNIQUE,
HType VARCHAR(1) NOT NULL,
HDate DATE NOT NULL,
OpBal DECIMAL(11,2) NOT NULL
);
CREATE TABLE SUBHEAD
(HCode NUMERIC(4,0) NOT NULL FOREIGN KEY REFERENCES HEAD(Code),
SubCode NUMERIC(4,0) NOT NULL,
SubName VARCHAR(50) NOT NULL,
SDate DATE NOT NULL,
OpBal DECIMAL (11,2) NOT NULL,
CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
);
syntax: http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#SQLRF52167
note that "in-line" constraint syntax is : "col col_type REFERENCES TAB(COL)" and not "FOREIGN KEY"
The data type for number is NUMBER not NUMERIC
So my memory did not fool me - you can have multiple in-line constraints (although the syntax graph doesn't show it):
SQL> create table tt1(a number primary key);
Table created.
SQL> create table tt2(a number references tt1(a) not null);
Table created.
CREATE TABLE SUBHEAD
(HCode NUMERIC(4,0) NOT NULL, FOREIGN KEY (Hcode) REFERENCES HEAD(Code),
SubCode NUMERIC(4,0) NOT NULL,
SubName VARCHAR(50) NOT NULL,
SDate DATE NOT NULL,
OpBal DECIMAL (11,2) NOT NULL,
CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
);
(note comma, and reference to the new table column)

Error: Could not create constraint SQL Server 2008

I am creating 6 tables. When creating the first 3 tables, everything works well. But then when I have to use a Foreign Key that I have used previously when creating another table, the system presents an error.
These are the 3 first tables:
CREATE TABLE Employee (
EmployeeID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Position TEXT NOT NULL,
Salary REAL NOT NULL,
Remarks TEXT
);
CREATE TABLE Planet (
PlanetID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Coordinates REAL NOT NULL
);
CREATE TABLE Has_Clearance (
Employee INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID),
Level INTEGER NOT NULL,
PRIMARY KEY(Employee, Planet)
);
Then I create the 4th table:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);
And I get the following error:
"There is already an object named 'fk_Employee_EmployeeID' in the database. Could not create constraint."
Please let me know how to create the FK in this 4th table.
The constraint "fk_Employee_EmployeeID" is first created on table "Has_Clearance" and then you try to create another constraint with the same name - which is not allowed.
Just rename the constraint in the 4-th table like this:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL CONSTRAINT fk_Maneger_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);