How to make (patient id) forgein key in table of bill?How does database indexing work?How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerGet list of all tables in Oracle?Use of null values in related tables with foreign key constraintsHow do I UPDATE from a SELECT in SQL Server?mySql errno: 150 Create table statement insideDatabase design for patients and diseasesDoctors, Patients and Contact information for bothHow to join sql tables correctly to avoid data repetition?
What is the best way to deal with NPC-NPC combat?
What is the most expensive material in the world that could be used to create Pun-Pun's lute?
Magical attacks and overcoming damage resistance
Creating a chemical industry from a medieval tech level without petroleum
Co-worker works way more than he should
What's the difference between using dependency injection with a container and using a service locator?
"The cow" OR "a cow" OR "cows" in this context
Is Electric Central Heating worth it if using Solar Panels?
Could moose/elk survive in the Amazon forest?
What is the unit of time_lock_delta in LND?
How can I practically buy stocks?
My bank got bought out, am I now going to have to start filing tax returns in a different state?
"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"
Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?
Is it acceptable to use working hours to read general interest books?
Nails holding drywall
Prove that the countable union of countable sets is also countable
Why must Chinese maps be obfuscated?
Can a stored procedure reference the database in which it is stored?
How exactly does Hawking radiation decrease the mass of black holes?
As an international instructor, should I openly talk about my accent?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
A strange hotel
Can I criticise the more senior developers around me for not writing clean code?
How to make (patient id) forgein key in table of bill?
How does database indexing work?How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerGet list of all tables in Oracle?Use of null values in related tables with foreign key constraintsHow do I UPDATE from a SELECT in SQL Server?mySql errno: 150 Create table statement insideDatabase design for patients and diseasesDoctors, Patients and Contact information for bothHow to join sql tables correctly to avoid data repetition?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
);
sql oracle
add a comment |
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
);
sql oracle
Please accept my answer if it helped you. Thanks!
– Josh Katz
Mar 22 at 16:46
add a comment |
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
);
sql oracle
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
);
sql oracle
sql oracle
edited Mar 22 at 18:23
DxTx
1,7921822
1,7921822
asked Mar 22 at 16:34
Mohamed AbdallaMohamed Abdalla
185
185
Please accept my answer if it helped you. Thanks!
– Josh Katz
Mar 22 at 16:46
add a comment |
Please accept my answer if it helped you. Thanks!
– Josh Katz
Mar 22 at 16:46
Please accept my answer if it helped you. Thanks!
– Josh Katz
Mar 22 at 16:46
Please accept my answer if it helped you. Thanks!
– Josh Katz
Mar 22 at 16:46
add a comment |
4 Answers
4
active
oldest
votes
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_Idcolumn (Patient_Id Varchar (5) Foreign key,,)
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
add a comment |
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)
);
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
add a comment |
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
add a comment |
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 );
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304090%2fhow-to-make-patient-id-forgein-key-in-table-of-bill%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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_Idcolumn (Patient_Id Varchar (5) Foreign key,,)
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
add a comment |
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_Idcolumn (Patient_Id Varchar (5) Foreign key,,)
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
add a comment |
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_Idcolumn (Patient_Id Varchar (5) Foreign key,,)
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_Idcolumn (Patient_Id Varchar (5) Foreign key,,)
edited Mar 22 at 18:01
answered Mar 22 at 17:01
DxTxDxTx
1,7921822
1,7921822
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
add a comment |
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
How to write code as you . 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) );
– Mohamed Abdalla
Mar 22 at 17:16
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
What do you mean..? Are you asking about the formatting or something else..? BTW, you can check these code using this website.
– DxTx
Mar 22 at 17:38
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You commit at my post but I want to write code as you
– Mohamed Abdalla
Mar 22 at 17:47
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
You're talking about formatting. :) That's easy actually. Check these articles: link1, link2, link3, link4. If you want to format your SQL query, you can use this site to do that.
– DxTx
Mar 22 at 17:58
add a comment |
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)
);
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
add a comment |
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)
);
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
add a comment |
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)
);
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)
);
answered Mar 22 at 16:55
SK -SK -
898
898
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
add a comment |
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
How to write code as you
– Mohamed Abdalla
Mar 22 at 17:14
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Mar 22 at 17:43
DxTx
1,7921822
1,7921822
answered Mar 22 at 16:46
Josh KatzJosh Katz
83119
83119
add a comment |
add a comment |
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 );
add a comment |
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 );
add a comment |
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 );
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 );
edited Mar 23 at 9:42
answered Mar 23 at 9:31
William RobertsonWilliam Robertson
8,57632233
8,57632233
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304090%2fhow-to-make-patient-id-forgein-key-in-table-of-bill%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please accept my answer if it helped you. Thanks!
– Josh Katz
Mar 22 at 16:46