Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CHAPTER – 13 – TABLE CREATION AND DATA MANIPULATION COMMANDS

TYPE – A – SHORT ANSWER QUESTIONS / CONCEPTUAL QUESTIONS

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

2. What is foreign key? How do your define a foreign key in your table?
3. How is FOREIGN KEY commands different from PRIMARY KEY command?

4. How is FOREIGN KEY commands related to the PRIMARY KEY?

5. How do you enforce business rules on a database?


A database system needs to be able to enforce business rules. Referential integrity
(foreign key cobstraint) is one way oracle provides for maintaining business rules.
Relational systems allow control of business rules with constraints, and referential
integrity rules form the backbone of relational tables.

6. What are table constraints? What are column constraints? How are these are
different?

7. What is default value? How do you define it? What is the default value of column for
which no default is define?
Default values, in the context of databases, are preset values defined for a column type. Default values are
used when many records hold similar data.
The DEFAULT constraint is used to set a default value for a column. The default value will be added to all new
records, if no other value is specified.
If you specify no default value for a column, the default is NULL unless you place a NOT NULL constraint on the
column.
8. Differentiate between
a. DROP TABLE DROP DATABASE
b. DROP TABLE DROP clause of ALTER TABLE

TYPE – B – SHORT ANSWER QUESTIONS / CONCEPTUAL QUESTIONS

Q1. Insert all those records of TABLE Accounts into TABLE Pending WHERE amt_outstanding is more
than 10000.Query if you have already CREATEd TABLE Pending :-

Query if you have already created table Pending :-


Insert into Pending
Select * from Accounts where amt_outstanding > 10000 ;

Query if you have not created Pending table in your database :-


Create table Pending Select * from Accounts where amt_outstanding > 10000 ;
2. Increase salary of employee records by 10% (table employee).
Update employee
Set salary = salary + salary * 0.10
Q3. Give commission of Rs. 500 to all employees who joined in year 1982 (TABLE Empl).
TABLE ---

UPDATE Empl
SET comm = comm + 500
WHERE hiredate between “1982-01-01” and “1982-12-31” ;
4. Allocate the department situated in BOSTON to employee with employee number 7500 (TABLEs EMPL,
Dept).
TABLE ----

ALTER TABLE Empl


Add (city char( 50 ) ) ;
UPDATE Empl
SET city = “BOSTON”
WHERE empno = 7500;

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.
DELETE FROM Orders
WHERE Orders.OrdNo = Payment.OrdNo and
Orders.ProdNo=Product.ProdNoAND Payment.Pment = Orders.Qty *
Product.Price ;
(At first, I have to make a condition to linking the order, payment, and product TABLE then make another condition
that willcheck whether is payment done by the customer is equal to the price of the product * quantity.)

6. Enlist the names of all TABLEs CREATEd by you.


SHOW TABLEs ;

7. Write Query statements for following transaction : (Consider TABLEs


of question
(i). Increase price of all products by 10%.
(ii). List the details of all orders whose payment is pending as per increased price.
(iii). Decrease prices by 10% for all those products for which orders were placed 10
months before.

(i).UPDATE product SET price = price + price * 0.10


(ii) SELECT * FROM orders, product, payment WHERE orders.OrdNo = Payment.OrdNo and
ProdNo# = ProdNo and ( Qty * Price) < Pment ;
(iii) UPDATE Product SET Price=Price - Price*0.1
WHERE Product.ProdNo=Orders.ProdNo AND DATEDIFF(month, Orders.Ord_date, GETDATE())>=10;
8. Modify TABLE Empl, add another column called Grade of VARCHAR type,
size 1 into it.
TABLE –

ALTER TABLE Empl ADD COLUMN grade varchar(1) ;

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
Table ---

update Empl
set grade = 1
where sal between 700 and 1500 ;

update Empl
set grade = 2
where sal between 1500 and 2200 ;

update Empl
set grade = 3
where sal between 2200 and 3000 ;

update Empl
set grade = 4
where sal = 3000 ;
10. Add a constraint (NN-Grade) in table Empl that declares column Grade not null.

Alter table Empl add column grade int(1) Not null ;

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

Insert into Empl(empno , ename , job , mgr , hiredate ,sal ,comm , deptno ) values( 8935, “Path wala ”,
“SALESMAN”,8862 , “1993-06-25” ,1000 , 110.00, 10 )
NOTE:- It will return error because we did not enter Grade. grade should have a value
12. Modify the definition of column Grade. Increase its size to 2.

Alter table Emple modify column grade int(2) not null ;


13. Drop the table Empl.

Drop table Empl ;


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

Create table department (id int(8) , name varchar (25) ) ;


15. Populate the table Department with data from table dept. Including only required columns.
Insert into Department()
Select id, name from dept ;
16. Create the table Employee based un the following table instance chart.
Table --

Create table employee (


Id int(8) ,
First_name varchar(25) ,
Last_name varchar(25),
Dept_id int(8)
);
17. Drop table Employee and Department.
Drop table Employee ;
Drop table Department ;
18. Create table Customer as per following Table Instance Chart.
Table --

Create table customer (


Cust_ID int (7) ,
Cust_Name varchar( 30 ) ,
Cust_Address1 varchar (20) ,
Cust_Address2 varchar (30) ,
Pincode int(6) ,
Cust_Phone varchar(10)
);
19. Add one column Email of data type VARCHAR and size 30 to the table Customer.

Alter table Customer add ( Email varchar(30) ) ;


20. Add one more column CustomerincomeGroup of datatype VARCHAR(10).

Alter table Customer add ( CustomerincomeGroup varchar(10) );


21. Insert few records with relevant information, in the table.

Insert into Customer values ( 1234, “Path wala ”, “india”, “up” , 211001 , 1234567890) ;
Insert into Customer values ( 1597, “Portal express”, “india”, “hp” , 245901 , 2468135790) ;
22. Drop the column CustomerlncomeGroup from table Customer.

Alter table Customer


Drop column CustomerlncomeGroup ;
23. Create table Department as per following Table Instance Chart.

Create table department (


DeptID int( 2 ) primary key ,
DeptName varchar (20) Not null
);
24. Create table Employee as per following Table Instance Chart.

Create table Employee (


EmpID int(6) primary key,
EmpName Varchar(20) Not null,
EmpAddress varchar (20),
EmpPhone Varchar(10),
EmpSal int(9),
DeptID Varchar(2),
Foreign key (DeptID ) References Department (Dept_ID)
);

You might also like