Você está na página 1de 20

DATABASE MANAGEMENT SYSTEM

Syllabus: 8. SQL Concepts:


Basics of SQL,DDL,DML,DCL,structure-creation,alteration,defining constraints-primary key,foreign key,unique,not null,check,IN operator,aggregate functions,Built-in functions-numeric,date,string functions,set operations,sub-queries,corelated subqueries,join,Exist,Any,All,view and its types,transaction control commands.

9. PL/SQL concepts:
Cursors,Stored Procedures,Stored function,Database Triggers Reference Book: SQL-PL/SQL by IVAN BAYROSS

What is Database?
It is collection of information(records) that is organized so that it can easily be accessed,managed and updated. What is Database Mangement System? It is set of computer programs that controls the creation,maintenance and the use of the database of an organization. Eg. Oracle

Structured Query Language

Introduction to SQL
What is SQL?
SQL stands for Structured Query Language. Originally developed by IBM in the early 1970s. It was initially spelt and pronounced as SEQUEL but now popularly called SQL. It has an ANSI as well as ISO standard.

In DBMS a group of similar information or data which is of interest to an organization is called an Entity. Entity information is stroed in an object called Table. Eg. Student (entity) & information about student stored in student_master table. Table is two dimentsional matrix that consists of rows and columns. Table must have a unique name(i.e student_master).

Components parts of a Two Dimensional Matrix:


Attributes/columns/fields:

Each entity can have a number of characteristics. Eg. Student have characteristics name,address,telephon number etc The values for these characteristics are called Attributes values.

Tuple/record/row:
An organization will deal with many clients and the same information must be record for each student. Multiple fields placed in a horizontal plane, is called a record or row or typle.

Introduction to SQL
Different Components of SQL: Data Defination Language(DDL) Data Manipulation Language(DML) Data Control Language(DCL) Transaction Control Data Retrieval(Queries)

DDL (Data Defination Language): The DDL commands related to structure of an object (table ,view etc.) i.e creating an object, modifying structure of an object, removing an object or changing the name of object E.g. CREATE,ALTER,DROP,RENAME,TRUNCATE DML (Data Manuplation Language): The DML commands are those related to the content of the table (i.e data). They deal with the insertion, updation and deletion of rows in a table. E.g. INSERT,UPDATE,DELETE.

DCL(Data Control Language):

The DCL commands are required to give or take back access rights on objects. eg. GRANT,REVOKE
TRANSACTION CONTROL:

Handle the unit of work i.e a transcation A transcation either exectues as a whole or none of its statements execute. eg. COMMIT,ROLLBACK,ROLLBACK TO SAVEPOINT
Data retirval:

-To display data(records,columns)in required fields. Eg. SELECT(many options with the select command)

DATA TYPES:
1)CHAR (size): To store character strings values of fixed length. Max no:255

oracle compares CHAR values using blank comparison semantics i.e if a value that is inserted in a cell CHAR data type is shorter than the size it is defined for then it will be padded with spaces on the right until it reaches the size characters in length.
2)VARCHAR/VARCHAR2(size): To store variable length alphanumeric data. max no: 2000characters

Note: inserted values will not be padded with spaces.


3)NUMBER(P,S): To store numbers (fixed or floating) P (precision): max. length of data S (scale): number of places to the right of the decimal IF scale is omitted then default is zero. If precision is omitted ,values are stored with their original precision upto the maximum of 38 digits. Max:38 digits

4) DATE:

To reprent date and time. Standard format: DD-MON-YY & 24 hour time format.eg. 21-JUL-09. By default time is 12:00:00 If no time portion is specified. The default date : first day of the current month. Variable length character string (2 GB)

5) LONG:

6)RAW/LONG RAW: To store binary data like digitalized picture or image.(255bytes for raw and 2 GB for long raw)

The Situation: Student table


field id name Address dob sex type numeric varcharacter varcharacter date character width 4 10 20 8 1 contents student id number name address date of birth sex: M / F

a) CREATE TABLE COMMAND: CREATE TABLE tablename (columnname size,columnname size); b) INSERT VALUES COMMAND: INSERT INTO tablename (column1,column2)VALUES(value1,value2); OR INSERT INTO tablename VALUES(value1,value2); NOTE:Values in between .

c)

To view table structure: DESC tablename;

d)

Retriving Data from a Table:


SELECT *(ALL/DISTINCT) | [<column1,column2>] FROM tablename [WHERE <conditions(with operators)>] ORDER BY <columns>[ASC/DESC]);

Retriving Data:

Retriving all rows and all columns: SELECT * FROM tablename; Retriving particular columns and all rows: SELECT column1 (AS newcolumnname),column2 FROM tablename; Retriving selected rows and all columns(with some condition) SELECT * FROM tablename WHERE search condition; Retriving selected columns and selected rows: SELECT columnname1,columnname2 FROM tablename WHERE search condition;

Elimination of duplicates from the select statement: SELECT DISTINCT columnname,columname FROM tablename; SELECT DISTINCT * FROM tablename; Sorting data in a table: SELECT * FROM tablename ORDER BY columnname desc/asc; (by default order is ascending order)

Creating table from a table: Syntax: CREATE TABLE tablename (columnname,columnname) AS SELECT columnname,columnname FROM tablename; Inserting data into a table from another table: Syntax: INSERT INTO tablename SELECT columnname,columnname FROM tablename; INSERT INTO tablename SELECT columnname,columnname FROM tablename WHERE column=expression;

Delete(removal)of all rows:


DELETE FROM tablename;

Removal of a specific row/s:


DELETE FROM tablename WHERE search conditions;

Update command:
used to change or modify data values in a table Updating all rows: UPDATE tablename SET columnname=expression,columnname=expression; Update records conditionally: UPDATE tablename SET columnname=expression,columnname=expression; WHERE columnname=expression;

Modifying the structure of tables:


ADDING NEW COLUMNS: ALTER TABLE tablename ADD(newcolumnname datatype(size),newcolumnname datatype(size); Modifying Existing columns: ALTER TABLE tablename MODIFY(columnname newdatatype(size); NOTE: Using ALTER TABLE clause the following tasks cannot be performed: 1)Change the name of table 2)Change the name of column 3)Drop a column 4)Decrease the size of a column if table data exists.

Renaming tables: RENAME oldtablename TO newtablename; Destroying tables: DROP TABLE client_master;

Você também pode gostar