Introduction To SQL
Introduction To SQL
Structured Q Query L Language This language provides interface to relational database systems. It was developed by IBM in 1970. It is often pronounced as SEQUEL.
SQL
also encompasses DML(Data Manipulation Language), for INSERTs, UPDATEs, DELETEs. It also encompasses DDL(Data Definition Language), for creating tables and other data structures. SQL has been a command language for communication with Oracle 9i server. When an SQL statement is entered it remains in a part of memory called SQL buffer, until a new SQL statement is entered.
SQL starts with a verb( i.e SQL action word). Example: SELECT statements. This verb may have additional adjectives. Example: FROM. Each verb is followed by number of clauses. Example: FROM, WHERE, HAVING. A space separates clauses. Example DROP TABLE EMP;
A comma separates parameters without clause. A ; is used to end SQL statements. Statements may be split across lines but keywords may not. Identifiers can contain up to 30 characters and must start with an alphabetic character. Character and date literals must be enclosed within single quotes.
DDL(Data Definition Language) Set of SQL commands used to create, modify and delete database structures but not data. They are normally used by DBA, database designer or application developer. They are not susceptible to rollback. DML(Data Manipulation Language) It is the area of SQL which allows changing data within database.
DCL(Data Control Language) Component of SQL statement that control access to data and to the database. Occasionally, DCL statements are grouped with DML statements. DQL(Data Query Language) It is the component of SQL statement that allows getting data from database and imposing ordering upon it. It allows performing operations on data obtained from the database.
CREATE: To create objects in the database. ALTER: Alters the structure of the database. DROP: Delete objects from the database TRUNCATE: Remove all records from a table, including all spaces allocated for the records are removed. COMMENT: Add comments to the data dictionary. GRANT: Gives users access privileges to database REVOKE: Withdraw access privileges given with the GRANT command
INSERT: Insert data into a table UPDATE: Updates existing data within a table DELETE: Deletes all records from a table, the space for the records remain. CALL: Call a PL/SQL or Java subprogram. EXPLAIN PLAN: Explain access path to data LOCK: TABLE Control concurrency
COMMIT: Save work done SAVEPOINT: Identify a point in a transaction to which you can later roll back. ROLLBACK: Restore database to original since the last COMMIT SET TRANSACTION: Change transaction options like what rollback segment to use. GRANT/REVOKE: Grant or take back permissions to or from the oracle users.
Syntax:
CREATE TABLE <TableName> (<ColumnName1> <DataType>(<size>), <ColumnName2> <DataType>(<size>));
are the attributes of the rows to be stored? What are the data types of the attributes? Which columns should be used to build the primary key? Which columns do (not) allow null values?
Syntax:
INSERT
In INSERT INTO SQL statement, table columns and values have a one to one relationship. i.e first value is inserted in first column, second value is inserted in second column and so on.
If there are exactly same numbers of values as there are columns and the values are sequenced accordingly, then there is no need to indicate column names. However, if there are less values than the columns in the table then it is mandatory to indicate both the table column name and its corresponding value.
The SELECT command is used to retrieve rows selected from one or more tables. To retrieve data from all rows and all columns Syntax: SELECT * from <TableName>; Filtering table data
Three
Selected columns and all rows Selected rows and all columns Selected columns and selected rows
SELECT * from <TableName> WHERE <Condition>; Here, <condition> is quantified as <ColumnName = Value>
A table could hold duplicate rows. In order to view unique rows the DISTINCT clause can be used.
Syntax(For
mentioned columns): SELECT DISTINCT <ColumnName1>, <ColumnName2> FROM <TableName>; all columns): SELECT DISTINCT * FROM <TableName>;
Syntax(For
The rows retrieved from table can be sorted either in ascending or descending order.
Syntax:
Syntax:
CREATE TABLE <TableName> (<ColumnName>, <ColumnName>) AS SELECT <ColumnName>, <ColumnName> FROM <TableName>;
To create a target table without the records from source table(i.e create structure only) the WHERE clause must specify a condition that cannot be satisfied.
Syntax:
INSERT INTO <TableName>(<Column1>, <ColumnN>) SELECT <Column1>, <ColumnN> FROM <TableName> WHERE <Condition>;
of all rows): DELETE FROM <TableName>; of specific rows(s)): DELETE FROM <TableName> WHERE <Condition>;
Syntax(Removal
UPDATE command is used to change or modify data values in a table. UPDATE in SQL is used to either update:
All
Syntax(Update
records conditionally): UPDATE <TableName> SET <Column1> = <Expression1>, <Column2> = <Expression2> WHERE <Condition>;
It is used to modify the structure of a table. It allows changing the structure of existing table. With ALTER TABLE it is possible:
To add or delete columns, Change data type of existing columns, Rename columns or the table itself.*
Dropping
Multiple columns from the table ALTER TABLE <TableName> DROP (<Column1>, <Column2>,..); existing columns* ALTER TABLE <TableName> MODIFY (<Column> <NewDatatype>(<NewSize>));
Modifying
the name of the table. Decrease the size of a column if table data exists.
TABLE <TableName>;
operations drop and re-create the table, which is much faster than deleting rows one by one. Truncate operations are not transaction-safe. Deleted rows cannot be returned.
A synonym is an alternative name for objects such as tables, views, sequences, stored procedures, and other database objects.
Syntax: CREATE [PUBLIC] SYNONYM SYNONYM_NAME FOR OBJECT_NAME;
Syntax: