Movie Ticket System (DB) - sql

I'm making a online movie reservation system on MVC and I am stuck at the database part on how the tables would link to each other. Does it seem alright?
ERD
CREATE TABLE [dbo].[Member]
(
[memberID] INT IDENTITY (1, 1) NOT NULL,
[memFirstName] VARCHAR (50) NULL,
[memLastName] VARCHAR (50) NULL,
[memEmailAddress] VARCHAR (50) NULL,
[memPassword] VARCHAR (15) NULL,
[memTelephone] CHAR (10) NULL,
[memAddress] VARCHAR (70) NULL,
PRIMARY KEY CLUSTERED ([memberID] ASC)
);
CREATE TABLE [dbo].[Movie]
(
[movieID] INT IDENTITY (1, 1) NOT NULL,
[movieName] VARCHAR (100) NULL,
[movieCategory] CHAR (10) NULL,
PRIMARY KEY CLUSTERED ([movieID] ASC)
);
CREATE TABLE [dbo].[Reservation]
(
[resID] INT IDENTITY (1, 1) NOT NULL,
[memberID] INT NOT NULL,
[showID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([resID] ASC),
FOREIGN KEY ([memberID]) REFERENCES [dbo].[Member] ([memberID]),
FOREIGN KEY ([showID]) REFERENCES [dbo].[Show] ([showID])
);
CREATE TABLE [dbo].[Show]
(
[showID] INT IDENTITY (1, 1) NOT NULL,
[showDateTime] DATETIME NOT NULL,
[movieID] INT NULL,
CONSTRAINT [PK_Show] PRIMARY KEY CLUSTERED ([showID] ASC),
FOREIGN KEY ([showID]) REFERENCES [dbo].[Movie] ([movieID])
);

Related

How can I add two different features as a foreign key to my SQL Column?

I have a Car table in my Sql Data and every car has own BrandId that declared every Brands in "Brands" table as a foreign key. I'm just wondering what if I want to add two different BrandId together in my one Car>BrandId Column, what would I do? How can it be possible for adding two features as a foreign key in one spesific column?
Here is my Car Tables Code
CREATE TABLE [dbo].[Cars] (
[CarId] INT IDENTITY (1, 1) NOT NULL,
[CarName] VARCHAR (255) NULL,
[ColorId] INT NULL,
[BrandId] INT NULL,
[ModelYear] VARCHAR (255) NULL,
[DailyPrice] DECIMAL (18) NULL,
[Details] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([CarId] ASC),
FOREIGN KEY ([ColorId]) REFERENCES [dbo].[Colors] ([ColorId]),
FOREIGN KEY ([BrandId]) REFERENCES [dbo].[Brands] ([BrandId])
);
And here te code of my Brands table;
CREATE TABLE [dbo].[Brands] (
[BrandId] INT IDENTITY (1, 1) NOT NULL,
[BrandName] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([BrandId] ASC)
);
You can simply create your table as follows :
CREATE TABLE [dbo].[Cars] (
[CarId] INT IDENTITY (1, 1) NOT NULL,
[CarName] VARCHAR (255) NULL,
[ColorId] INT NULL,
[BrandId1] INT NULL,
[BrandId2] INT NULL,
[ModelYear] VARCHAR (255) NULL,
[DailyPrice] DECIMAL (18) NULL,
[Details] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([CarId] ASC),
FOREIGN KEY ([ColorId]) REFERENCES [dbo].[Colors] ([ColorId]),
FOREIGN KEY ([BrandId1]) REFERENCES [dbo].[Brands] ([BrandId]),
FOREIGN KEY ([BrandId2] REFERENCES [dbo].[Brands] ([BrandId])

Problems referencing primary key with foreign key

This is code to create the beginnings of a book library database with Microsoft SQL Server Management Studio.
CREATE DATABASE BOOK_LIBRARY
CREATE TABLE LIBRARY_USER
(
usr_id int not null primary key,
f_name varchar(30) not null,
m_init char(1),
l_name varchar(30) not null,
balance decimal(6,2),
join_date date,
addrss_1 varchar(30) not null,
addrss_2 varchar(30),
city varchar(30) not null,
addrss_state char(2) not null,
zip_code varchar(10) not null,
email varchar(30)
);
CREATE TABLE LIBRARY_TRANSACTIONS
(
transaction_id int not null primary key,
maximum_borrow_duration int not null,
strt_date date not null,
actual_return_date date not null,
borrow_usr_id int not null,
foreign key (borrow_usr_id) references LIBRARY_USER(usr_id)
);
CREATE TABLE BOOKS
(
isbn varchar(17) not null primary key,
title varchar(30) not null,
number_of_copies int not null,
Author varchar(30) not null,
Number_of_pages int not null,
publish_year int not null,
book_type varchar(20)
);
CREATE TABLE DIGITAL_BOOKS
(
digital_id int not null primary key,
format varchar(30) not null,
size_mb int not null,
digital_isbn varchar(17) not null,
foreign key(digital_isbn) references BOOKS(isbn)
);
CREATE TABLE PHYSICAL_BOOKS
(
physical_id int not null primary key,
condition varchar(20) not null,
physical_isbn varchar(17) not null,
foreign key(physical_isbn) references BOOKS(isbn)
);
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id),
foreign key(digi_id) references DIGITAL_BOOKS(digital_id),
foreign key(phys_id) references PHYSICAL_BOOKS(physical_id)
);
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
Despite me being able to look and see that digi_id and phys_id are in fact primary keys of the book_copy table, I keep getting this error:
Msg 1776, Level 16, State 0, Line 66
There are no primary or candidate keys in the referenced table 'BOOK_COPY' that match the referencing column list in the foreign key 'FK__CONTNS__digi_id__37A5467C'.
Msg 1750, Level 16, State 1, Line 66
Could not create constraint or index. See previous errors.
Am I missing something obvious? I'm just starting out with using this program, so any help is greatly appreciated.
Here:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
You are creating two foreign keys to parent table BOOK_COPY. But that table has a compound primary key (ie a multi-column primary key)
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id), --> here
...
)
As a consequence, you need a compound foreign key:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id, phys_id) references BOOK_COPY(digi_id, phys_id)
);

T-SQL Use a column name as a parameter in a function used as default value

I have this table:
CREATE TABLE [Operating].[SalesDetails] (
[BillId] INT NOT NULL,
[SerialNumber] NVARCHAR (50) NOT NULL,
[Quantity] INT NOT NULL,
[Price] DECIMAL (19, 2) DEFAULT (IT.SalePrice([SerialNumber])) NOT NULL,
[LineTotal] AS ([Quantity]*[Price]),
[Returned] BIT DEFAULT ((0)) NOT NULL,
[Note] NVARCHAR (30) NULL,
CONSTRAINT [PK_Operating_SalesDetails_BillId_SerialNumber]
PRIMARY KEY CLUSTERED ([BillId] ASC, [SerialNumber] ASC),
CONSTRAINT [FK_Operating_SalesDetails_Operating_Sales_BillId]
FOREIGN KEY ([BillId]) REFERENCES [Operating].[Sales] ([BillId]),
CONSTRAINT [FK_Operating_SalesDetails_Operating_PurchasesDetails_SerialNumber]
FOREIGN KEY ([SerialNumber])
REFERENCES [Operating].[PurchasesDetails] ([SerialNumber]),
CONSTRAINT [CK_Operating_SalesDetails_Price_Positive]
CHECK ([Price]>=(0)),
CONSTRAINT [CK_Operating_SalesDetails_Quantity_Positive]
CHECK ([Quantity]>(0))
);
Function:
CREATE FUNCTION [IT].[SalePrice] (#SerialNumber NVARCHAR (50)) RETURNS DECIMAL (19, 2)
AS
BEGIN
DECLARE #SalePrice DECIMAL (19, 2)
SET #SalePrice = (
SELECT [Sale]
FROM [Operating].[PurchasesDetails]
WHERE [SerialNumber] = #SerialNumber
)
RETURN #SalePrice
END
This is the Purchases Details table:
CREATE TABLE [Operating].[PurchasesDetails] (
[BillId] INT NOT NULL,
[ProductId] INT NOT NULL,
[ManufacturerId] INT NOT NULL,
[SerialNumber] NVARCHAR (50) DEFAULT ([IT].[SerialNumber]()) NOT NULL,
[Quantity] INT NOT NULL,
[Purchase] DECIMAL (19, 2) NOT NULL,
[Sale] DECIMAL (19, 2) NOT NULL,
[Returned] BIT DEFAULT ((0)) NOT NULL,
[Note] NVARCHAR (30) NULL,
CONSTRAINT [PK_Operating_PurchasesDetails_SerialNumber]
PRIMARY KEY CLUSTERED ([SerialNumber] ASC),
CONSTRAINT [FK_Operating_PurchasesDetails_Operating_Purchases_BillId]
FOREIGN KEY ([BillId]) REFERENCES [Operating].[Purchases] ([BillId]),
CONSTRAINT [FK_Operating_PurchasesDetails_Operating_Products_ProductId]
FOREIGN KEY ([ProductId]) REFERENCES [Operating].[Products] ([ProductId]),
CONSTRAINT [FK_Operating_PurchasesDetails_Operating_Manufacturers_ManufacturerId]
FOREIGN KEY ([ManufacturerId])
REFERENCES [Operating].[Manufacturers] ([ManufacturerId]),
CONSTRAINT [CK_Operating_PurchasesDetails_Purchase_Positive]
CHECK ([Purchase]>=(0)),
CONSTRAINT [CK_Operating_PurchasesDetails_Quantity_Positive]
CHECK ([Quantity]>(0)),
CONSTRAINT [CK_Operating_PurchasesDetails_Sale_Positive]
CHECK ([Sale]>=(0)),
CONSTRAINT [CK_Operating_PurchasesDetails_SerialNumber_Quantity]
CHECK ([Quantity]=(1) OR [Quantity]>(1)
AND [SerialNumber] like 'NS[0-1][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
);
I get error SQL70536 when I use [SerialNumber] as a parameter.
SQL70536: The name “[SerialNumber]” is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Note: The function is tested and is working.
Please help.
A trigger solved the problem. Thanks.
CREATE TABLE [Operating].[SalesDetails] (
[BillId] INT NOT NULL,
[SerialNumber] NVARCHAR (50) NOT NULL,
[Quantity] INT NOT NULL,
[Price] DECIMAL (19, 2) NOT NULL,
[LineTotal] AS ([Quantity] * [Price]),
[Returned] BIT DEFAULT ((0)) NOT NULL,
[Note] NVARCHAR (30) NULL,
CONSTRAINT [PK_Operating_SalesDetails_BillId_SerialNumber]
PRIMARY KEY CLUSTERED ([BillId] ASC, [SerialNumber] ASC),
CONSTRAINT [FK_Operating_SalesDetails_Operating_Sales_BillId]
FOREIGN KEY ([BillId]) REFERENCES [Operating].[Sales] ([BillId]),
CONSTRAINT [FK_Operating_SalesDetails_Operating_PurchasesDetails_SerialNumber]
FOREIGN KEY ([SerialNumber])
REFERENCES [Operating].[PurchasesDetails] ([SerialNumber]),
CONSTRAINT [CK_Operating_SalesDetails_Price_Positive] CHECK ([Price] >= (0)),
CONSTRAINT [CK_Operating_SalesDetails_Quantity_Positive] CHECK ([Quantity] > (0))
);
GO
CREATE TRIGGER [Operating].[SalePrice] ON [Operating].[SalesDetails]
INSTEAD OF INSERT AS
BEGIN
INSERT INTO [Operating].[SalesDetails]
([BillId],[SerialNumber],[Quantity],[Price],[Returned],[Note])
VALUES (
(SELECT [BillId] FROM [INSERTED]),
(SELECT [SerialNumber] FROM [INSERTED]),
(SELECT [Quantity] FROM [INSERTED]),
(SELECT [Sale] FROM [Operating].[PurchasesDetails]
WHERE [SerialNumber] = (SELECT [SerialNumber] FROM [INSERTED])),
(SELECT [Returned] FROM [INSERTED]),
(SELECT [Note] FROM [INSERTED]))
END;
Is there a shorter way to write the trigger?
I eddited the trigger as suggested:
CREATE TABLE [Operating].[SalesDetails] (
[BillId] INT NOT NULL,
[SerialNumber] NVARCHAR (50) NOT NULL,
[Quantity] INT NOT NULL,
[Price] DECIMAL (19, 2) NOT NULL,
[LineTotal] AS ([Quantity]*[Price]),
[Returned] BIT DEFAULT ((0)) NOT NULL,
[Note] NVARCHAR (30) NULL,
CONSTRAINT [PK_Operating_SalesDetails_BillId_SerialNumber]
PRIMARY KEY CLUSTERED ([BillId] ASC, [SerialNumber] ASC),
CONSTRAINT [FK_Operating_SalesDetails_Operating_Sales_BillId]
FOREIGN KEY ([BillId]) REFERENCES [Operating].[Sales] ([BillId]),
CONSTRAINT [FK_Operating_SalesDetails_Operating_PurchasesDetails_SerialNumber]
FOREIGN KEY ([SerialNumber])
REFERENCES [Operating].[PurchasesDetails] ([SerialNumber]),
CONSTRAINT [CK_Operating_SalesDetails_Price_Positive] CHECK ([Price]>=(0)),
CONSTRAINT [CK_Operating_SalesDetails_Quantity_Positive] CHECK ([Quantity]>(0))
);
GO
CREATE TRIGGER [Operating].[SalePrice] ON [Operating].[SalesDetails]
INSTEAD OF INSERT AS
BEGIN
INSERT INTO [Operating].[SalesDetails]
([BillId],[SerialNumber],[Quantity],[Price],[Returned],[Note])
SELECT I.[BillId], I.[SerialNumber], I.[Quantity],
COALESCE (I.[Price],P.[Sale]), I.[Returned], I.[Note]
FROM [inserted] AS I LEFT OUTER JOIN [Operating].[PurchasesDetails] AS P
ON I.[SerialNumber] = P.[SerialNumber]
END;
Thank you for your help.

Can a value be a primary and a foreign key?

I have the following tables in my SQL Server database.
Customer is a subtype of User thus I made a reference from Customer to User.
I also want to link my customer to my reservation. I'd like to use the Foreign key in the Customer table as a PK for this relation. When I write this out in SQL Server, I get this error under "(userid)":
Invalid column 'userid'
How can I make this relation?
Create table [dbo].[User]
(
[id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
[name] varchar(50) NOT NULL,
[password] nvarchar(100) NOT NULL,
)
Create table [dbo].[Customer]
(
[userid] int FOREIGN KEY REFERENCES [dbo].[User](id) NOT NULL,
[street] varchar(40) NOT NULL,
[housenumber] varchar(10) NOT NULL,
[postalcode] varchar(10) NOT NULL,
[phonenumber] varchar(20) NOT NULL,
[email] varchar(30) NOT NULL,
)
Create table [dbo].[Reservation]
(
[id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
[datum] date NOT NULL,
[prijs] decimal NOT NULL,
[levertijd] datetime NOT NULL,
[ophaaltijd] datetime NOT NULL,
[leverAdres] varchar(60) NOT NULL,
[klantId] int FOREIGN KEY REFERENCES [dbo].[Customer](userid) NOT NULL
)
yes this is possible, try it like this
Create table [dbo].[Customer]
(
[userid] int not null,
[street] varchar(40) NOT NULL,
[housenumber] varchar(10) NOT NULL,
[postalcode] varchar(10) NOT NULL,
[phonenumber] varchar(20) NOT NULL,
[email] varchar(30) NOT NULL,
constraint PK_UserID primary key ([userid]),
constraint FK_Customer_User foreign key (userid) references [User] (id)
)
But I have to say that userid seems like an odd primary key for a table called Customer
I would so something like this :
Create table [dbo].[Customer]
(
[CustomerID] int not null identity,
[userid] int not null,
[street] varchar(40) NOT NULL,
[housenumber] varchar(10) NOT NULL,
[postalcode] varchar(10) NOT NULL,
[phonenumber] varchar(20) NOT NULL,
[email] varchar(30) NOT NULL,
constraint PK_CustomerID primary key ([CustomerID]),
constraint FK_Customer_User foreign key (userid) references [User] (id)
)
Create table [dbo].[Reservation]
(
[id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
[datum] date NOT NULL,
[prijs] decimal NOT NULL,
[levertijd] datetime NOT NULL,
[ophaaltijd] datetime NOT NULL,
[leverAdres] varchar(60) NOT NULL,
[klantId] int FOREIGN KEY REFERENCES [dbo].[Customer](customerid) NOT NULL
)

Azure SQL Foreign Key with Visual Studio 2015

I have an Azure SQL database created in the management station, I connect to it on Visual Studio 2015, I create tables Player and Team, I'm trying to create a foreign key to reference team name but I keep getting the following error;
SQL71516 :: The referenced table '[dbo].[Team]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
From looking at online sources, mainly MSDN, I have tried a few ways to solve this issue but have had no luck. Here is my SQL code;
Team Table:
CREATE TABLE [dbo].[Team] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Wins] INT NOT NULL,
[Draws] INT NOT NULL,
[Losses] INT NOT NULL,
[GoalsFor] INT NOT NULL,
[GoalsAgainst] INT NOT NULL,
[GoalDifference] INT NOT NULL,
[Points] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Player Table:
CREATE TABLE [dbo].[Player] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Team] VARCHAR (30) NOT NULL,
[Goals] INT NOT NULL,
[Assists] INT NOT NULL,
[Apps] INT NOT NULL,
[Club] VARCHAR (30) NOT NULL,
CONSTRAINT [FK_Player_Club] FOREIGN KEY ([Club]) REFERENCES [dbo].[Team]([Name]),
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Team Table:
CREATE TABLE [dbo].[Team] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Wins] INT NOT NULL,
[Draws] INT NOT NULL,
[Losses] INT NOT NULL,
[GoalsFor] INT NOT NULL,
[GoalsAgainst] INT NOT NULL,
[GoalDifference] INT NOT NULL,
[Points] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Player Table:
CREATE TABLE [dbo].[Player] (
[Id] INT NOT NULL,
[Name] VARCHAR (30) NOT NULL,
[Team] VARCHAR (30) NOT NULL,
[Goals] INT NOT NULL,
[Assists] INT NOT NULL,
[Apps] INT NOT NULL,
[TeamId] INT NOT NULL,
CONSTRAINT [FK_Player_Club] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Team]([Id]),
PRIMARY KEY CLUSTERED ([Id] ASC)
);