Andhra Loyola Institute Of Engineering and
Submitted to: Sujatha madam Subject title: Information Technology(C108) Topic: SQL Command Set Date: 14-12-2009. Registered no: 09HP1E0039.
Submitted by: Md.Rizwan. MBA-1.
Signature of Lecturer
Signature of HOD
SQL COMMAND SET
SQL statements are divided into categories based on the function that they serve. Some experts consider these categories to be either separate languages or sublanguages. However, in SQL they all have the basic syntax and rules, so categories of statements within a single language. The categories, each of which is described in a subsequent section, are • • • • • Data Definition Language (DDL) Data Manipulation Language (DML) Data Control Language (DCL) Transaction Control Language (TCL) Data Query Language (DQL)
Data Definition Language (DDL)
Data Definition Language (DDL) includes SQL statements that allow the database user to create and modify the structure of database objects, such as tables, views, and indexes. SQL statements that use the commands CREATE, ALTER, and DROP are considered part of DDL. It is important to understand that DDL statements affect the containers that hold the data in the database rather than the data itself. So there are DDL statements to create, drop, and alter tables, but none of these statements provide the ability to create or modify rows of data in those tables.
Creates a new database object of the type named in the statement. The CREATE tab TABLE statement is one of the most fundamental in SQL. The relational paradigm requires all stored data to be anchored in a table, so the ability to store anything in the database always starts with the creation of a table. Syntax:
CREATE TABLE <table_name> (column1 data_type (size), column2
data_type (size), . . . . . . . );
CREATE TABLE statement is really simple. Each statement includes a table name
and a column and comma-separated list of one or more column definitions enclosed in a pair of parentheses. The table name must be unique with in the database. A table must have at least one column, which makes logical sense when you think about it. Example:
CREATE TABLE student (sno number(2), sname varchar2(20), JOD date);
ALTER: Once a table has been created, just about anything that was specified in the CREATE TABLE statement can be changed using the ALTER TABLE statement. While there is a bit of variation across DBMS implementations, here is a list of the type of changes usually supported by the ALTER TABLE statement, along with the general syntax for each type: Adding a column to a table. It is used to add new column to the already existing table. Syntax:
ALTER TABLE <table_name> ADD (new_column_name1
data_type(size), new_column_name2 data_type(size), . . . . . . .); Example:
ALTER TABLE student add(Fname varchar2(20));
Table altered. Changing the definition of a column. Most DBMS won’t let you decrease column precision if there is data in the table, and very few will let you change the data type of an existing column. Changing unnamed column constraints can be problematic, this is another good reason to name all your constraints. Syntax:
ALTER TABLE <table_name> MODIFY (column_name
ALTER TABLE student MODIFY(sno number(10));
Table altered. DROP: Drops (destroys) an existing database object of the type named in the statement. It is the simplest of the DDL statement. Syntax:
DROP TABLE <table_name>;
DROP TABLE student;
Data Manipulation Language (DML):
Data Manipulation Language ( DML) includes SQL statements that allow the database user to add data to the database (in the form of rows in the tables), remove data from the database, and modify existing data in the database. SQL statements that use the commands INSERT, UPDATE, and DELETE are considered part of DML. INSERT: The INSERT statement in SQL is used to add new rows of data to tables. It insert in two basic forms: one where column values are provided in the statement itself, and the other where values are given bulk by giving back slash (/). Single Row Insert Using the VALUES:
The INSERT statement that uses a VALUES can be create only one row each time it is run because the values for that one row of data are provided in the statement itself.
INSERT into <table_name> VALUES (value1, value2, . . . . . . .);
INSERT into student VALUES (1, ‘abc’, ‘05-25-2005’);
1 row created. Note: If the value is of character type specify in single codes (‘’). Syntax2:
INSERT into <table_name> (column1, column2, . . . . . . .)
VALUES(value1,value2, . . . . .); Example2:
INSERT into student (sno, sname, JOD) VALUES (1, ‘abc’, ‘05-25-20005’);
1 row created. Note: By this we can insert the values of the attributes which we want. Bulk Row Insert: The INSERT statement that takes VALUES and create many rows by giving back slash (/) every time and we can enter values. Syntax:
INSERT into <table_name> VALUES (&value1, &value2, . . . . . . .);
Note: & accepts ‘n’ number of values. Example:
INSERT into student VALUES (&sno, &sname, &JOD);
1 row created.
Note: After entering the query we can observe that by default it will ask that Enter value for sno: Enter value for sname: Enter value for JOD: Here we can directly give values. If we want again we can give ‘/’ then the previous query will activate. UPDATE: The UPDATE statement in SQL is used to update the data values for table columns listed in the statement. Updates existing database table rows. Syntax:
UPDATE <table_name> set column_name1=expression [, column_name2=expression, . . . . . . ] [WHERE Clause];
UPDATE student set sno=85 where sname=’abc’;
1 row updated. DELETE: The DELETE statement removes rows from a table or database. The DELETE statement never references any columns because it removes entire rows of data, including all data values (all columns) for each affected row. Syntax1:
DELETE from <table_name>;
DELETE from student;
1 row deleted.
DELETE from <table_name> [WHERE Clause];
DELETE from student where sname=’abc’;
1 row deleted.
Data Control Language (DCL)
Data Control Language (DCL) includes SQL statements that allow administrators to control access to the data within the database and the use of various DBMS system privileges, such as the ability to start up or shut down the database. SQL statements that use the commands GRANT and ALTER are considered part of DCL. GRANT: The GRANT statement is used to bestow one or more privileges to a database user. Syntax:
GRANT privilege [, privilege . . . .] [ON object] TO grantee [, grantee . . .] [WITH GRANT OPTION];
REVOKE: Revoking means avoiding the privileges to be granted to particular
REVOKE privilege [, privilege . . . .] [ON object] FROM grantee [, grantee . . .];
Transaction Control Language (TCL)
Transaction Control Language (TCL) includes SQL statements that stores or retrieve the data from the database. TCL controls DCL statements. TCL consists of statements COMMIT, ROLLBACK, and SAVEPOINT
COMMIT: What ever changes we have done on our table are on all tables those are saved temporarily. Syntax: Commit; Example: Commit; Commit complete. ROLLBACK: This retrieves last statements that we have done (OR) it acts as an undo. Syntax: Rollback; Example: Rollback; Rollback complete. SAVEPOINT: It will save the data up to the point we mentioned. If we dint mention the point then it will take by default, by that time we don’t know that up to what extent we saved.
If we use ROLLBACK then we don’t know how many is saved and how many are deleted. Syntax: Save point <name>; Example: Save point <a2>; Note: <a2> is the level name. Up to what level that have to save in the database, we can give by this.
Data Query Language (DQL)
Data Query Language ( DQL) includes SQL statements that retrieve data from the database. Although it’s a very important part of SQL, DQL consists of statements SELECT and DUAL. SELECT: To see particular record in a table (or) whole details of the table. This will retrieve data from the database. Syntax1:
SELECT * from <table_name>;
Note: To see the fields of the whole table. Example1:
SELECT * from student;
Sno 1 2
Sname abc xyz
JOD 05-Jan-2005 09-May-2007
SELECT * from tab;
Note: To see all the tables in that username. Example2:
SELECT * from tab;
TNAME BONUS DEPT EMP
TABTYPE TABLE TABLE TABLE
SELECT column1, column2, . . . . from <table_name>;
Note: To see the particular columns of the table. Example3:
SELECT sname,sno from student;
sname abc xyz
sno 1 2
SELECT * from <table_name> [WHERE Clause];
Note: To see the limited rows of the table up to which we want.
SELECT * from student where sname=’abc’;
SELECT column1, column2, . . . . from <table_name> [WHERE Clause];
Note: To see the particular columns of the table. Example5:
SELECT sname from student where JOD=05-Jan-20005;
Sname abc DUAL: It is a workspace used to do some calculations. This is a dummy table to calculate arithmetic expressions and we get system data from this. Syntax: Select sysdate from DUAL; SYSDATE 06-DEC-09