Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Relationship between 2 tables question

$
0
0
Hello guys, I'm new to SQL and I'm wondering if the relationship I've done below is correct or what else could I change to make it better?  Thanks in advance.
CREATE TABLE Cinema (
CinemaID int NOT NULL CONSTRAINT PK_Cinema_CinemaID PRIMARY KEY,
Name varchar(50) NOT NULL,
Location varchar(50) NULL
);

GO

CREATE TABLE Movies (
MovieID int NOT NULL CONSTRAINT PK_Movies_MovieID PRIMARY KEY,
Name varchar(50),
Genre varchar(40),
CinemaID int NOT NULL CONSTRAINT FK_Movies_CinemaID FOREIGN KEY REFERENCES Cinema(CinemaID),
ReleaseDate date,
EndDate date
);

GO

INSERT INTO Cinema (CinemaID,Name,Location)
VALUES (1,'CineWorld','Dublin'),
       (2,'IMC','Dun Laoghaire'),
       (3,'Savoy','Dublin'),
       (4,'IMC','Mullingar');

GO

INSERT INTO Movies (MovieID,Name,Genre,CinemaID,ReleaseDate,EndDate)
VALUES (1,'Iron Man 3','Action/Sci-fi',1,'2013-06-07',NULL),
       (2,'Avatar','Sci-fi/Adventure',1,'2011-12-17','2012-01-03');


SELECT c.Name AS 'Cinema Name', m.Name AS 'Movie Name'
FROM Movies AS m
INNER JOIN Cinema AS c ON m.CinemaID = c.CinemaID
WHERE m.Name LIKE 'I%'
ORDER BY m.Name;





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>