Chapter 8 TestBank
Chapter 8 TestBank
Exam ISTN212
A) DELETE TABLE
B) DROP TABLE
C) REMOVE TABLE
D) TRUNCATE TABLE
Chapter 8 TestBank 1
D) INSERT INTO tablename (columnname datatype);
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.
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.
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.
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
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
5. Create a view named HighValueProducts that includes all products with a price greater than
$100 from the PRODUCT table.
Answers
2. B) DROP TABLE
Chapter 8 TestBank 4
4. A) ALTER TABLE tablename ADD columnname datatype;
2. True/False Questions
1. True
2. False
3. False
4. True
5. True
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.
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:
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)
);
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;
sql
Copy code
INSERT INTO PRODUCT (P_CODE, P_DESCRIPT, P_PRICE) VALUES
('12ABC', 'New Product', 19.99);
Chapter 8 TestBank 7
sql
Copy code
UPDATE PRODUCT SET P_PRICE = 21.99
Chapter 8 TestBank 8