Dbms Unit 3 Part1
Dbms Unit 3 Part1
SQL Commands
INSERT
CREATE GRANT
SELECT COMMIT
UPDATE
ALTER
DELETE
DROP REVOKE ROLLBACK
MERGE
RENAME
CALL
SAVEPOINT
TRUNCATE
COMMENT
Basic SQL query
DDL commands
• Data Definition Language (DDL) or Schema Definition Language,
statements are used to define the database structure or schema.
• These statements handle the design and storage of database
objects.
CREATE:
CREATE DATABASE:
The CREATE DATABASE statement is used to create a new SQL
database.
Syntax:
CREATE DATABASE databasename;
Example :
• The following SQL statement creates a database called "testDB":
CREATE TABLE:
The CREATE TABLE statement is used to create a new table in a
database.
Syntax:
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
Field indicates the column name, Type is the data type for the
column, NULL indicates whether the column can contain NULL
values, Key indicates whether the column is indexed, and Default
specifies the column's default value. Extra displays special
information about columns
ALTER TABLE:
The ALTER TABLE statement is used to add, delete, or modify columns
in an existing table. The ALTER TABLE statement is also used to add
and drop various constraints on an existing table.
ALTER TABLE - ADD Column
- To add a column in a table.
Syntax:
ALTER TABLE table_name ADD column_name datatype ;
Example : The following SQL adds an "Email" column to the "Persons"
table:
ALTER TABLE Persons ADD Email varchar(255) ;
Add Multiple Columns in table:
ALTER TABLE table_name ADD ( column_name1 datatype[(size)],
column_name2 datatype[(size)], ... );
Example:
ALTER table persons change city district varchar(50);
RENAME:
RENAME command is used to set a new name for
any existing table.
Syntax (Rename Table):
RENAME TABLE old_table_name to new_table_name ;
Example:
RENAME TABLE persons to students_info;
Example:
TRUNCATE TABLE Persons;
OR
ID Name
1 Ram
2 Sam
3 Henry
4 Adam
5 Alice
UPDATE
UPDATE Query is used to modify the existing records in a table. The WHERE clause with the
UPDATE query to update the selected rows, otherwise all the rows would be affected.
Syntax:
UPDATE table_name SET column1 = value1, column2 = value2...., columnN =
valueN WHERE [condition];
Example:
Update the ADDRESS for a customer whose ID number is 5 in the
table.
UPDATE person SET ADDRESS = “LONDON” WHERE ID = 5;
Now, the PERSON table would have the following records,
ID Name Age Address SALARY
1 Ram 32 Chnnai 2000
2 Sam 28 Hyd 2500
3 Henry 30 Banglore 5000
4 Adam 29 Pune 6500
5 Alice 25 LONDON 8500
To modify all the ADDRESS and the SALARY column values in the Person
table, not need to use the WHERE clause as the UPDATE query would be
enough as shown in the following code block.
UPDATE person SET ADDRESS = “LONDON” , SALARY = 5000;
DELETE
DELETE Query is used to delete the existing records from a table.
You can use the WHERE clause with a DELETE query to delete the
selected rows, otherwise all the records would be deleted.
Syntax:
DELETE FROM table_name WHERE [condition];
To DELETE all the records from the Person table, no need to use the WHERE clause and the
DELETE query would be as follows –
DELETE FROM person;
UNION
The UNION operator is used to combine the
result-set of two or more SELECT statements.
• Each SELECT statement within UNION must
have the same number of columns.
• The columns must also have similar data
types.
• The columns in each SELECT statement must
also be in the same order
INTERSECT
The SQL INTERSECT clause/operator is used to combine two SELECT statements. But it
returns only common rows returned by the two SELECT statements.
Syntax:
Except
The SQL EXCEPT clause/operator is used to combine two SELECT statements
and returns rows from the first SELECT statement that are not returned by
the second SELECT statement. This means EXCEPT returns only rows, which
are not available in the second SELECT statement.
Syntax :
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition] EXCEPT
SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE condition]
Nested Query
• A Subquery or Inner query or a Nested query is a query within
another SQL query and embedded within the WHERE clause.
• A subquery is used to return data that will be used in the main
query as a condition to further restrict the data to be retrieved.
• Subqueries can be used with the SELECT, INSERT, UPDATE, and
DELETE statements along with the operators like =, , >=, <=, IN,
BETWEEN, etc.
• There are a few rules that subqueries must follow −
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause,
unless multiple columns are in the main query for the
subquery to compare its selected columns.
• An ORDER BY command cannot be used in a subquery,
although the main query can use an ORDER BY. The GROUP
BY command can be used to perform the same function as
the ORDER BY in a subquery.
• Subqueries that return more than one row can only be used
with multiple value operators such as the IN operator.
• A subquery cannot be immediately enclosed in a set function.
• The BETWEEN operator cannot be used with a subquery.
However, the BETWEEN operator can be used within the
subquery.
Subqueries with the SELECT Statement
Subqueries are most frequently used with the SELECT statement. The
basic syntax is as follows
Syntax:
Example,
Select count(*) from emp;
5
Min Function
• The MIN function returns the smallest value in the specified table
field.
Example:
Select min(salary) from emp;
10000
Max Function
SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY FROM CUSTOMERS WHERE SALARY IS NULL;
This would produce the following result −
101 8 10 90 0
mysql> create trigger t1
before insert
on Sales
for each row
begin
set new.total=new.qtysold*new.price;
end;
mysql> insert into Sales values(102,9,10,180,0); mysql> select * from
Sales;
sid itemid qtysold price total
101 8 10 90 0
102 9 10 180 1800