You are on page 1of 9

Structured Query Language (SQL)

Database: A collection of understandable and meaningful data is called a database. DBMS: A system which can store, retrieve, process and manage the data in an efficient manner is called a DataBase Management System. RDBMS: A DBMS which is based on a Relational Model is called RDBMS. In RDBMS, data is stored in the form of related tables. As a result the data can be viewed in many different ways. The relational database can be spread and managed into different tables rather than centralizing the data into a single table. SQL: Developed by IBM in 1970s, it is a language which provides an interface to relational database systems. SQL is ANSI and ISO standard and often called as SEQUEL. Components of SQL: 1. Data Definition Language 2. Data Manipulation Language 3. Data Control Language 4. Transaction Control Language. 5. Data Query Language Data Definition Language: CREATE ALTER DROP TRUNCATE To create objects in a database Alters the structure of the database Delete objects from the database. Removes all records from the database including the space allocated from the records.

Data Manipulation Language: INSERT UPDATE DELETE INSERT ALL Inserts Data into the tables. Updates the current available data in the tables Deletes one or more rows from the tables but not the space allocated for the rows Inserts multiple rows into the table at a time.

Note: 1.) Delete statement, when issued, increases complexity when a record/row in a table which has a relationship with other table and is linked by a foreign key. If you wish to delete the row in the base table, then you need to first delete the row from the particular table having the foreign key constraint. 2.) Oracle has a concept of referential integrity between tables i.e., if a record from the base table is deleted, then the record in linked tables will also gets deleted if any.

Data Control Language: COMMIT ROLLBACK SAVEPOINT To save the work done Restore the database upto the last commit issued A save point is a transaction to which we can later rollback

Transaction Control Language: GRANT REVOKE Giving users access privileges to the database Withdraw/ take back the access privileges given by GRANT command

Data Query Language:

SELECT

The SQL SELECT statement is used to query or retrieve data from a table in the database

DATA TYPES in SQL:

CHAR(size)

This data type is used to store character string values of fixed length. This size in brackets determines the no.of characters the cell can hold and the maximum characters the CHAR data type can hold is 255 characters. Example: Name CHAR(10)

VARCHAR(size) or VARCHAR2(size)

This data type is used to store variable length alphanumeric data. It is a flexible form of CHAR data type. Maximum 4000 characters can be stored. The DATE data type is used to represent date and time. Standard format is DD-MM-YY. To store date in other forms, we need to use appropriate functions. By default, time in the date field is set to 12:00:00am if no time is specified along with Date. The Number data type is used to store both fixed and floating numbers. The precision (P) determines the maximum length of the data, whereas the scale(S) determines the number of places to the right of decimal. Negative or positive number upto 38 digits of precision can be stored Ex: 2234+.29 or 2234 -.29 This data type is used to store variable length character strings containing upto 2GB. Only one LONG variable can be defined per table. A table containing LONG data type cannot be clustered. This data type is used to store binary data such as digitized picture or image.

DATE

NUMBER(p,s)

LONG

RAW/LONG RAW RAW can have maximum length of 255 bytes. LONGRAW can contain upto 2GB

DATA CONSTRAINTS: Constraints are used to limit the type of data that can go into a table.Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). We will focus on the following constraints:

NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK DEFAULT The NOT NULL constraint enforces a column to NOT accept NULL values.

NOT NULL

UNIQUE

The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field. The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have only ONE primary key. FOREIGN KEY in one table points to a PRIMARY KEY in another table. The FOREIGN KEY constraint also prevents that invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

PRIMARY KEY

FOREIGN KEY

CHECK

DEFAULT

The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified.

SELECT statement usage and its CLAUSES:


SELECT is the beginning of the SQL command for querying (retrieving) data from a database table, view, or object. Objects are similar to tables, but they have a more complex structure. Order of Execution: SELECT -- which you use to specify the fields or calculated fields you want to return FROM -- which you use to specify table or tables you want to use and usually includes joins. WHERE -- which you use to filter rows GROUP BY -- used to grouped together values using field or expression you specified HAVING -- filters group by results with aggregate function (doesn't work on row by row basis like WHERE) ORDER BY -- used to specify results order.

Usage of SELECT Statement: 1. Simple Query: A SELECT statement can be used as simple query to retrieve data from a table or group of related tables. 2. Complex Query: A SELECT statement can be embedded within another SELECT statement. 3. Create a view or table: A SELECT statement can be used to create a view or new table. A view is a stored query that is executed whenever another SELECT statement retrieves data from the view by using the view in a query. 4. Insert, update or delete date: A SELECT statement can be used within the INSERT, UPDATE or DELETE statements to add greater flexibility to those commands.

Types of Select Queries: Simple Query: SELECT * FROM EMP_TABLE; Filtered Query: SELECT EMP_ID, NAME, DEPT FROM EMP_TABLE WHERE NAME LIKE s%; (*=ALL ROWs)

DATABASE OBJECTS IN SQL Table View Index Synonym Cluster Sequence Procedure Function Package Trigger Tablespace

TABLE Data is stored inside SQL tables which are contained within SQL databases. A single database can house hundreds of tables, each playing its own unique role in the database schema. SQL tables are comprised of rows and columns. Table columns are responsible for storing many different types of data, like numbers, texts, dates, and even files.

Syntax to Create a Table:

CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... );

Example : CREATE TABLE orders (id number PRIMARY KEY, customer VARCHAR(50), day_of_order DATE, product VARCHAR(50), quantity number);

VIEW A view is a virtual table based on the result-set of an SQL statement. View contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table. Syntax to Create a view: CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition Example: CREATE VIEW [Products Above Average Price] AS SELECT ProductName, UnitPrice FROM Products WHERE UnitPrice > (SELECT AVG(UnitPrice) FROM Products)

INDEX An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/queries. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against. Syntax to Create an Index: CREATE [unique] INDEX index_name ON table_name (column_name,[column_name],..); Example: Create index PIndex on EMP (EMP_ID); DROP index PIndex;

SYNONYM A synonym is an alias or alternate name for a table, view, Sequence, or other Schema object. They are used mainly to make it easy for users to access database objects owned by other users. Because a synonym is just an alternate name for an object, it requires no storage other than its definition. Synonyms can be public or private Syntax to create a Synonym: CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema.]synonym FOR [schema.]object [@dblink] Example: CREATE SYNONYM addresses for hr.locations; DROP SYNONYM addresses.

You might also like