KnowledgeBoat Logo
OPEN IN APP

Chapter 14

Table Creation and Data Manipulation Commands

Class 12 - Computer Science with Python Sumita Arora



Checkpoint 14.1

Question 1

Which command is used for creating tables ?

Answer

In SQL, CREATE TABLE command is used for creating tables in database.

Question 2

What is a constraint ? Name some constraints that you can apply to enhance database integrity.

Answer

A constraint is a condition or check applicable on a field or set of fields.
Some constraints that we can apply to enhance database integrity are:

  1. Unique constraint.
  2. Primary key constraint.
  3. Default constraint.
  4. Check constraint.
  5. Foreign key constraint.

Question 3

What is the role of UNIQUE constraint ? How is PRIMARY KEY constraint different from UNIQUE constraint ?

Answer

UNIQUE constraint ensures that no two rows have the same value in the specified column(s).
There are differences between UNIQUE and PRIMARY KEY constraints. Though both ensure unique values for each row in a column, but UNIQUE allows NULL values whereas PRIMARY KEY does not. Also, there can exist multiple columns with UNIQUE constraints in a table, but there can exist only one column or one combination with PRIMARY KEY constraint.

Question 4

What is primary key ? What is PRIMARY KEY constraint ?

Answer

A primary key is a unique identifier for each record in a table, and it must be unique and not null. A PRIMARY KEY constraint declares a column or one group of columns as the primary key of the table. This constraint must be applied to columns declared as NOT NULL.

Question 5

What is NOT NULL constraint ? What is DEFAULT constraint ?

Answer

The NOT NULL constraint is used in SQL to ensure that a column cannot contain NULL (i.e., empty) values.

A DEFAULT constraint is used in SQL to specify a default value for a column in a table. When the user does not enter a value for the column (having default value), automatically the defined default value is inserted in the field.

Question 6

When a column's value is skipped in an INSERT command, which value is inserted in the database ?

Answer

The columns that are not listed in the INSERT command will have their default value, if it is defined for them, otherwise, NULL value. If any other column (that does not have a default value and is defined NOT NULL) is skipped or omitted, an error message is generated and the row is not added.

Question 7

Can a column defined with NOT NULL constraint, be skipped in an INSERT command ?

Answer

If a column defined with a NOT NULL constraint is skipped in an INSERT command, it will result in an error, and the row will not be added to the table.

Question 8

How would you view the structure of table Dept ?

Answer

To view the structure of a table in SQL, we use DESC[RIBE] command of MySQL * Plus. The syntax of this command is as follows : DESC[RIBE] <tablename> ;.

For example, the command to view the structure of table Dept is DESCRIBE Dept ; or DESC Dept ;.

Question 9

Table Empl has same structure as that of table EMPL. Write a query statement to insert data from table NewEmpl into EMPL where salary and comm is more than Rs. 4000.

Answer

INSERT INTO Empl 
SELECT *
FROM NewEmpl
WHERE SAL > 4000 AND COMM > 4000 ;

Question 10

What is the error in following statement ?

UPDATE EMPL ;

Answer

The error in the statement UPDATE EMPL; is that it is incomplete. The UPDATE command specifies the rows to be changed using the WHERE clause and the new data using the SET keyword. Therefore, the statement is incomplete as it lacks both the SET and WHERE clauses.

Question 11

Identify the error :

DELETE ALL FROM TABLE EMPL ;

Answer

The statement DELETE ALL FROM TABLE EMPL; is in error due to the misuse of the keyword ALL and the unnecessary inclusion of TABLE before the table name. In SQL, the syntax of DELETE statement is :

DELETE FROM  <TABLENAME>
[ WHERE <PREDICATE> ] ;

According to this syntax, the correct command to remove all the contents of EMPL table is :

DELETE FROM EMPL ;

Question 12

Differentiate between DDL and DML.

Answer

Data Definition Language (DDL)Data Manipulation Language (DML)
DDL provides a set of definitions to specify the storage structure and access methods used by the database system.DML is a language that enables users to access or manipulate data as organized by the appropriate data model.
DDL commands are used to perform tasks such as creating, altering, and dropping schema objects. They are also used to grant and revoke privileges and roles, as well as for maintenance commands related to tables.DML commands are used to retrieve, insert, delete, modify data stored in the database.
Examples of DDL commands are CREATE, ALTER, DROP, GRANT, ANALYZE etc.Examples of DML commands are INSERT, UPDATE, DELETE, SELECT etc.

Multiple Choice Questions

Question 1

Consider the following SQL statement. What type of statement is this ?

CREATE TABLE employee (name VARCHAR, id INTEGER)
  1. DML
  2. DDL
  3. DCL
  4. Integrity constraint

Answer

DDL

Reason — The SQL statement CREATE TABLE employee (name VARCHAR, id INTEGER) is a Data Definition Language (DDL) statement. DDL statements are used to define, modify, and delete database objects such as tables, views, indexes, etc. In this case, the statement is creating a new table named employee with columns, name of type VARCHAR and id of type INTEGER.

Question 2

The data types CHAR(n) and VARCHAR(n) are used to create ..............., and ............... length types of string/text fields in a database.

  1. Fixed, equal
  2. Equal, variable
  3. Fixed, variable
  4. Variable, equal

Answer

Fixed, variable

Reason — CHAR datatype specifies a fixed length string. Defining a length is not required, but the default is 1. While VARCHAR datatype specifies a variable length string. Defining a length is required.

Question 3

A table Table1 has two text fields defined as below :

:
Name1 varchar(20), 
Name2 char(20),
:

If Name1 stores value as 'Ana' and Name2 stores value as 'Anuj', then Name1 will consume .............. characters' space and Name2 will consume ............... characters' space.

  1. 3, 20
  2. 20, 4
  3. 20, 20
  4. 3, 4

Answer

3, 20

Reason — For the field Name1 with VARCHAR(20) datatype, storing the value 'Ana' will consume 3 character's space because VARCHAR(n) column can have a maximum size of n bytes and it stores values exactly as specified without adding blanks for shorter lengths. Exceeding n bytes results in an error message. Whereas for the field Name2 with CHAR(20) datatype, storing the value 'Anuj' will consume 20 characters' space because CHAR(n) ensures that all values stored in that column are of length n bytes, padding shorter values with blanks while maintaining a fixed size of n bytes.

Question 4

Consider the following SQL statement. What type of statement is this ?

INSERT INTO instructor VALUES (10211, 'Shreya' , 'Biology', 66000 ) ;
  1. Procedure
  2. DML
  3. DCL
  4. DDL

Answer

DML

Reason — The above SQL statement is Data Manipulation Language (DML) statement. DML statements are used to access and manipulate data in tables. The DML commands include SELECT, LOCK TABLE, UPDATE, INSERT INTO, DELETE. In this case, the INSERT INTO statement is used to insert a new row of data into the instructor table.

Question 5

In the given query which keyword has to be inserted ?

INSERT INTO employee ............... (1002, Kausar, 2000) ;
  1. Table
  2. Values
  3. Relation
  4. Field

Answer

Values

Reason — The syntax of INSERT INTO command is :

INSERT INTO <tablename> [<column List>]
Values (<value>, <value> ...............) ;

According to this syntax, Values keyword is used to specify the values that will be inserted into the specified columns of the table.

Question 6

Which of the following is/are the DDL statements ?

  1. Create
  2. Drop
  3. Alter
  4. All of these

Answer

All of these

Reason — DDL (Data Definition Language) commands are used to create and define tables and other database objects in SQL (Structured Query Language). DDL commands such as CREATE, ALTER, and DROP, are used to create, define, change and delete objects like tables, indexes, views, and constraints.

Question 7

In SQL, which command(s) is(are) used to change a table's structure / characteristics ?

  1. ALTER TABLE
  2. MODIFY TABLE
  3. CHANGE TABLE
  4. All of these

Answer

ALTER TABLE

Reason — The ALTER TABLE command in SQL is used to change the definitions of existing tables. It allows for various operations such as adding a new column, redefining a column, and adding an integrity constraint. Therefore, it changes the structure of the table.

Question 8

Which of the following commands will delete the table from MYSQL database ?

  1. DELETE TABLE
  2. DROP TABLE
  3. REMOVE TABLE
  4. ALTER TABLE

Answer

DROP TABLE

Reason — The DROP TABLE command in SQL will delete the table from the MYSQL database. Once this command is executed, the table and all its associated data are removed from the database. After dropping the table, the table name is no longer recognized within the database system, and no further commands can be executed on that object.

Question 9

............... defines rules regarding the values allowed in columns and is the standard mechanism for enforcing database integrity.

  1. Column
  2. Constraint
  3. Index
  4. Trigger

Answer

Constraint

Reason — Constraint defines rules regarding the values allowed in columns and is the standard mechanism for enforcing database integrity. Once an integrity constraint is enabled, all data in the table must confirm to the rule that it specifies.

Question 10

Fill in the blank :

............... command is used to remove primary key from a table in SQL.

  1. update
  2. remove
  3. alter
  4. drop

Answer

alter

Reason — To remove a primary key constraint from a table the ALTER command is used. The DROP clause of ALTER TABLE command is used with syntax ALTER TABLE <TABLENAME> DROP PRIMARY KEY ;.

Question 11

Which command defines its columns, integrity constraint in create table :

  1. Create table command
  2. Drop table command
  3. Alter table command
  4. All of these

Answer

Create table command

Reason — The CREATE TABLE command is used to define a new table in SQL, and it allows to define the columns of the table along with any integrity constraints such as primary keys, foreign keys, unique constraints, etc.

Question 12

Which command is used for removing a table and all its data from the database :

  1. Create table command
  2. Drop table command
  3. Alter table command
  4. All of these

Answer

Drop table command

Reason — The DROP TABLE command is used to delete a table and all its data from the database. Once this command is given, the table name is no longer recognized and no more commands can be given on the object.

Question 13

Which of the following is not a DDL command ?

  1. UPDATE
  2. TRUNCATE
  3. ALTER
  4. None of these

Answer

UPDATE

Reason — Data Definition Language (DDL) statements are used to define, modify, and delete database objects such as tables, views, indexes, etc. The DDL commands are CREATE, ALTER, TRUNCATE, DROP etc. But the UPDATE command is Data Manipulation Language (DML) command, used to modify existing data in a table.

Question 14

Which of the following is not a legal constraint for a CREATE TABLE command ?

  1. Primary key
  2. Foreign key
  3. Unique
  4. Distinct

Answer

Distinct

Reason — The legal constraints for a CREATE TABLE command include the Primary key constraint, Foreign key constraint, Unique constraint, Check constraint, Default constraint. However, the Distinct is not a valid option for a CREATE TABLE command.

Fill in the Blanks

Question 1

A database can be opened with USE <database> command.

Question 2

To list the names of existing database, you can use SHOW DATABASES command.

Question 3

A constraint is a condition or check applicable on a field or a set of fields.

Question 4

The REFERENCES constraint creates a foreign key.

Question 5

Issue COMMIT command to make changes to a table permanent.

Question 6

To increase the size of a column in an existing table, use command ALTER TABLE.

Question 7

To remove table data as well table structure, use command DROP TABLE.

Question 8

To define a column as a primary key, primary key constraint is used in CREATE TABLE.

True/False Questions

Question 1

Constraints can be defined with CREATE TABLE command.

Answer

True

Reason — In SQL, the CREATE TABLE command is used to define a new table in SQL, and it allows to define the columns of the table along with any integrity constraints such as primary keys, foreign keys, unique constraints, etc.

Question 2

Constraints can only be defined with CREATE TABLE command.

Answer

False

Reason — Constraints can be defined with the CREATE TABLE command in SQL, but they can also be added or altered later using the ALTER TABLE command. The ALTER TABLE command allows to modify an existing table by adding, modifying, or dropping columns, as well as adding an integrity constraints.

Question 3

Unique and Primary key constraints are the same.

Answer

False

Reason — UNIQUE and PRIMARY KEY constraints are not the same and there are differences between them. Though both ensure unique values for each row in a column, but UNIQUE allows NULL values whereas PRIMARY KEY does not. Also, there can exist multiple columns with UNIQUE constraints in a table, but there can exist only one column or one combination with PRIMARY KEY constraint.

Question 4

DELETE from <table> command is same as DROP TABLE command.

Answer

False

Reason — DELETE from <table> command is used to remove all the contents from the table, leaving it empty. On the other hand, the DROP TABLE command is used to delete the table from the database along with all its data and structure.

Question 5

Conditional updates in table data are possible through UPDATE command.

Answer

True

Reason — Conditional updates in table data are possible through the UPDATE command in SQL. The UPDATE command specifies the rows to be changed using the WHERE clause and the new data using the SET keyword. This enables to perform updates selectively based on specific conditions defined in the WHERE clause.

Assertions and Reasons

Question 1

Assertion. The PRIMARY KEY and UNIQUE constraints are the same.

Reason. The columns with PRIMARY KEY or UNIQUE constraints have unique values for each row.

Answer

(d)

Assertion is false but Reason is true.

Explanation
UNIQUE and PRIMARY KEY constraints are not the same and there are differences between them. Though both ensure unique values for each row in a column, but UNIQUE allows NULL values whereas PRIMARY KEY does not. Also, there can exist multiple columns with UNIQUE constraints in a table, but there can exist only one column or one combination with PRIMARY KEY constraint.

Question 2

Assertion. The treatment of NULL values is different with PRIMARY KEY and UNIQUE constraints.

Reason. The column(s) with PRIMARY KEY do not allow the NULL value even in a single row but UNIQUE constraint allows NULL for one of the rows.

Answer

(a)

Both Assertion and Reason are true and Reason is the correct explanation of Assertion.

Explanation
The treatment of NULL values is different with PRIMARY KEY and UNIQUE constraints. The UNIQUE constraint allows NULL values for one of the rows, while the PRIMARY KEY does not allow the NULL value in any row.

Question 3

Assertion. There is no restriction on the number of columns that can have PRIMARY KEY constraint or UNIQUE constraints.

Reason. There can be multiple columns with UNIQUE constraint but PRIMARY KEY constraint can be defined only once for one or more columns.

Answer

(d)

Assertion is false but Reason is true.

Explanation
There can exist multiple columns with UNIQUE constraints in a table, but there can exist only one column or one combination with PRIMARY KEY constraint.

Question 4

Assertion. There are different commands for creating and changing table design.

Reason. The CREATE TABLE command creates the tables while ALTER TABLE command changes the design of an existing table.

Answer

(a)

Both Assertion and Reason are true and Reason is the correct explanation of Assertion.

Explanation
The CREATE TABLE command is used to create tables in a database, specifying the table's structure, including column names, data types, and constraints. Conversely, the ALTER TABLE command is used to modify the structure of an existing table, such as adding, removing, or modifying columns, constraints, or indexes.

Question 5

Assertion. Both DELETE and DROP TABLE carry out the same thing — deletion in tables.

Reason. The DELETE command deletes the rows and DROP TABLE deletes the whole table.

Answer

(d)

Assertion is false but Reason is true.

Explanation
The DELETE command removes rows from a table while leaving the table structure intact. It does not delete the entire table, instead, it removes specific rows within it. On the other hand, the DROP TABLE command in SQL deletes a table from the database, including its structure and all its data. So, while both commands involve deletion, they operate on different levels: the DELETE command removes rows, while the DROP TABLE command removes the entire table.

Question 6

Assertion. Both UPDATE and ALTER TABLE commands are similar.

Reason. The UPDATE command as well ALTER TABLE command make changes in the table.

Answer

(e)

Both Assertion and Reason are false.

Explanation
The UPDATE command specifies the rows to be changed using the WHERE clause and the new data using the SET keyword. On the other hand, the ALTER TABLE command is used to change the definitions of existing tables. It can add columns, integrity constraints, and redefine columns. While both commands involve making changes, they operate on different aspects of the table - UPDATE command modifies data, while ALTER TABLE command modifies the table structure.

Type A: Short Answer Questions/Conceptual Questions

Question 1

What are different divisions of SQL and commands ? Give examples of commands in each division.

Answer

SQL commands can be divided into following categories :

  1. Data Definition Language (DDL) commands — CREATE, ALTER, DROP, TRUNCATE etc.
  2. Data Manipulation Language (DML) commands — INSERT, UPDATE, DELETE etc.
  3. Transaction Control Language (TCL) commands — COMMIT, ROLLBACK, SAVEPOINT etc.
  4. Session Control Commands
  5. System Control Commands

Question 2

What is foreign key ? How do you define a foreign key in your table ?

Answer

A non-key attribute, whose values are derived from the primary key of some other table, is known as foreign key in its current table. Defining a foreign key in a table involves specifying the relationship between the tables and setting up rules for data integrity. When two tables are related by a common column or set of columns, the related column(s) in the parent table (or primary table) should be either declared as a primary key or unique key. Meanwhile, the related column(s) in the child table (or related table) should have a foreign key constraint referencing the primary or unique key in the parent table.

Question 3

How is FOREIGN KEY commands different from PRIMARY KEY command ?

Answer

The PRIMARY KEY is a set of one or more attributes that can uniquely identify tuples within the relation. A primary key column cannot contain NULL values, and it must have unique values for each row. Only one primary key constraint can exist per table. Conversely, the FOREIGN KEY command establishes a relationship between two tables by linking a column or set of columns in one table to the primary key or a unique key in another table. It enforces referential integrity, ensuring that values in the foreign key column(s) of the referencing table match values in the referenced table's primary key or unique key column(s). A foreign key can allow NULL values, indicating that the relationship is optional. Multiple foreign key constraints can exist in a table, each referencing a different parent table.

Question 4

How is FOREIGN KEY commands related to the PRIMARY KEY ?

Answer

FOREIGN KEY commands establish relationships between tables by linking columns in one table to the PRIMARY KEY or a unique key in another table. This linkage ensures referential integrity, meaning that values in the FOREIGN KEY column(s) of the referencing table must match values in the PRIMARY KEY or unique key column(s) of the referenced table. Therefore, FOREIGN KEY commands are directly related to PRIMARY KEY commands as they rely on the unique identification provided by PRIMARY KEY constraints in other tables.

Question 5

How do you enforce business rules on a database ?

Answer

Database constraints enforce business rules on a database. These include PRIMARY KEY for unique identifiers, FOREIGN KEY for maintaining relationships between tables, UNIQUE for ensuring uniqueness, CHECK constraint limit values that can be inserted into a column of a table, and default constraints are utilized to specify a default value for a column when no value is explicitly provided during an insert operation.

Question 6

What are table constraints ? What are column constraints ? How are these two different ?

Answer

Table constraints are rules or conditions applied to an entire table in a database. They are defined when creating or altering a table's schema.

Column constraints are rules or conditions applied to individual columns within a database table. They are specified at the column level when creating or altering a table's schema.

The difference between the two is that column constraints apply only to individual columns, whereas table constraints apply to groups of one or more columns.

Question 7

What is default value ? How do you define it ? What is the default value of column for which no default value is define ?

Answer

A default value is a predefined value assigned to a column in a database table. It can be specified using the DEFAULT clause when defining the table's schema. If no default value is defined for a column, and a new row is inserted into the table without providing a value for that column, the column's default value will be NULL, unless the column is defined with a NOT NULL constraint. In such cases, an error will occur if a value is not provided.

Question 8(i)

Differentiate between DROP TABLE, DROP DATABASE.

Answer

DROP TABLEDROP DATABASE
This command is used to delete a specific table from the database along with all its data, indexes, triggers, and constraints.This command is used to delete an entire database including all its tables, views, stored procedures, triggers, and other objects.
The syntax is : DROP TABLE table_name;.The syntax is : DROP DATABASE database_name;.

Question 8(ii)

Differentiate between DROP TABLE, DROP clause of ALTER TABLE.

Answer

DROP TABLEDROP clause of ALTER TABLE
This command is used to delete a specific table from the database along with all its data, indexes, triggers, and constraints.This command is used to remove a specific component of a table, such as columns, constraints, or indexes.
The syntax is : DROP TABLE table_name;The syntax is : ALTER TABLE table_name DROP COLUMN column_name;

Type B: Application Based Questions

Question 1

Insert all those records of table Accounts into table Pending where amt_outstanding is more than 10000.

Answer

INSERT INTO Pending
SELECT * FROM Accounts
WHERE amt_outstanding > 10000;

Question 2

Increase salary of employee records by 10% (table employee).

Answer

Table employee

IDFirst_NameLast_NameUser_IDSalary
1DimJosephJdim5000
2JaganathMishrajnmishra4000
3SiddharthMishrasmishra8000
4ShankarGirisgiri7000
5GautamBuddhabgautam2000
UPDATE employee
SET Salary = (Salary * 0.1) + Salary ;
Output

To view all the details (all columns and rows) of the "employee" table the below query is executed :

SELECT * FROM employee ;
+----+------------+-----------+----------+--------+
| ID | First_Name | Last_Name | User_ID  | Salary |
+----+------------+-----------+----------+--------+
|  1 | Dim        | Joseph    | Jdim     |   5500 |
|  2 | Jaganath   | Mishra    | jnmishra |   4400 |
|  3 | Siddharth  | Mishra    | smishra  |   8800 |
|  4 | Shankar    | Giri      | sgiri    |   7700 |
|  5 | Gautam     | Buddha    | bgautam  |   2200 |
+----+------------+-----------+----------+--------+

Question 3

Give commission of Rs.500 to all employees who joined in year 1982 (table Empl).

Answer

Table Empl

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
8369SMITHCLERK89021990-12-18800NULL20
8499ANYASALESMAN86981991-02-20160030030
8521SETHSALESMAN86981991-02-22125050030
8566MAHADEVANMANAGER88391991-04-022985NULL20
8654MOMINSALESMAN86981991-09-281250140030
8698BINAMANAGER88391991-05-012850NULL30
8839AMIRPRESIDENTNULL1991-11-185000NULL10
8844KULDEEPSALESMAN86981991-09-081500030
8882SHIAVNSHMANAGER88391991-06-092450NULL10
8886ANOOPCLERK88881993-01-121100NULL20
8888SCOTTANALYST85661992-12-093000NULL20
8900JATINCLERK86981991-12-03950NULL30
8902FAKIRANALYST85661991-12-033000NULL20
8934MITACLERK88821992-01-231300NULL10
UPDATE Empl
SET COMM = 500
WHERE YEAR(HIREDATE) = 1982;
Explanation

Since there are no employees who joined in the year 1982 according to the data provided in the "Empl" table, executing the given SQL query will result in no changes to the "COMM" column.

Question 4

Allocate the department situated in BOSTON to employee with employee number 7500 (tables EMPL, Dept)

Answer

UPDATE EMPL
SET DEPTNO = (
    SELECT DEPTNO
    FROM Dept
    WHERE LOC = 'BOSTON'
)
WHERE EMPNO = 7500;

Question 5

Given the following tables :

Orders (OrdNo, Ord_date, ProdNo#, Qty) 
Product (ProdNo, Descp, Price)
Payment (OrdNo, Pment)

Write a query to delete all those records from table Orders whose complete payment has been made.

Answer

DELETE FROM Orders
WHERE OrdNo IN (
    SELECT Payment.OrdNo
    FROM Payment
    WHERE Payment.Pment = 'COMPLETE');

Question 6

Enlist the names of all tables created by you.

Answer

SHOW TABLES ;

Question 7

Write Query statements for following transaction : (Consider tables of question 12)

  1. Increase price of all products by 10%.
  2. List the details of all orders whose payment is pending as per increased price.
  3. Decrease prices by 10% for all those products for which orders were placed 10 months before.

Answer

The following tables are considered :

Orders (OrdNo, Ord_date, ProdNo#, Qty) 
Product (ProdNo, Descp, Price)
Payment (OrdNo, Pment)

1.

UPDATE product
SET price = (price * 0.1) + price ;

2.

SELECT *
FROM Orders
JOIN Payment ON Orders.OrdNo = Payment.OrdNo
WHERE Payment.Pment = 'Pending';

3.

UPDATE Product
SET Price = Price - (Price * 0.1)
WHERE ProdNo IN (
    SELECT ProdNo#
    FROM Orders
    WHERE YEAR(Ord_date) = YEAR(CURDATE()) - 1 
    AND MONTH(Ord_date) = MONTH(CURDATE()) - 10
);

Question 8

Modify table Empl, add another column called Grade of VARCHAR type, size 1 into it.

Answer

ALTER TABLE Empl
ADD (Grade VARCHAR(1)) ;

Question 9

In the added column Grade, assign grades as follows :

if sal is in range 700 — 1500, Grade is 1 ;
if sal is in range 1500 — 2200, Grade is 2 ;
if sal is in range 2200 — 3000, Grade is 3 ;
if sal is in range 3000 — Grade is 4 ;

Answer

UPDATE Empl
SET Grade = '1'
WHERE Sal >= 700 AND Sal <= 1500;

UPDATE Empl
SET Grade = '2'
WHERE Sal > 1500 AND Sal <= 2200;

UPDATE Empl
SET Grade = '3'
WHERE Sal > 2200 AND Sal <= 3000;

UPDATE Empl
SET Grade = '4'
WHERE Sal > 3000;

Question 10

Add a constraint (NN-Grade) in table Empl that declares column Grade not null.

Answer

ALTER TABLE Empl
ADD CONSTRAINT NN_Grade 
(Grade NOT NULL) ;

Question 11

Insert a record of your choice in table Empl. Make sure not to enter Grade.

Answer

INSERT INTO Empl (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES (12345, 'DEEPAK', 'CLERK', 8902, '1990-12-18', 8000, 200, 10);

Question 12

Modify the definition of column Grade. Increase its size to 2.

Answer

ALTER TABLE Empl
MODIFY (Grade VARCHAR(2)) ; 

Question 13

Drop the table Empl.

Answer

DROP TABLE IF EXISTS Empl;

Question 14

Create the table Department table based on the following table instance chart.

Column NameIDName
Data TypeNUMBERVARCHAR
Length825

Answer

CREATE TABLE Department (
ID NUMBER(8),
Name VARCHAR(25)
);

Question 15

Populate the table Department with data from table dept. Including only required columns.

Answer

INSERT INTO Department (ID, Name)
SELECT ID, Name
FROM dept ;

Question 16

Create the table Employee based on the following table instance chart.

Column NameData TypeLength
IDNUMBER8
First_NameVARCHAR25
Last_NameVARCHAR25
Dept_IDNUMBER8

Answer

CREATE TABLE Employee (
ID NUMBER(8),
First_Name VARCHAR(25),
Last_Name VARCHAR(25),
Dept_ID NUMBER(8)
);

Question 17

Drop table Employee and Department.

Answer

DROP TABLE IF EXISTS Employee ;
DROP TABLE IF EXISTS Department ;

Question 18

Create table Customer as per following Table Instance Chart.

Column NameData TypeLength
Cust_IDNUMBER7
Cust_NameVARCHAR30
Cust_Address1VARCHAR20
Cust_Address2VARCHAR30
PincodeNUMBER6
Cust_PhoneVARCHAR10

Answer

CREATE TABLE Customer (
Cust_ID NUMBER(7),
Cust_Name VARCHAR(30),
Cust_Address1 VARCHAR(20),
Cust_Address2 VARCHAR(30),
Pincode NUMBER(6),
Cust_Phone VARCHAR(10)
);

Question 19

Add one column Email of data type VARCHAR and size 30 to the table Customer.

Answer

ALTER TABLE Customer
ADD (Email VARCHAR(30)) ;

Question 20

Add one more column CustomerIncomeGroup of datatype VARCHAR(10).

Answer

ALTER TABLE Customer
ADD (CustomerIncomeGroup VARCHAR(10));

Question 21

Insert few records with relevant information, in the table.

Answer

INSERT INTO Customer (Cust_ID, Cust_Name, Cust_Address1, Cust_Address2, Pincode, Cust_Phone, Email, CustomerIncomeGroup)
VALUES
(11, 'Amit', '1st Main Street', 'Mumbai', 12345, '5551234121', 'amit@gmail.com', 'High'),
(24, 'Vidya', '4th Main Street', 'Bangalore', 54321, '5234325678', 'vidya24@gmail.com', 'Medium'),
(39, 'Amruta', '78th Main Street', 'Goa', 98765, '5976539012', 'amruta78@gmail.com', 'Low');

Question 22

Drop the column CustomerIncomeGroup from table Customer.

Answer

ALTER TABLE Customer
DROP COLUMN CustomerIncomeGroup ;

Question 23

Create table Department as per following Table Instance Chart.

Column NameDeptIDDeptName
Key TypePrimary 
Nulls/Unique NOT NULL
DatatypeNUMBERVARCHAR
Length220

Answer

CREATE TABLE Department (
    DeptID NUMBER(2) PRIMARY KEY,
    DeptName VARCHAR(20) NOT NULL
);

Question 24

Create table Employee as per following Table Instance Chart.

Column NameEmpIDEmpNameEmpAddressEmpPhoneEmpSalDeptID
Key TypePrimary    Foreign
Nulls/Unique NOT NULL    
Fk Table     Department
Fk Column     Dept_ID
DatatypeNUMBERVARCHARVARCHARVARCHARNUMBERVARCHAR
Length62030109, 22

Answer

CREATE TABLE Employee (
    EmpID NUMBER(6) PRIMARY KEY,
    EmpName VARCHAR(20) NOT NULL,
    EmpAddress VARCHAR(30),
    EmpPhone VARCHAR(10),
    EmpSal NUMBER(9, 2),
    DeptID VARCHAR(2),
    FOREIGN KEY (DeptID) REFERENCES Department (Dept_ID)
    ON DELETE CASCADE ON UPDATE CASCADE
);

Question 25

View structures of all tables created by you.

Answer

DESCRIBE <TABLENAME> ;
PrevNext