0% found this document useful (0 votes)
10 views2 pages

1819.exam SQL - Jan28 Solved

The document contains the solutions to an exam on SQL databases. It includes creating a Customers table, a query to retrieve customer and product data, and a stored procedure to calculate average units in stock by category ID. The solutions provide the necessary SQL statements to meet the requirements of each question.

Uploaded by

apereb07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

1819.exam SQL - Jan28 Solved

The document contains the solutions to an exam on SQL databases. It includes creating a Customers table, a query to retrieve customer and product data, and a stored procedure to calculate average units in stock by category ID. The solutions provide the necessary SQL statements to meet the requirements of each question.

Uploaded by

apereb07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DATABASES

Second Exam, January 28, 2019


Duration: 30 minutes.
First Name:
Last Name:
Departamento de Ingenierías DNI:
ELÉCTRICA y de SISTEMAS Y
Signature:
AUTOMÁTICA

I. Write the Transact SQL code necessary for creating the following table, named Customers, of a database of a
live streaming platform: (35 points)
(Escribe el código necesario en Transact SQL para crear la siguiente tabla, llamada Customers, en la base de datos de una
plataforma de video en streaming. (35 puntos))

Column Name Details Domain Value Examples

First_name String of maximum length 50. This value must be necessarily ‘Cora’, ‘Iria’, ‘Manuel’, ‘John’, …
provided and (First_name, Last_name) has to be unique.

Last_name String of maximum length 60. This value must be necessarily ‘Romanov’, ‘Smith’, ‘Pérez’, ‘Wardlaw’, …
provided and (First_name, Last_name) has to be unique.

DNI String of fixed length of 9 characters. This value is the primary ‘00000000X’, ‘12345678P’, ‘98989898M’,
key. The value cannot be null and must have 8 numbers followed ‘71382938A’, …
by a letter

LastVideoWatched Integer. The value can be null. It is a foreign key of the table 1, 20103, 50000, 203, …
“Videos” referencing its column VideoID.

DateLastVideo Date. The value must be entered and the default value is the current 12/05/2013, 07/02/2011, 01/01/2001, 11/11/2001
date. …

Address String of max length 100. This value can be null. ‘C/ Ordoño II, 99, León (Spain)’, …

PrefPlatform String of maximum length 15. The value cannot be null. It must ‘PC’, ‘Tablet’, ‘Other’, …
have one of the following values: ‘PC’, ‘Mac’, ‘Tablet’,
‘Smartphone’, ‘Console’, ‘Other’.

II. This question relates to the Northwind database (See over for its E-R diagram). Write an SQL query to show the
all the product IDs and product names bought by the customer whose identifier is ‘LAZYK’. Show also (in the
same query) the name of such customer. (35 points).
NOTE: The name of the customer is stored in the column ‘ContactName’ of the table ‘Customers’
(Esta pregunta está relacionada con la base de datos Northwind (Mirad el diagrama E-R). Escribid una consulta SQL para
mostrar todos los IDs de los productos y los nombres de los productos comprados por el comprador con identificador
‘LAZYK’. Mostrad también el nombre de dicho comprador, en la misma consulta. (35 puntos))
NOTA: El nombre del comprador está almacenado en la columna ‘ContactName’ de la tabla ‘Customers’

III. This question relates to the Northwind database (See its E-R diagram). Write a stored procedure named
avgUnitsInStock that returns the average number of Units in Stock of the products whose category ID is
supplied as input parameter if the input parameter is not null. Write also the SQL sentence that invokes the
stored procedure and passes it the actual input parameter 3 (30 points).
(Esta pregunta está relacionada con la base de datos Northwind (Mirad el diagrama E-R). Escribid un procedimiento
almacenado llamado avgUnitsInStock que devuelve el número medio de unidades en stock de los productos cuyo ID de

1
Solutions Exam SQL 2nd Call (January 28, 2019)

Exercise 1 (Create table)

CREATE TABLE Customers (


First_name varchar(50) not null,
Last_name varchar(60) not null,
DNI char(9) not null CONSTRAINT PK_Customers PRIMARY KEY,
LastVideoWatched int CONSTRAINT FK_Custo_Videos FOREIGN KEY
REFERENCES Videos(VideoID),
DateLastVideo date not null CONSTRAINT DF_Custo_Date DEFAULT
getdate(),
AddressCusto varchar(100) null,
PrefPlatform varchar(15) not null CONSTRAINT CH_Custo_PrefPlat
CHECK(PrefPlatform IN
('PC','Mac','Tablet','Smartphone','Console','Other')),
CONSTRAINT CK_Custo_DNI CHECK (DNI LIKE('[0-9][0-9][0-9][0-9][0-
9][0-9][0-9][0-9][A-Z]')),
CONSTRAINT UQ_Customers_Name UNIQUE(First_name, Last_name)
)

Exercise 2 (Select query)

SELECT c.ContactName, p.ProductID, p.ProductName


FROM Products p JOIN [Order Details] od ON p.ProductID=od.ProductID
JOIN Orders o ON od.OrderID=o.OrderID
JOIN Customers c ON o.CustomerID=c.CustomerID
WHERE c.CustomerID='LAZYK'

Exercise 3 (Stored Procedure)

CREATE PROCEDURE avgUnitsInStock (@catID int)


AS
IF @catID IS NOT NULL
BEGIN
SELECT avg(UnitsInStock)
FROM Products
WHERE CategoryID=@catID
/*ELSE
SELECT 'Category is null'*/
END
RETURN

-- Execute the procedure


EXEC avgUnitsInStock 3

You might also like