i used the program SQL Fiddle and it keeps telling me that the table doesn't exist,what can i do to fix the two tables referencing each other? - sql

the Staff table references the branch table
CREATE TABLE Staff(
StaffNo VARCHAR(5) NOT NULL,
firstName VARCHAR(15) NOT NULL UNIQUE,
lastName VARCHAR(15) NOT NULL,
position VARCHAR(10) NOT NULL,
salary INTEGER
DEFAULT 3000,
CHECK (salary BETWEEN 3000 AND 25000),
email VARCHAR(25),
branchNo CHAR(6) NOT NULL,
PRIMARY KEY (StaffNo),
FOREIGN KEY (branchNo) REFERENCES Branch (branchNo));
and at the same time the branch table references the Staff table
create table Branch(
branchNo char(6) not null primary key,
street varchar(30) not null,
city varchar(20),
postCode char(5) not null,
ManagerNo varchar(5) not null,
foreign key (ManagerNo) references Staff(StaffNo));

Since your tables reference each other in the Foreign Keys you will get an error on either table creation if the other table has not been created yet. I would suggest that you remove the creation of the FOREIGN KEYs to separate ALTER TABLE statements:
CREATE TABLE Staff(
StaffNo VARCHAR(5) NOT NULL,
firstName VARCHAR(15) NOT NULL UNIQUE,
lastName VARCHAR(15) NOT NULL,
position VARCHAR(10) NOT NULL,
salary INTEGER
DEFAULT 3000,
CHECK (salary BETWEEN 3000 AND 25000),
email VARCHAR(25),
branchNo CHAR(6) NOT NULL,
PRIMARY KEY (StaffNo)
);
create table Branch(
branchNo char(6) not null primary key,
street varchar(30) not null,
city varchar(20),
postCode char(5) not null,
ManagerNo varchar(5) not null
);
alter table staff
add constraint fk1_branchNo foreign key (branchNo) references Branch (branchNo);
alter table branch
add constraint fk1_ManagerNo foreign key (ManagerNo) references Staff (StaffNo);
See SQL Fiddle with Demo

You can remove one reference from one table and keep the other.then you can retrieve data using the remainig reference.Is there any problem with that?

Related

SQL, Delete Records Based On Related Fields In Other Table

I have a SQL table based on hotel data. I have two tables and a bridge table to relate them. I'm still learning so I'm sure some of this is not ideal or has potential risks.
Guest Table
CREATE TABLE Guest
(
Guest_ID INT PRIMARY KEY IDENTITY (1, 1),
GuestName NVARCHAR(60) NOT NULL,
Street NVARCHAR(50) NOT NULL,
City NCHAR(30) NOT NULL,
[State] CHAR(2) NOT NULL,
CONSTRAINT [State.State]
FOREIGN KEY ([State]) REFERENCES [State]([State]),
Zip CHAR(5) NOT NULL,
Phone VARCHAR(15) NOT NULL
);
Room Table
CREATE TABLE Room
(
Room_ID SMALLINT PRIMARY KEY,
Room_Type_ID SMALLINT NOT NULL,
CONSTRAINT Room_Type_ID
FOREIGN KEY (Room_Type_ID) REFERENCES Room_Type([Type_ID]),
Amenity_Type_ID SMALLINT NOT NULL,
CONSTRAINT Amenity_Type_ID
FOREIGN KEY (Amenity_Type_ID) REFERENCES Amenity_Type([Type_ID])
);
Bridge Table (Reservations)
CREATE TABLE Guest_Bridge_Rooms
(
Guest_ID INT NOT NULL,
CONSTRAINT Guest_ID
FOREIGN KEY (Guest_ID) REFERENCES Guest(Guest_ID),
Room_ID SMALLINT NOT NULL,
CONSTRAINT Room_ID
FOREIGN KEY (Room_ID) REFERENCES Room(Room_ID),
Date_Start DATE NOT NULL,
Date_End DATE NOT NULL,
Occ_Adults SMALLINT NOT NULL,
Occ_Children SMALLINT NOT NULL,
Price_Total DECIMAL(13,2) NOT NULL
);
Now with these tables, I would like to write a script to DELETE all rows where a reservation (bridged table) has a specific guest NAME by somehow relating the given Guest_ID to its GuestName in the related table. I could simply use Guest_ID but that is not the goal here.
For example something like
DELETE FROM Guest_Bridge_Rooms
WHERE Guest[ID].GuestName = 'John Doe';
Is there a simple way to do this?
You can use a subquery:
DELETE FROM Guest_Bridge_Rooms
WHERE Guest_ID = (SELECT g.Guest_Id FROM Guests g WHERE g.GuestName = 'John Doe');
Note: The exact syntax might vary, depending on the database. This also assumes that GuestName is unique in Guests.

SQL Missing left/right parenthesis

I keep getting a missing right or left parenthesis error when I try to create my tables with a foreign key, but when I remove the foreign key my code has no problem creating the tables. I need the code to run with the foreign keys.
--deletes respective tables previously created
drop table DEPARTMENT;
drop table POSITION;
drop table EMPLOYEE;
drop table COMPANY;
drop table DEGREE;
drop table GRAD_INFO;
drop table STUDENT;
CREATE TABLE Department
(
Dept_Name varchar(255) PRIMARY KEY, --primary key
Dept_Phone_Num numeric(35) not null
);
CREATE TABLE Position
(
Position_Name varchar(255) NOT NULL PRIMARY KEY, --primary key
Position_Salary decimal(10, 2) not null,
Employee_num numeric(35) not null,
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department (Dept_Name) --foreign key
);
CREATE TABLE Employee
(
EmployeeID INT NOT NULL PRIMARY KEY, --primary key
Employee_Salary DECIMAL(10,2) DEFAULT NULL,
Employee_FName VARCHAR(30) DEFAULT NULL,
Employee_LName VARCHAR(30) NOT NULL,
Employee_Address VARCHAR(30) DEFAULT NULL,
Employee_Phone_Num INT(10) DEFAULT NULL,
Employee_Email VARCHAR(30) NOT NULL,
Employee_Status VARCHAR(10) NOT NULL,
CONSTRAINT Position_Name
FOREIGN KEY Position_Name REFERENCES Position(Position_Name), --foreign key
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department(Dept_Name) --foreign key
);
CREATE TABLE Company
(
Comp_Name VARCHAR(20) NOT NULL PRIMARY KEY, --primary key
Position_Offer VARCHAR(30) NOT NULL,
Salary_Offer DECIMAL(10,2) NOT NULL,
Signing_Bonus DECIMAL(10,2) DEFAULT NULL,
Comp_Phone_Num INT(10) NOT NULL,
Comp_Address VARCHAR(30) NOT NULL,
CONSTRAINT fk_Employee_ID
FOREIGN KEY Employee_ID REFERENCES Employee (Employee_ID), --foreign key
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department (Dept_Name) --foreign key
);
CREATE TABLE Degree
(
Degree_Type varchar(255) PRIMARY KEY, --primary key
Academic_Status varchar(255) not null,
Academic_Level varchar(255) not null,
Major varchar(255) not null,
Minor varchar(255) default null
);
CREATE TABLE Gead_Info
(
Grad_Status VARCHAR(255) NOT NULL PRIMARY KEY, --primary key
College_Name VARCHAR(255) NOT NULL,
Num_Of_Degrees NUMERIC(10) NOT NULL,
Grad_Date NUMERIC(6) NOT NULL,
CONSTRAINT fk_Degree_Type
FOREIGN KEY Degree_Type REFERENCES Degree(Degree_Type) --foreign key
);
CREATE TABLE Student
(
Student_ID NUMERIC(10) NOT NULL PRIMARY KEY, --primary key
Student_FName VARCHAR(20) NOT NULL,
Student_LName VARCHAR(20) NOT NULL,
Student_Address VARCHAR(20) NOT NULL,
Student_Email VARCHAR(20) NOT NULL,
Student_Phone_Num VARCHAR(10) NOT NULL,
CONSTRAINT fk_Comp_Name
FOREIGN KEY Comp_Name REFERENCES Company(Comp_Name), --foreign key
CONSTRAINT fk_Grad_Status
FOREIGN KEY Grad_Status REFERENCES Gead_Info(Grad_Status) --foreign key
);
The degree table is the only table where I don't get an error.
Should I use an alter table instead after I create all the tables or is there another way to solve my problem?
edit: updated code
I think your most immediate issue is with the Department table not having a comma between the two CONSTRAINT clauses. You might also run into issues with your Position table's CONSTRAINT clause referencing the Position table instead of the Employee table, although honestly I don't think you need that relationship defined at all since the Employee table already has a foreign key constraint with Position. And like what was stated earlier, your table definitions aren't in the right order so you have some tables referencing other tables before those others are created.
Give this a shot to see if the syntax works. I've added the column definition for the foreign key in Position, put parentheses around the foreign key column name in the CONSTRAINT clause, and removed the space between Department and the left paren in the CONSTRAINT clause. If this script works, make the same changes to your other table defs.
CREATE TABLE Department(
Dept_Name varchar(255) Primary Key, --primary key
Dept_Phone_Num numeric(35) not null
);
CREATE TABLE Position(
Position_Name varchar(255) not null Primary Key, --primary key
Position_Salary decimal(10, 2) not null,
Employee_num numeric(35) not null,
Dept_Name varchar(255) not null,
CONSTRAINT fk_Dept_Name FOREIGN KEY (Dept_Name) REFERENCES Department(Dept_Name) --foreign key
);

Error ORA-00904 in SQL developer. Seemly no fix online that works that I can find while creating a table

I have seen many posts regarding the seemingly infamous ORA-00904 yet nothing seems to be helping. I am creating 2 tables (for now, more to come later) which are as follows;
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
and
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
but every time I run it, I get error;
Error starting at line : 4 in command -
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Error report -
ORA-00904: "CUSTOMERID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
ANY HELP WOULD BE GREATLY APPRECIATED!
Oracle by default converts all names to UpperCase if you don't enquote them. (Edit: At the time of this answer the Foreign Key Constraint in the starting post did contain a quoted column name).
So, your constraint tries to find a column named "customerID" whereas the real name of your column is "CUSTOMERID".
Either enquote your column:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
"customerID" INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("customerID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Don't use quotes on your FOREIGN KEY constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Or use quotes and write it uppercase in your foreign key constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("CUSTOMERID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
As already mentioned, keeping the style consistent is recommended to prevent future problems, so Option 2 would be the easiest solution. Otherwise you need to enquote all columns to be consistent.
/Edit:
I just tried it - this definitely works with Oracle 12C:
CREATE TABLE Customer (
CustomerID INT,
PRIMARY KEY (CustomerID)
)
Followed by:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
The referenced table needs to created before the foreign key reference. One method is to create the tables first and then add the foreign key references:
CREATE TABLE Employee (
EmployeeID int NOT NULL,
PRIMARY KEY (EmployeeID),
customerID INT NOT NULL,
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
CREATE TABLE Customer (
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
-- FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
alter table Employee add CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer (CustomerID)
Here is a db<>fiddle.
Note that I commented out the foreign key constraint to Project.
Also, in most data models you can just order the table creates -- there are no cycles in the foreign key "linkages". However, because you have not defined Project, I am suggesting this more general approach.

SQL Error: ORA-02267 column type incompatible with referenced column type

Source code for oracle 12c
CREATE TABLE CUSTOMER (
CustomerID Char(20) NOT NULL,
CustomerFirstName Char(20) NOT NULL,
CustomerLastName Char(25) NOT NULL,
CustomerAddress Char(45) NOT NULL,
CustomerEmail Char(100) NOT NULL,
CONSTRAINT Customer_PK Primary Key(CustomerID)
CREATE TABLE EMPLOYEE (
EmployeeID VarChar(10) NOT NULL,
EmployeeFName VarChar(20) NOT NULL,
EmployeeRole VarChar(30) NOT NULL, CONSTRAINT
Employee_PK Primary Key(EmployeeID)
CREATE TABLE PRODUCT (
ProductID VarChar(10) NOT NULL,
ProductName VarChar(20) NOT NULL,
ProductType VarChar(20) NOT NULL,
ProductPrice Number(8,2) NOT NULL,
CONSTRAINT Product_PK Primary Key(ProductID)
CREATE TABLE CUSTORDER (
CustOrderID VarChar(10) NOT NULL,
CustOrderDate Date NOT NULL,
CustShippingStatus VarChar(20) NOT NULL,
SupplierID VarChar(20) NOT NULL,
CONSTRAINT CustOrder_PK Primary Key (CustOrderID),
CONSTRAINT CustOrder_FK FOREIGN KEY(SupplierID)
REFERENCES SUPPLIER(SupplierID)
CREATE TABLE SUPPLIER ( SupplierID Char(10) NOT NULL,
SupplierName Char(20) NOT NULL,
SupplierAddress Char(45) NOT NULL,
CONSTRAINT Supplier_PK Primary Key (SupplierID)
CREATE TABLE INVOICE (
InvoiceID Char(20) NOT NULL,
TotalItems Int NOT NULL,
TotalCost Number(8,2) NOT NULL,
SalesDate Date NOT NULL,
PaymentType VarChar(10) NOT NULL,
ProductID VarChar(10) NOT NULL,
EmployeeID VarChar(10) NOT NULL,
CustomerID Char(20) NOT NULL,
SupplierID Char(10) NOT NULL,
CONSTRAINT Invoice_PK Primary Key(InvoiceID),
CONSTRAINT Invoice_Product_FK Foreign Key(ProductID)
REFERENCES PRODUCT(ProductID),
CONSTRAINT Invoice_Employee_FK Foreign Key(EmployeeID)
REFERENCES EMPLOYEE(EmployeeID),
CONSTRAINT Invoice_Customer_FK Foreign Key(CustomerID)
REFERENCES CUSTOMER(CustomerID),
CONSTRAINT Invoice_Supplier_FK Foreign Key(SupplierID)
REFERENCES SUPPLIER(SupplierID)
You need to align the datatype and length of the referencing column of each foreign key with the column it references in the source table.
In your code, SUPPLIER(SupplierID) is declared as Char(20). On the other hand, referencing column INVOICE(SupplierID) is Char(10) and CUSTORDER(SupplierID) is VarChar(20). If you make these two columns Char(20), you code just works.
Aside: the referenced table must exist at the time when the referencing table is created - your code createst CUSTORDER before SUPPLIER. However I assume that's a typo when writing the question, otherwise you would be getting a different error: ORA-00942: table or view does not exist.
You also have a missing right parentheses at the end of each and every create table statement: I would consider these typos too.
Demo on DB Fiddle

how to avoid empty entries in sql?

CREATE TABLE emp3(
id int(3) auto_increment,
first_name varchar(30) NOT NULL,
last_name varchar(30),
email varchar(20) not null unique,
PRIMARY KEY (id)
)
This is my table.
if i insert query like this insert into emp3 values(3,' ','',''); it is getting stored in the table.
but i should avoid this.How can i do it?
You can add constraints according to your needs:
CREATE TABLE emp3(
id int(3) auto_increment,
first_name varchar(30) NOT NULL,
last_name varchar(30),
email varchar(20) not null unique,
PRIMARY KEY (id)
);
ALTER TABLE emp3
ADD CONSTRAINT check_name_not_blank CHECK ((first_name<>'')),
ADD CONSTRAINT check_email_not_blank CHECK ((email<>''))
you yould avoid empty strings in your columns with a constraint like
ALTER TABLE emp3 WITH CHECK
ADD CONSTRAINT [CK_emp3] CHECK
(
([first_name]<>'') AND
([last_name]<>'') AND
([email ]<>'')
)
you can do this just by setting them as Not Null
CREATE TABLE emp3(
id int(3) auto_increment,
first_name varchar(30) NOT NULL,
last_name varchar(30),
email varchar(20) not null unique,
PRIMARY KEY (id)
)