Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
283 views

Introduction To SQL

SQL is a language used to interface with relational database systems. It was developed by IBM in 1970 and includes commands for data manipulation (DML), data definition (DDL), and data control (DCL). SQL statements start with a verb like SELECT and clauses are separated by commas within statements and statements are terminated with a semicolon.

Uploaded by

vipul123patel123
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
283 views

Introduction To SQL

SQL is a language used to interface with relational database systems. It was developed by IBM in 1970 and includes commands for data manipulation (DML), data definition (DDL), and data control (DCL). SQL statements start with a verb like SELECT and clauses are separated by commas within statements and statements are terminated with a semicolon.

Uploaded by

vipul123patel123
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

SQL concepts

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>));

Checklist When Creating Tables


What

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

INTO <tablename> (<columnname1>, <columnname2>) values(<expression>, <expression2>);

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

ways of filtering table data are:

Selected columns and all rows Selected rows and all columns Selected columns and selected rows

Selected Columns and all Rows


Syntax:

SELECT <ColumnName1>, <ColumnName2> FROM <TableName>;

Selected Rows and all Columns


Syntax:

SELECT * from <TableName> WHERE <Condition>; Here, <condition> is quantified as <ColumnName = Value>

Selected Columns and Selected Rows


Syntax:

SELECT <ColumnName1>, <ColumnName2> FROM <TableName> WHERE <Condition>;

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:

SELECT * FROM <TableName> ORDER BY <ColumnName> <[Sort Order]>;

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> SELECT <ColumnName1>, <ColumnNameN> FROM <TableName>;

Insert only selected data from Source Table to Target Table


Syntax:

INSERT INTO <TableName>(<Column1>, <ColumnN>) SELECT <Column1>, <ColumnN> FROM <TableName> WHERE <Condition>;

DELETE in SQL is used to remove either:


All

the rows from a table OR A set of rows from a table.


Syntax(Removal

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

the rows from a table OR A selected set rows from a table


Syntax(Updating

all rows): UPDATE <TableName> SET <Column1> = <Expression1>, <Column2> = <Expression2>;

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.*

ALTER TABLE works by making temporary copy of the original table.

Adding new columns


Syntax:

ALTER TABLE <TableName> ADD(<NewColumn> <Datatype>(<Size>), <NewColumn> <Datatype>(<Size>));

Dropping Single Column from a table:


Syntax:*

ALTER TABLE <TableName> DROP COLUMN <Column1>;

Dropping

Multiple columns from the table ALTER TABLE <TableName> DROP (<Column1>, <Column2>,..); existing columns* ALTER TABLE <TableName> MODIFY (<Column> <NewDatatype>(<NewSize>));

Modifying

The following cannot be performed using ALTER TABLE clause:


Change

the name of the table. Decrease the size of a column if table data exists.

To rename a table, the syntax is


Syntax:

RENAME <TableName> TO <NewTableName>;

TRUNCATE TABLE empties a table completely.


Syntax: TRUNCATE

TABLE <TableName>;

Logically it is similar to DELETE but there are some differences:


Truncate

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.

To destroy or discard a table DROP TABLE is used.


Syntax:

DROP TABLE <TableName>;

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:

DROP [PUBLIC] SYNONYM SYNONYM_NAME;

To display all the tables in the current tablespace.

Syntax: SELECT * FROM TAB;

To display information about columns in a table.

Syntax: DESCRIBE <TableName>;

You might also like