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

1 - DML and DDL Homework

Uploaded by

Phan Thieny
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)
25 views2 pages

1 - DML and DDL Homework

Uploaded by

Phan Thieny
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

DML and DDL Homework

Using the Chinook database and SQL Server Management Studio (SSMS), write SQL
queries for the following questions. Submit your answers in a single SQL file to the
online portal. Use SQL’s commenting syntax to include your name at the top of the file.
Also include the question number as a comment before each answer. The answer key to
the questions will be released after the deadline to submit homework has passed.

NOTE: The questions in this homework build off of one another so they must be executed in order.
You should add the following code to the beginning of your homework script. It will check for your
database and delete it if it exists. Replace my name with your database name:

USE master
IF DB_ID('MyDB_EricWilliamson') IS NOT NULL
BEGIN
ALTER DATABASE MyDB_EricWilliamson SET OFFLINE WITH ROLLBACK IMMEDIATE;
ALTER DATABASE MyDB_EricWilliamson SET ONLINE;
DROP DATABASE MyDB_EricWilliamson;
END

If you script everything correctly, you should be able to run your code repeatedly in one pass. It will
drop and recreate the database and its objects each time.

1. Create a new database called MyDB_[First and Last Name].


Example: MyDB_EricWilliamson
Add the following script after the create database command:
GO
USE [Your database name]
(The GO statement tells SQL to run any code that follows it as a separate statement. This will
allow me to run your entire homework script in one pass.)

2. Copy all records and columns from the Chinook.dbo.Customer table into a new table in your
database called “Users”.
Include the USE keyword to select your new database.
Hint: Let SQL build the table for you using your SELECT statement.

3. Delete all records from the Users table that have an odd CustomerId number .
Hint: There is a math operator to help you with this.

4. Update the Company column in the User table.


If the User Email contains “gmail” then write “Google” as the company.
If the User Email contains “yahoo” then write “Yahoo!” as the company.
Otherwise leave the current value for Company as is.
Hint: You can achieve this without using a WHERE clause.
5. Rename the Users CustomerId column to “UserId”.

6. Add a Primary Key to the Users table.


Set the UserId column as the primary key.
Name the primary key “pk_Users”

7. Create an Address table in the database


Add the following columns to the table: AddresId int, AddressType varchar(10), AddressLine1
varchar(50), City varchar(30), State varchar(2), UserId int, CreateDate datetime.
Make the AddressId column the primary key for the table and also make it an identity column
with a starting point of 1 and an increment of 1.
The CreateDate column should default to the date and time a record is added to the table.

8. Add a unique constraint to the Address table.


The constraint should not allow a duplicate combination of the UserId and AddressType
columns.
Name the constraint “uc_AddressType”.

9. Add a foreign key to the Address table.


The foreign key is UserId.
The primary key is the UserId column in the Users table.
Name the constraint “fk_UserAddress”.

10. Insert 3 records into the Address table.


Insert the following data into the correct columns on the Address table.
AddressType AddressLine1 City State UserID
home 111 Elm St. Los Angeles CA 2
home 222 Palm Ave. San Diego CA 4
work 333 Oak Ln. La Jolla CA 4
Hint: The AddressId and CreateDate columns should automatically populate with a value.

11. Select all records from the Address and Users tables. (32 rows total)
Two separate queries. One for Address and one for Users
No tricks here. I want to see the content of your tables so I can grade the homework faster.

You might also like