0% found this document useful (0 votes)
11 views

Chapter 8 TestBank

Uploaded by

rajkarankeshani4
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)
11 views

Chapter 8 TestBank

Uploaded by

rajkarankeshani4
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/ 8

Chapter 8 TestBank

Exam ISTN212

Advanced SQL (Chapter 8)

1. Multiple Choice Questions (MCQs)


1. What is the primary purpose of the CREATE TABLE command in SQL?

A) To insert data into a table

B) To define a new table structure in the database

C) To modify the structure of an existing table

D) To delete a table from the database

2. Which SQL command is used to remove a table from the database?

A) DELETE TABLE

B) DROP TABLE

C) REMOVE TABLE

D) TRUNCATE TABLE

3. What does the PRIMARY KEY constraint do when creating a table?

A) Ensures that the column(s) can contain duplicate values

B) Creates a unique identifier for each row in the table

C) Allows null values in the column(s)

D) Defines a foreign key relationship with another table

4. How do you add a new column to an existing table in SQL?

A) ALTER TABLE tablename ADD columnname datatype;

B) UPDATE TABLE tablename ADD columnname datatype;

C) MODIFY TABLE tablename ADD columnname datatype;

Chapter 8 TestBank 1
D) INSERT INTO tablename (columnname datatype);

5. What is the purpose of the CREATE VIEW command?

A) To create a new table in the database

B) To update existing data in a table

C) To define a virtual table based on a SELECT query

D) To delete rows from a table

2. True/False Questions
1. The INSERT INTO command can be used to add new rows to a table.

2. The UPDATE command can be used to modify the structure of an existing table.

3. A VIEW in SQL is a physical table that stores data.

4. The DROP TABLE command removes both the table structure and its data from the database.

5. The FOREIGN KEY constraint is used to ensure data integrity between related tables.

3. Short Answer Questions


1. What is a data dictionary, and how is it related to database creation?

2. Describe the purpose of the DEFAULT keyword when creating a table in SQL.

3. Explain the difference between PRIMARY KEY and FOREIGN KEY constraints.

4. Provide the SQL command to create an index on the P_PRICE column of the PRODUCT table.

5. How can you copy data from one table to another using SQL? Provide an example.

4. Fill-in-the-Blanks
1. The __________ command is used to add a new column to an existing table structure.

2. To enforce a constraint that ensures no duplicate values in a column, use the __________

constraint.

3. The __________ command deletes a table from the database along with its structure and
data.

4. A __________ is a virtual table that is based on the result of a SELECT query.

Chapter 8 TestBank 2
5. The __________ keyword in SQL is used to provide a default value for a column if none is
specified by the user.

5. Essay Questions
1. Discuss the process of creating a new table in SQL, including defining columns, data
types, constraints, and primary keys. Provide examples for a product table and a
customer table.

2. Explain the role of indexes in SQL. How do they improve query performance, and
what are the potential downsides of using too many indexes? Provide examples of
creating and dropping indexes.

3. Describe the differences between the INSERT , UPDATE , and DELETE commands in SQL.
How do each of these commands affect the data in a table? Include examples to
illustrate each command.

4. What is a view in SQL, and how can it be beneficial for database management?
Describe how to create and manage a view. Provide an example of creating a view
based on a SELECT query.

6. Case Study
Scenario: You have a Sales table and need to create a new table called DiscountedSales that
includes only sales records where the discount is greater than 10%. You also need to ensure that
the new table has the same structure as the Sales table and copy relevant records into it.

Steps:

1. Create the DiscountedSales table with the same structure as the Sales table.

2. Copy the records with a discount greater than 10% from the Sales table into the
DiscountedSales table.

SQL Queries:

sql
Copy code
-- 1. Create the DiscountedSales table
CREATE TABLE DiscountedSales AS
SELECT * FROM Sales

Chapter 8 TestBank 3
WHERE 1 = 0; -- Create table with the same structure but no d
ata

-- 2. Insert relevant records into DiscountedSales


INSERT INTO DiscountedSales
SELECT * FROM Sales
WHERE Discount > 0.10;

Explanation:

The CREATE TABLE AS SELECT command creates a new table with the same structure as the
table. The INSERT INTO ...
Sales SELECT command copies records meeting the specified
criteria (discount > 10%) from the Sales table to the DiscountedSales table.

7. Application Questions
1. Write an SQL command to add a new column P_CATEGORY to the PRODUCT table with the data
type VARCHAR(20) .

2. Write an SQL query to create a unique index on the CUS_PHONE column in the CUSTOMER

table.

3. Provide an example of an UPDATE command that increases the price of all products in the
PRODUCT table by 5%.

4. Write an SQL command to delete all rows from the PRODUCT table where the P_QOH

(quantity on hand) is less than 10.

5. Create a view named HighValueProducts that includes all products with a price greater than
$100 from the PRODUCT table.

Answers

1. Multiple Choice Questions (MCQs)


1. B) To define a new table structure in the database

2. B) DROP TABLE

3. B) Creates a unique identifier for each row in the table

Chapter 8 TestBank 4
4. A) ALTER TABLE tablename ADD columnname datatype;

5. C) To define a virtual table based on a SELECT query

2. True/False Questions
1. True

2. False

3. False

4. True

5. True

3. Short Answer Questions


1. A data dictionary is a system catalog that stores metadata about the database structure,
including information about tables, columns, data types, and constraints. It is automatically
created by the RDBMS when a database is created.

2. The DEFAULT keyword specifies a default value for a column when no value is provided
during an INSERT operation.

3. A PRIMARY KEY uniquely identifies each row in a table and cannot contain null values. A
FOREIGN KEY establishes a relationship between tables by referencing the PRIMARY KEY of
another table.

4. CREATE INDEX indexname ON PRODUCT(P_PRICE);

5. Use the INSERT INTO ... SELECT command to copy data from one table to another. Example:

sql
Copy code
INSERT INTO PART (PART_CODE, PART_DESCRIPT, PART_PRICE, V_
CODE)
SELECT P_CODE, P_DESCRIPT, P_PRICE, V_CODE FROM PRODUCT;

4. Fill-in-the-Blanks
1. ALTER TABLE

Chapter 8 TestBank 5
2. UNIQUE

3. DROP TABLE

4. VIEW

5. DEFAULT

5. Essay Questions
1. Creating a New Table:

To create a table in SQL:

Define the table name and columns.

Specify data types and constraints for each column.

Example for a product table:

sql
Copy code
CREATE TABLE PRODUCT (
P_CODE VARCHAR(10) PRIMARY KEY,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE,
P_QOH SMALLINT,
P_PRICE NUMERIC(8,2)
);

Example for a customer table:

sql
Copy code
CREATE TABLE CUSTOMER (
CUS_CODE INT PRIMARY KEY,
CUS_LNAME VARCHAR(15) NOT NULL,
CUS_FNAME VARCHAR(15) NOT NULL,
CUS_INITIAL CHAR(1),
CUS_AREACODE CHAR(3) DEFAULT '615',

Chapter 8 TestBank 6
CUS_PHONE CHAR(12),
CUS_BALANCE DECIMAL(9,2) DEFAULT 0.00
);

2. Role of Indexes:
Indexes improve query performance by allowing quick access to rows based on indexed
columns. They can speed up search operations but might slow down INSERT , UPDATE , and
DELETE operations due to the overhead of maintaining the index. Example:

Create index:

sql
Copy code
CREATE INDEX idx_price ON PRODUCT(P_PRICE);

Drop index:

sql
Copy code
DROP INDEX idx_price;

3. INSERT , UPDATE , and DELETE :

INSERT : Adds new rows to a table. Example:

sql
Copy code
INSERT INTO PRODUCT (P_CODE, P_DESCRIPT, P_PRICE) VALUES
('12ABC', 'New Product', 19.99);

UPDATE : Modifies existing rows in a table. Example:

Chapter 8 TestBank 7
sql
Copy code
UPDATE PRODUCT SET P_PRICE = 21.99

Chapter 8 TestBank 8

You might also like