Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use T-SQL to create two table structures based on the following entities: Table

ID: 3685016 • Letter: U

Question

Use T-SQL to create two table structures based on the following entities:

Table name: Booth

Table name: Machine

Booth_Product

Booth_Price

Machine_Product

Machine_Price

Each table will have the two attributes indicated in the example above. The MACHINE_PRODUCT and BOOTH_PRODUCT will be character data type and the two price fields will be a money data type. Copy and paste the T-SQL table create statements into your assignment submission for this part of the assignment. Preceding your T-SQL create table statement use the following statements:

use cis305;

if object_id('dbo.booth', 'U') is not null

       drop table dbo.booth;

…T-SQL create table statement

AND

use cis305;

if object_id('dbo.MACHINE', 'U') is not null

       drop table dbo.MACHINE;

…T-SQL create table statement

Question: What is the purpose of the if-drop statements?   

I've been struggling with this all week. Can someone work this out for me? I'm using Microsoft SQL Server Management Studio. It's due today.

Explanation / Answer

drop vs if-drop

drop: This will throw an error if the table doesn't exist or if other database objects depend on it. Most often, the other database objects will be foreign key references, but there may be others, too. (Views, for example.)

if-drop: This will not throw an error if the table doesn't exist, but it will still throw an error if other database objects depend on it.

Example:

Suppose there is a table called Booth. If you use "drop" or "if-drop" to delete the table then both works fine with any errors.

But if there is no table called Booth and if you use "drop" then it will throw an error. But "if-drop" will not throw an error.