xxxxxxxxxx
CREATE TABLE dbo.city (
city_id int IDENTITY,
city varchar(50) NOT NULL,
country_id int NOT NULL,
CONSTRAINT PK_city PRIMARY KEY CLUSTERED (city_id)
)
ON [PRIMARY]
GO
CREATE TABLE dbo.country (
country_id int IDENTITY,
country varchar(50) NOT NULL,
CONSTRAINT PK_country PRIMARY KEY CLUSTERED (country_id)
)
ON [PRIMARY]
GO
Step 2
ALTER TABLE dbo.city WITH NOCHECK
ADD FOREIGN KEY (country_id) REFERENCES dbo.country (country_id)
GO
xxxxxxxxxx
--example of many to many relationship in sql with junction table.
CREATE TABLE Students (
Student_ID INT PRIMARY KEY,
Student_Name VARCHAR(50)
-- Other student-related columns
);
CREATE TABLE Courses (
Course_ID INT PRIMARY KEY,
Course_Name VARCHAR(50)
-- Other course-related columns
);
CREATE TABLE Student_Course (
Student_ID INT,
Course_ID INT,
PRIMARY KEY (Student_ID, Course_ID),
FOREIGN KEY (Student_ID) REFERENCES Students(Student_ID),
FOREIGN KEY (Course_ID) REFERENCES Courses(Course_ID)
);