I try to create three tables by using website suport compiler any code but I have a problem in the table of the bill.
When I run it I get to error show me it is near in foreign key
These are codes of three tables
CREATE TABLE patient (
Patient Id (5) Primary key,
Name Varchar (20) Not null ,
Age Int Not null ,
Weight Int Not null ,
Gender Varchar (10) Not null,
Address Varchar (50) Not null ,
Disease Varchar (20) Not null
);
CREATE TABLE doctors (
DoctorId Varchar (5) Primary key,
Doctorname Varchar (15) Not null,
Dept Varchar (15) Not null
);
CREATE TABLE bill (
Bill_no Varchar (50) Primary key,
Patient_Id Varchar (5) Foreign key,,
doctor_charge Int Not null,
patient_type Varchar (10) null,
no_of_days Int null,
lab_charge Int null,
bill Int Not null
);
Patient Table
CREATE TABLE patient
(
patient_id VARCHAR (5) PRIMARY KEY,
name VARCHAR (20) NOT NULL,
age INT NOT NULL,
weight INT NOT NULL,
gender VARCHAR (10) NOT NULL,
address VARCHAR (50) NOT NULL,
disease VARCHAR (20) NOT NULL
);
Errors
No data type has been assigned in Patient id column (Patient Id (5)
Primary key)
Patient id column name contains spaces. You need to
enclose the column name in double quotes or replace space with something else
(ex: _). It's not recommended to use spaces.
A column name with space
CREATE TABLE tablename ("column name" datatype);
Doctors Table
CREATE TABLE doctors
(
doctorid VARCHAR (5) PRIMARY KEY,
doctorname VARCHAR (15) NOT NULL,
dept VARCHAR (15) NOT NULL
);
Bill Table
CREATE TABLE bill
(
bill_no VARCHAR (50) PRIMARY KEY,
patient_id VARCHAR (5),
doctor_charge INT NOT NULL,
patient_type VARCHAR (10) NULL,
no_of_days INT NULL,
lab_charge INT NULL,
bill INT NOT NULL,
FOREIGN KEY (patient_id) REFERENCES patient(patient_id)
);
Errors
The way you have assigned foreign key is wrong. Please refer this and this article for more information. (Patient_Id Varchar (5) Foreign key,,)
There are two commans in the Patient_Id column (Patient_Id Varchar (5) Foreign key,,)
You have to provide reference of the table for which you want to use the reference key.
For example, you have table Persons which has Primary key PersonID, in that case if you want to use that as foreign key in another table, lets say Orders.
In Oracle
CREATE TABLE Orders (
OrderID numeric(10) not null,
OrderNumber numeric(10) not null,
PersonID numeric(10) not null,
CONSTRAINT fk_person_id
FOREIGN KEY (PersonID )
REFERENCES Persons(PersonID )
Your Case :
CREATE TABLE bill
( Bill_no Varchar (50) Primary key,
Patient_Id Varchar (5),
doctor_charge Int Not null,
patient_type Varchar (10) null,
no_of_days Int null,
lab_charge Int null,
bill Int Not null,
CONSTRAINT fk_patient_id
FOREIGN KEY (Patient_Id)
REFERENCES patient(Patient_Id)
);
Remove the 'Foreign Key' from the table creation script.
Add this to your SQL script:
ALTER TABLE [Bill] WITH CHECK ADD CONSTRAINT [FK_Bill_Patient] FOREIGN KEY([Patient_Id])
REFERENCES [Patient] ([Patient_Id])
GO
ALTER TABLE [Bill] CHECK CONSTRAINT [FK_Bill_Patient]
GO
The words FOREIGN KEY are only needed for introducing the name of the FK constraint. Since your other constraints are not named, you might as well skip that part and go straight to REFERENCES.
If you specify a foreign key constraint as part of the column definition, you can omit the datatype to allow it to inherit from its parent at the time of creation, which I think is good practice as the types will automatically match.
We use VARCHAR2 in Oracle, not VARCHAR.
You don't need to specify NULL for columns that are allowed to be null.
I am not sure a 5-character string is a good datatype for a unique ID. How will you generate the values? Normally an auto-incrementing sequence number simplifies this.
create table doctors
( doctorid varchar2(5) primary key
, doctorname varchar2(15) not null
, dept varchar2(15) not null );
create table patients
( patient_id varchar2(5) primary key
, name varchar2(20) not null
, age integer not null
, weight integer not null
, gender varchar2(10) not null
, address varchar2(50) not null
, disease varchar2(20) not null );
create table bills
( bill_no varchar2(50) primary key
, patient_id references patients -- Allow datatype to inherit from parent
, patient_type varchar2(10)
, no_of_days integer
, lab_charge integer
, bill integer not null );
Related
I have homework to create tables and insert data into it and when i insert data into Branch table it gives me a unique constraint error and I don’t know why, and how can I solve this problem. my codes:
create table Branch(
Branchno varchar (4) primary key not null,
Street varchar (15) not null,
City varchar (10) not null,
Postcode varchar (10) not null)
create table staff (
Staffno varchar (4),
Fname varchar2 (50) not null,
Lname varchar (50),
Position varchar2 (50),
Sex varchar (1),
Dob date,
Salary number (30),
Branchno varchar (4),
constraint pk_staff primary key (staffno),
constraint fk_staff_branchno foreign key (branchno)
references Branch (branchno)
)
create table Client
(Clientno varchar (4) primary key not null,
Fname varchar (50) not null,
Lname varchar (50) not null,
Telno varchar (50) not null,
Preftype varchar (18) not null,
Maxrent INT)
create table PropertyForRent(
Propertyno varchar (40) not null,
Street varchar (40),
City varchar (40),
Postcode varchar (40),
Type varchar (40),
Rooms number (30),
Rent number (30),
Ownerno varchar (40),
Staffno varchar (40),
Branchno varchar (40),
constraint pk_PropertyForRent primary key (Propertyno),
foreign key (staffno)
references staff (staffno),
foreign key (branchno)
references branch (branchno)
)
create table Registration
(clientno varchar (60) not null,
branchno varchar (50) not null,
staffno varchar (50) not null,
datejoined date not null,
foreign key (clientno) references client (clientno),
foreign key (branchno) references branch (branchno),
foreign key (staffno) references staff (staffno)
)
The data that I insert to give me this error:
INSERT INTO Branch values
('B005','22 Deer Rd','London','SW1 4EH');
INSERT INTO Branch values
('B007','16 Argy11 St','Aberdeen','AB2 3SU');
I try with Oracle 11g and not receive any error:
SQL> INSERT INTO Branch values
('B005','22 Deer Rd','London','SW1 4EH'); 2
1 row created.
SQL> INSERT INTO Branch values
('B007','16 Argy11 St','Aberdeen','AB2 3SU'); 2
1 row created.
SQL> commit;
Commit complete.
Probably have you already record with these keys or have you any trigger on this table.
Instead of an INSERT INTO
you could
MERGE INTO Branch dest
USING( SELECT 'B005' Branchno,'22 Deer Rd' Street,'London' City,'SW1 4EH' Postcode FROM dual) src
ON( dest.Branchno = src.Branchno )
WHEN NOT MATCHED THEN
INSERT( Branchno, Street, City,Postcode )
VALUES ( src.Branchno, src.Street, src.City,src.Postcode );
That looks on the first sight, more difficult, but you would not get an error and all stops.
trggers can't stop you from inserting doulbe entires as they you can only can raise errors with wuld stop again every multi insert
see example
CREATE TABLE Performances (
DT Datetime NOT NULL,
Name varchar(20) NOT NULL,
Start_Time time NULL,
Min_price integer NOT NULL,
Max_price integer NOT NULL,
Location Varchar (20) NOT NULL,
PRIMARY KEY (DT, Name),
create Table [TYPE OF SHOWS] (
Name Varchar (20) NOT NULL ,
Type_of_show Varchar (20),
PRIMARY KEY (Name),
CONSTRAINT fk_PerformanceName FOREIGN KEY (Name)
REFERENCES Performances (Name),
)
I'm unable to set the foreign key only to Name, how can I implemnt this?
I suspect that you actually want the relation the other way around, with Performances referencing Type of Shows, like:
create Table [TYPE OF SHOWS] (
Name Varchar (20) NOT NULL ,
Type_of_show Varchar (20),
PRIMARY KEY (Name)
);
CREATE TABLE Performances (
DT Datetime NOT NULL,
Name varchar(20) NOT NULL,
Start_Time time NULL,
Min_price integer NOT NULL,
Max_price integer NOT NULL,
Location Varchar (20) NOT NULL,
PRIMARY KEY (DT, Name),
CONSTRAINT fk_PerformanceName FOREIGN KEY (Name) REFERENCES [TYPE OF SHOWS](Name)
);
I am getting ORA-00955 error which is name is already used by an existing object. I know it is in the foreign key constraint im trying to use. It happens in both foreign key constraints. I cant figure out why its happening. I tried renaming them to anything possible and I cant understand why it wont work.
DROP TABLE STUDENT;
CREATE TABLE STUDENT
(stuID VARCHAR (7) PRIMARY KEY NOT NULL,
major VARCHAR (15) NOT NULL,
firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
yr VARCHAR(8) NOT NULL
);
DROP TABLE ENROLL;
CREATE TABLE ENROLL
(stuID VARCHAR (7) NOT NULL,
grade VARCHAR (1) NOT NULL,
courseID VARCHAR(7) NOT NULL,
CONSTRAINT fk_STUDENT
FOREIGN KEY (stuID)
REFERENCES STUDENT(stuid)
);
DROP TABLE CLASS;
CREATE TABLE CLASS
(instructorID VARCHAR(7) NOT NULL,
classRoomNumber VARCHAR(7) NOT NULL,
dateStarts DATE NOT NULL,
courseID VARCHAR(7) PRIMARY KEY NOT NULL,
description CHAR(100) NOT NULL,
CONSTRAINT fk_INSTRUCTOR
FOREIGN KEY (instructorID)
REFERENCES INSTRUCTOR(instructorid)
);
DROP TABLE INSTRUCTOR;
CREATE TABLE INSTRUCTOR
(firstName VARCHAR (15) NOT NULL,
lastName VARCHAR (15) NOT NULL,
departmentName VARCHAR (15) NOT NULL,
instructorID VARCHAR(7) PRIMARY KEY NOT NULL
);
In a comment, you said that there's still an error.
If you create (and/or drop) tables in a correct order, everything is OK. I prefer dropping them separately.
At first, tables don't exist so I'll just create them:
SQL> CREATE TABLE student (
2 stuid VARCHAR(7) PRIMARY KEY NOT NULL,
3 major VARCHAR(15) NOT NULL,
4 firstname VARCHAR(15) NOT NULL,
5 lastname VARCHAR(15) NOT NULL,
6 yr VARCHAR(8) NOT NULL
7 );
Table created.
SQL> CREATE TABLE enroll (
2 stuid VARCHAR(7) NOT NULL,
3 grade VARCHAR(1) NOT NULL,
4 courseid VARCHAR(7) NOT NULL,
5 CONSTRAINT fk_student FOREIGN KEY ( stuid )
6 REFERENCES student ( stuid )
7 );
Table created.
SQL> CREATE TABLE instructor (
2 firstname VARCHAR(15) NOT NULL,
3 lastname VARCHAR(15) NOT NULL,
4 departmentname VARCHAR(15) NOT NULL,
5 instructorid VARCHAR(7) PRIMARY KEY NOT NULL
6 );
Table created.
SQL> CREATE TABLE class (
2 instructorid VARCHAR(7) NOT NULL,
3 classroomnumber VARCHAR(7) NOT NULL,
4 datestarts DATE NOT NULL,
5 courseid VARCHAR(7) PRIMARY KEY NOT NULL,
6 description CHAR(100) NOT NULL,
7 CONSTRAINT fk_instructor FOREIGN KEY ( instructorid )
8 REFERENCES instructor ( instructorid )
9 );
Table created.
Drop tables in reverse order, so that detail is dropped before its master
SQL> DROP TABLE enroll;
Table dropped.
SQL> DROP TABLE student;
Table dropped.
SQL> DROP TABLE class;
Table dropped.
SQL> DROP TABLE instructor;
Table dropped.
SQL>
Table INSTRUCTOR is referenced by table CLASS, hence you heed to create table INSTRUCTOR before you create table CLASS.
Also, you should use DROP TABLE ... CASCADE CONSTRAINTS instead of just DROP TABLE .... This allows foreign and primary keys to be properly dropped at the same time as the table, and might avoid error name already used by existing object that you are currently getting.
Create table Hotel (
Hotel_no varchar (10) NOT NULL,
Hotel_name varchar (10) NOT NULL,
Address varchar (600) NOT NULL,
Primary key (Hotel_no) );
Create table Room (
Room_no varchar (10) NOT NULL,
Hotel_no varchar (10) NOT NULL,
type varchar (40) NOT NULL,
price varchar (20) NOT NULL,
Primary key (Room_no, Hotel_no) );
Create table Guest (
Guest_no varchar (10) NOT NULL,
Guest_name varchar (10) NOT NULL,
Address varchar (600) NOT NULL,
Primary key (Guest_no) );
Create table Booking (
date_from date,
date_to date,
Hotel_no varchar (10) NOT NULL,
Guest_no varchar (10) NOT NULL,
Room_no varchar (10) NOT NULL,
Primary key (date_from, Hotel_no, Guest_no),
Foreign key(Room_no)references Room(Room_no));
when i try adding the foreign key to the booking table it gives an ORA-02270 error, and i cant seem to figure out the problem
any assistance would be greatly appreciated.
Your foreign key on Room should reference the primary key Room(Room_no, Hotel_no)
If the primary key is a set of columns then the foreign key should also be a set of columns corresponding to the composite key columns.
Try below code :
Create table Booking ( date_from date, date_to date, Hotel_no varchar (10) NOT NULL, Guest_no varchar (10) NOT NULL, Room_no varchar (10) NOT NULL, Primary key (date_from, Hotel_no, Guest_no), Foreign key(Room_no,Hotel_no)references Room(Room_no,Hotel_no);
TABLES
CREATE TABLE LocalBusiness
(
BusinessID INT NOT NULL PRIMARY KEY,
BusinessName VARCHAR2 (20) NOT NULL,
TypeID INT,
Latitude DECIMAL (10,2),
Longitude DECIMAL (10,2),
Web_address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
official_rating int,
min_price NUMBER(4,2),
max_price NUMBER(4,2),
FOREIGN KEY (TypeID) REFERENCES LocalBusinessType (TypeID),
CONSTRAINT chk_Officialrating CHECK (official_rating> 0 AND official_rating<6 )
);
CREATE TABLE Address
(
AddressID INT NOT NULL PRIMARY KEY,
BusinessID INT,
AreaID INT,
Address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
FOREIGN KEY (AreaID) REFERENCES Area (AreaID)
);
CREATE TABLE Phone
(
PhoneNoID INT NOT NULL PRIMARY KEY,
PhoneNo VARCHAR2 (15) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT PhoneNo_unique UNIQUE (PhoneNo)
);
CREATE TABLE Email
(
EmailID INT NOT NULL PRIMARY KEY,
email_address VARCHAR2 (50) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT email_unique UNIQUE (email_address)
);
CREATE TABLE Area
(
AreaID INT NOT NULL PRIMARY KEY,
AreaName VARCHAR2 (20) NOT NULL,
Region VARCHAR2 (20) NOT NULL
);
SELECT statement:
SELECT
LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price,
Phone.description, Phone.PhoneNo,
Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM
LocalBusiness
JOIN
Address ON LocalBusiness.BusinessID = Address.BusinessID
JOIN
Area ON Address.AreaID = Area.AreaID
AND LocalBusiness.BusinessID = Address.BusinessID
JOIN
Phone ON Phone.BusinessID = LocalBusiness.BusinessID
JOIN
Email ON Email.BusinessID = LocalBusiness.BusinessID
WHERE
TypeID = '1'
ORDER BY
LocalBusiness.BusinessName ASC;
The where clause in the sql join statement written above seem to be ineffective as the some values for the TypeID return incomplete data while others return no rows at all. How do I go about fixing this?
Yyou repeat a condition
AND LocalBusiness.BusinessID=Address.BusinessID in JOIN Area
Try avoinding this repetition
SELECT LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price, Phone.description,
Phone.PhoneNo, Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM LocalBusiness
JOIN Address ON LocalBusiness.BusinessID=Address.BusinessID
JOIN Area ON Address.AreaID=Area.AreaID
JOIN Phone ON Phone.BusinessID=LocalBusiness.BusinessID
JOIN Email ON Email.BusinessID=LocalBusiness.BusinessID
WHERE TypeID = '1'
ORDER BY LocalBusiness.BusinessName ASC;
Otherwise check th consistence of your data ..