Foreign key and referential integrity - mysql

I've been facing an issue while I'm using MySQL, here is the issue, I've been created two tables named Persons and Orders, the Orders table references the Persons table as the following query shown, I inserted values in the Persons table, however, I when I check the Orders table, I spot that the value of PersonID column of the Persons table doesn't include in the PersonID column of the Orders table
CREATE TABLE Persons
(
PersonID INT NOT NULL UNIQUE PRIMARY KEY,
LastName VARCHAR(60) NOT NULL,
FirstName VARCHAR(60) NOT NULL,
AGE INT NOT NULL,
);
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

In foreign key relationship it's possible to have a value in master table but not in the foreign key column. But it's not possible to have a value in foreign key column of a detail table and not having it in master table.
If a PersonId is in the Persons table but not in Orders table then foreign key constraint won't raise any concern. But it will raise an error if you try to insert a PersonId into Orders table which is not available in PersonId column of Persons table.
Hope above discussions will help you to get your answer. Otherwise please share some sample data to clarify more.

Related

How we can use the primary key in another table

I have Two table
customer table which has contained the information of the customer. But it has an account number we make a primary key of the account number.
And now the second Table is Bill Table.I've use the account number of the customer table when we update some information about the Bill table then will update is automatically of the particular account number
so, please tell me how we can resolve this problem, and how we can use
the foreign key of Bill table
I think what you are asking is how to update the Customer table at the same time when you are updating Bill table.
You can easily use a stored procedure to achieve this task. Within the stored procedure you can use transactions to make sure the 2nd update happens only if the first update is succeded. Otherwise, you can rollback.
So imagine that this is your customer table:
CREATE TABLE customer (
AccountNum int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
PRIMARY KEY (AccountNum)
);
PRIMARY KEY (AccountNum) > so you have a primary key in that table. Kudos!
CREATE TABLE BillTable (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
AccountNum int NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (AccountNum) REFERENCES customer(AccountNum)
);
Now you linked customer and BillTable.

Intersection of Two Tables in SQL

Basically I need to create a new table that uses specific information from two other tables.
For example, I have a table called person with the elements person_id, first_name, last_name, gender, age, and fav_quote. I have a second table called department with the elements dept_id, dept_name, and building. I now need to create and intersection table with the person_id and dept_id elements included. And both must be the primary key (which I assume just means PRIMARY KEY (person_id, dept_id) command in my source).
CREATE TABLE person (
person_id INT(8) NOT NULL auto_increment,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
gender VARCHAR(1),
age INT(8),
fav_quote TEXT,
PRIMARY KEY (person_id)
);
CREATE TABLE department (
dept_id INT(8) NOT NULL auto_increment,
dept_name VARCHAR(25) NOT NULL,
building VARCHAR(25) NOT NULL,
PRIMARY KEY (dept_id)
);
That is the code I have for the initial two tables I'm just not sure how to create an intersection and, having gone back over my notes, I can't find the instructions on how to write it.
You got the primary key part right. I'd add foreign keys to your existing table in order to prevent creating interactions with people or departments that don't exist:
CREATE TABLE person_department
person_id INT(8) NOT NULL,
dept_id INT(8) NOT NULL,
PRIMARY KEY(person_id, dept_id),
FOREIGN KEY(person_id) REFERENCES person(person_id),
FOREIGN KEY(dept_id) REFERENCES department(dept_id)
)
You need a table with 2 fields; person_id and dept_id. The table will have foreign keys to the two tables person and department primary keys’ and a composite primary key of both.
Also, this table is only necessary if there is a one to many relationship of person to department. Otherwise just add dept_id as a foreign key in person.

MySQL - How to create a foreign key between three tables

I have been set the task of creating 3 tables in MySql. One table called subjects with a field called subject_id as the primary key, another table called students with a field called student_id and a final table called entries.
The entries table must have two foreign keys, subject_id and student_id.
This is the official task:
Can anyone possibly help?
In your create table query after you define the column that will be your foreign key, simply write "Foreign Key - References -" and specify the column (where I wrote the first dash mark) you wish to connect to the other table. The second dash should be the table name followed by the column it references in parentheses.
If the table is already made, just use an alter table query and write "add foreign key - references -" with the same format as above.
Something like this
CREATE TABLE Subjects (
SubjectId INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (SubjectId))
CREATE TABLE Students (
StudentId INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (StudentId))
CREATE TABLE Entries(
EntriesId INT NOT NULL AUTO_INCREMENT,
SubjectId INT NOT NULL,
StudentId INT NOT NULL,
FOREIGN KEY (SubjectId) REFERENCES Subjects (SubjectId),
FOREIGN KEY (StudentId) REFERENCES Students (StudentId))

Creating a table with primary and forgein key

im trying to create a products table with a primary and foreign key with this.
...
here is the code that i have tried so far
CREATE TABLE products
(
prod_id int NOT NULL,
prod_name int NOT NULL,
price varchar(15)
5on_hand varchar(15),
supp_id varchar(20),
PRIMARY KEY (prod_id),
FOREIGN KEY (supp_id)
);
any help would be greatly appreciated
A foreign key is a reference to a field in another table. To specify a foreign key, you have to specify what field(s) it refers to. The database cannot guess this.
So, guessing that supp_id refers to the id in the Suppliers table, your foreign key clause should look like this:
FOREIGN KEY (supp_id) REFERENCES Supplier(id)
I would strongly encourage the following:
All tables have an primary key column that is an integer and auto-incremented.
Names should be character strings.
Prices should be decimals.
So, I'm thinking something like this is appropriate:
CREATE TABLE Products (
ProductId int NOT NULL auto_increment primary key,
Name varchar(255) NOT NULL,
Price decimal(19, 4),
OnHand integer, -- assuming this is quantity on-hand
SupplierId int,
FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId)
);

How to add on delete cascade option in mysql?

I created two tables students and orders and I added a foreign key constraint to the order table but I forgot to add on delete cascade option to this table.
table STUDENTS:
CREATE TABLE STUDENTS (
ID varchar(50) NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
)
Table ORDERS
CREATE TABLE Orders
(O_Id int NOT NULL PRIMAY KEY,
Order_No int NOT NULL,ID varchar(50))
Add foreign key to "orders":
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID)
I tried this attempt :
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY(ID)
REFERENCES STUDENTS (ID) ON DELETE CASCADE
Thanks.
You have a typo in the Order table. You have PRIMAY where it should be PRIMARY.
After correcting this, I tried creating the tables and all statements worked fine, including the last one.