1.12: Solutions

1.1 Create Database Solution

To create a new database called "SQL_Library" using SQL, enter the following code into a SQL query:

CREATE DATABASE SQL_Library
GO

1.2 Drop Database Solution

Delete the SQL_Library database if it exists.

USE master;  
GO  

IF EXISTS(SELECT * from sys.databases WHERE name='SQL_Library')  
BEGIN  
    DROP DATABASE SQL_Library;  
END

1.3 Create Table Solution

To create a Products table, enter the following code in a SQL query:

CREATE TABLE dbo.Products
   (ProductID int PRIMARY KEY NOT NULL, 
    ProductName varchar(25) NOT NULL,
    Price money NULL,
    ProductDescription text NULL);

Note: Unlike some query languages, T-SQL does not require a semi-colon at the end of a statement. Therefore, the semi-colon shown in the example above is optional. You may also notice that SQL keywords are not case-sensitive. For example, "create table" would produce the same result as "CREATE TABLE," as shown above.

1.4 Insert Solution

Note: You may notice that the last two examples do not include "dbo." in front of "Products." Adding "dbo." in front of the table name is optional in some query languages, including T-SQL.

1.5 Update Solution

1.6 Read Solution

1.7 Alter Solution

Add a column called "Manufacturer":

Drop the "Manufacturer" column:

Add a column called "UPC" with data type VARCHAR(25):

Change the "UPC" column to a TEXT data type:

Drop the "UPC" column:

1.8 Customer Table Solution

Create a "Customers" table with the specified criteria:

Seed table with specified data:

This is what your resulting table should look like:

Table with seed data in Visual Studio

1.9 Diagramming Notation Solution

Diagramming Notation Solution Table

1.10 Foreign Keys Solution

Purchase Table Solution

Copy and paste the code below into a new query. You'll need it for the next lesson.

1.11 Join Solution

This is what your query should look like:

Last updated