Which command is used for creating tables ?
Answer
In SQL, CREATE TABLE
command is used for creating tables in database.
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:
- Unique constraint.
- Primary key constraint.
- Default constraint.
- Check constraint.
- Foreign key constraint.
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.
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.
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.
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.
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.
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 ;
.
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 ;
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.
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 ;
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. |
Consider the following SQL statement. What type of statement is this ?
CREATE TABLE employee (name VARCHAR, id INTEGER)
- DML
- DDL
- DCL
- 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
.
The data types CHAR(n)
and VARCHAR(n)
are used to create ..............., and ............... length types of string/text fields in a database.
- Fixed, equal
- Equal, variable
- Fixed, variable
- 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.
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.
- 3, 20
- 20, 4
- 20, 20
- 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.
Consider the following SQL statement. What type of statement is this ?
INSERT INTO instructor VALUES (10211, 'Shreya' , 'Biology', 66000 ) ;
- Procedure
- DML
- DCL
- 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.
In the given query which keyword has to be inserted ?
INSERT INTO employee ............... (1002, Kausar, 2000) ;
- Table
- Values
- Relation
- 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.
Which of the following is/are the DDL statements ?
- Create
- Drop
- Alter
- 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.
In SQL, which command(s) is(are) used to change a table's structure / characteristics ?
- ALTER TABLE
- MODIFY TABLE
- CHANGE TABLE
- 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.
Which of the following commands will delete the table from MYSQL database ?
- DELETE TABLE
- DROP TABLE
- REMOVE TABLE
- 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.
............... defines rules regarding the values allowed in columns and is the standard mechanism for enforcing database integrity.
- Column
- Constraint
- Index
- 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.
Fill in the blank :
............... command is used to remove primary key from a table in SQL.
- update
- remove
- alter
- 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 ;
.
Which command defines its columns, integrity constraint in create table :
- Create table command
- Drop table command
- Alter table command
- 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.
Which command is used for removing a table and all its data from the database :
- Create table command
- Drop table command
- Alter table command
- 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.
Which of the following is not a DDL command ?
- UPDATE
- TRUNCATE
- ALTER
- 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.
Which of the following is not a legal constraint for a CREATE TABLE command ?
- Primary key
- Foreign key
- Unique
- 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.
A database can be opened with USE <database> command.
To list the names of existing database, you can use SHOW DATABASES command.
A constraint is a condition or check applicable on a field or a set of fields.
The REFERENCES constraint creates a foreign key.
Issue COMMIT command to make changes to a table permanent.
To increase the size of a column in an existing table, use command ALTER TABLE.
To remove table data as well table structure, use command DROP TABLE.
To define a column as a primary key, primary key constraint is used in CREATE TABLE.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
What are different divisions of SQL and commands ? Give examples of commands in each division.
Answer
SQL commands can be divided into following categories :
- Data Definition Language (DDL) commands — CREATE, ALTER, DROP, TRUNCATE etc.
- Data Manipulation Language (DML) commands — INSERT, UPDATE, DELETE etc.
- Transaction Control Language (TCL) commands — COMMIT, ROLLBACK, SAVEPOINT etc.
- Session Control Commands
- System Control Commands
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.
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.
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.
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.
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.
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.
Differentiate between DROP TABLE, DROP DATABASE.
Answer
DROP TABLE | DROP 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; . |
Differentiate between DROP TABLE, DROP clause of ALTER TABLE.
Answer
DROP TABLE | DROP 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; |
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;
Increase salary of employee records by 10% (table employee).
Answer
Table employee
ID | First_Name | Last_Name | User_ID | Salary |
---|---|---|---|---|
1 | Dim | Joseph | Jdim | 5000 |
2 | Jaganath | Mishra | jnmishra | 4000 |
3 | Siddharth | Mishra | smishra | 8000 |
4 | Shankar | Giri | sgiri | 7000 |
5 | Gautam | Buddha | bgautam | 2000 |
UPDATE employee
SET Salary = (Salary * 0.1) + Salary ;
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 |
+----+------------+-----------+----------+--------+
Give commission of Rs.500 to all employees who joined in year 1982 (table Empl).
Answer
Table Empl
EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
---|---|---|---|---|---|---|---|
8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800 | NULL | 20 |
8499 | ANYA | SALESMAN | 8698 | 1991-02-20 | 1600 | 300 | 30 |
8521 | SETH | SALESMAN | 8698 | 1991-02-22 | 1250 | 500 | 30 |
8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985 | NULL | 20 |
8654 | MOMIN | SALESMAN | 8698 | 1991-09-28 | 1250 | 1400 | 30 |
8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850 | NULL | 30 |
8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000 | NULL | 10 |
8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500 | 0 | 30 |
8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450 | NULL | 10 |
8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100 | NULL | 20 |
8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000 | NULL | 20 |
8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950 | NULL | 30 |
8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000 | NULL | 20 |
8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300 | NULL | 10 |
UPDATE Empl
SET COMM = 500
WHERE YEAR(HIREDATE) = 1982;
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.
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;
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');
Enlist the names of all tables created by you.
Answer
SHOW TABLES ;
Write Query statements for following transaction : (Consider tables of question 12)
- Increase price of all products by 10%.
- List the details of all orders whose payment is pending as per increased price.
- 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
);
Modify table Empl, add another column called Grade of VARCHAR type, size 1 into it.
Answer
ALTER TABLE Empl
ADD (Grade VARCHAR(1)) ;
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;
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) ;
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);
Modify the definition of column Grade. Increase its size to 2.
Answer
ALTER TABLE Empl
MODIFY (Grade VARCHAR(2)) ;
Drop the table Empl.
Answer
DROP TABLE IF EXISTS Empl;
Create the table Department table based on the following table instance chart.
Column Name | ID | Name |
---|---|---|
Data Type | NUMBER | VARCHAR |
Length | 8 | 25 |
Answer
CREATE TABLE Department (
ID NUMBER(8),
Name VARCHAR(25)
);
Populate the table Department with data from table dept. Including only required columns.
Answer
INSERT INTO Department (ID, Name)
SELECT ID, Name
FROM dept ;
Create the table Employee based on the following table instance chart.
Column Name | Data Type | Length |
---|---|---|
ID | NUMBER | 8 |
First_Name | VARCHAR | 25 |
Last_Name | VARCHAR | 25 |
Dept_ID | NUMBER | 8 |
Answer
CREATE TABLE Employee (
ID NUMBER(8),
First_Name VARCHAR(25),
Last_Name VARCHAR(25),
Dept_ID NUMBER(8)
);
Drop table Employee and Department.
Answer
DROP TABLE IF EXISTS Employee ;
DROP TABLE IF EXISTS Department ;
Create table Customer as per following Table Instance Chart.
Column Name | Data Type | Length |
---|---|---|
Cust_ID | NUMBER | 7 |
Cust_Name | VARCHAR | 30 |
Cust_Address1 | VARCHAR | 20 |
Cust_Address2 | VARCHAR | 30 |
Pincode | NUMBER | 6 |
Cust_Phone | VARCHAR | 10 |
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)
);
Add one column Email of data type VARCHAR and size 30 to the table Customer.
Answer
ALTER TABLE Customer
ADD (Email VARCHAR(30)) ;
Add one more column CustomerIncomeGroup of datatype VARCHAR(10).
Answer
ALTER TABLE Customer
ADD (CustomerIncomeGroup VARCHAR(10));
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');
Drop the column CustomerIncomeGroup from table Customer.
Answer
ALTER TABLE Customer
DROP COLUMN CustomerIncomeGroup ;
Create table Department as per following Table Instance Chart.
Column Name | DeptID | DeptName |
---|---|---|
Key Type | Primary | |
Nulls/Unique | NOT NULL | |
Datatype | NUMBER | VARCHAR |
Length | 2 | 20 |
Answer
CREATE TABLE Department (
DeptID NUMBER(2) PRIMARY KEY,
DeptName VARCHAR(20) NOT NULL
);
Create table Employee as per following Table Instance Chart.
Column Name | EmpID | EmpName | EmpAddress | EmpPhone | EmpSal | DeptID |
---|---|---|---|---|---|---|
Key Type | Primary | Foreign | ||||
Nulls/Unique | NOT NULL | |||||
Fk Table | Department | |||||
Fk Column | Dept_ID | |||||
Datatype | NUMBER | VARCHAR | VARCHAR | VARCHAR | NUMBER | VARCHAR |
Length | 6 | 20 | 30 | 10 | 9, 2 | 2 |
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
);
View structures of all tables created by you.
Answer
DESCRIBE <TABLENAME> ;