You are on page 1of 3

Database Management System

Practical No : 2

 
Aim : 1. Update/delete data of the table
2. Modify structure of the table
3. Add/modify/delete columns of the table
4. Create table from existing table
Sorting data in a table
Select * from <TableName> order by <ColumnName1>, <ColumnName2> <SortOrder>;

Creating a table from a table


Create table <TableName> (<ColumnName1>, <ColumnName2>) as select <ColumnName1>,
<ColumnName2> from <TableName>;

Inserting data into a table from another table


Insert into <TableName> select <ColumnName1>, <ColumnName2> from <TableName>;

Delete data from a table


Delete from <TableName>;

Updating the contents of a table


Update <TableName> set <ColumnName1>=<expression1>,<ColumnName2>=<expression2>;

Modifying the structure of tables


alter table <TableName> add (<ColumnName1><datatype1>, <ColumnName2><datatype2>);

Renaming tables
Rename <TableName> to <NewTableName>;

Truncating Tables
Truncate table <TableName>;

Destroying Tables
drop table <TableName>;

Use of logical operators in where condition

Select <ColumnName1> <ColumnName2> where <cond1> and <cond2>


Select <ColumnName1> <ColumnName2> where <cond1> or <cond2>
Select <ColumnName1> <ColumnName2> where not<cond1>

Range searching
Select <ColumnName1> <ColumnName2> where < ColumnName> between <val1> and <val2>

Pattern Matching
Select <ColumnName1> <ColumnName2> where < ColumnName> like <pattern>
WHERE EName LIKE 'a%' Finds any values that start with "a"
WHERE EName LIKE '%a' Finds any values that end with "a"
WHERE EName LIKE '%or%' Finds any values that have "or" in any position
WHERE EName LIKE '_r%' Finds any values that have "r" in the second position
WHERE EName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE EName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
'[a-c]%' starting with "a", "b", or "c":
'[bsp]%' starting with "b", "s", or "p"
'[!bsp]%' or not like '[bsp]%' NOT starting with "b", "s", or "p"
Example: WHERE SALARY LIKE '200%' => Finds any values that start with 200.

In and not in predicates


Select <ColumnName1> <ColumnName2> where < ColumnName> in (list)

 
Do as directed:
1. List all the clients who are located in Mumbai or Chennai. (Use logical operator)
Ans:
2. Find the names of salesman who have a salary equal to Rs. 3000.
3. List the salesman who are associated with orders given.
4. List the orders whose status is either cancelled or fulfilled. (Use IN)
5. List products whose selling price is greater than 500 and less than or equal to 750.
(Use logical as well as BETWEEN operator). Notice the difference.
6. List the names of all clients have ‘a’ as the second letter in their names.
7. List the products whose selling price is more than 500 with the new selling price
calculated as original selling price plus 15%.
8. List the name, city and state of clients who are not in the state of “Maharashtra”.
9. List the products with highest to lowest selling price.
10.List the orders ordered in the year 2017.
11.Change the city of Harsh to Bangalore.
12.Change the city of salesman to Pune. What is wrong in doing this type of query?
13.Delete all the salesmen whose salaries are equal to Rs. 3500.
14.Delete all the clients who live in “Tamil Nadu”.
15.Add a column called “Telephone” of data type “Number” and size “10” to the
client table.
16.Change the size of sell price to 6, 2. Observe the behavior.
17.Change the name of salesman table to sman.
18.Create a new table sales_order_new from sales_order containing data as well as
structure.
19.Create a new table order_details from sales_order_details containing only the
structure (no data).
20.Analyze the commit and rollback behavior of drop, delete and truncate command
on sales_order_new.

You might also like