1Schema::table('posts', function (Blueprint $table) {
2 $table->unsignedBigInteger('user_id');
3
4 $table->foreign('user_id')->references('id')->on('users');
5});
6OR
7Schema::table('posts', function (Blueprint $table) {
8 $table->foreignId('user_id')->constrained();
9});
1CREATE TABLE Employee(
2 EmployeeID int NOT NULL,
3 LastName varchar(50) NOT NULL,
4 FirstName varchar(20) NOT NULL,
5 Age int,
6 DeptNo int,
7 PRIMARY KEY (EmployeeID),
8 FOREIGN KEY (DeptNo) REFERENCES Department(DeptNo)
9);
10
1Foreign Key:
2It is a column that comes from a different table and
3using Foreign key tables are related each other
4It is the primary key of another table
5It can be duplicate or null for another table
6
7
8Primary Key :
9It is unique column in every table in a database
10It can ONLY accept;
11 - nonduplicate values
12 - cannot be NULL
13
14Unique Key:
15Only unique value and also can contain NULL
1USE Organization
2CREATE TABLE Employee
3(
4Id INT PRIMARY KEY IDENTITY(1,1),
5Name VARCHAR (50) NOT NULL,
6Age INT,
7Gender VARCHAR (50),
8Dep_Id int FOREIGN KEY REFERENCES Department(Id),
9Insur_Id int FOREIGN KEY REFERENCES Insurance(Id)
10)
1USE Organization
2CREATE TABLE Employee_Office
3(
4Id INT PRIMARY KEY IDENTITY(1,1),
5Emp_Id int FOREIGN KEY REFERENCES Employee(Id),
6Office_Id int FOREIGN KEY REFERENCES Office(Id)
7)