18
SQL STRUCTURED QUERY LANGUAGE

SQL

Embed Size (px)

DESCRIPTION

SQL. STRUCTURED QUERY LANGUAGE. Relationship. E-R-DIAGRAM :. Composite attribute. N. Work for. L NAME. M. location. name. F name. Department. NAME. Multivalued attribute. Entity. ssn. number. 1. No.of employee. Mgr. 1. EMPLOYEE. Key attribute. Employer. - PowerPoint PPT Presentation

Citation preview

SQLSTRUCTURED QUERY LANGUAGE

E-R-DIAGRAM :

L NAME

NAME

EMPLOYEE

Super vision

Work for

name

Department

Mgr No.of employee

number

location

Dependent of

dependent Relationship

name

F name

ssn

Composite attribute

Relationship

Multivalued attributeEntity

Key attribute

Derived attribute

Identifying weak relationship

Employer

N 1

1

1

M

N

CONSTRAINTS These are the restriction or condition that are used on the column of the table to preserve the data

correctness .

Constraints type are following :

1. Unique

2. Check

3. Not null

4. Primary key

5. Foreign key

oNot null This ensure that at least some values should be present for an attribute

Can have more than one not null on a table .

Null : Null is neither zero or blank space .

It is used to represent empty .

Ex : Name cannot be left blank/null in table .

Unique :

o It checks for duplicate values a unique .

o A unique column can have multiple null value .

Ex : employee id / mobile / mail id .

Check :

Check is used for enforcing some additional condition with respect to business

Requirement .

Ex : Sal > 3000 , Age >14 .

Primary key :

o It is used for identifying a record uniquely in a table . It is the combination of “ Not null “ and “ Unique” constraints

o You can have only one primary key is allowed per table .

Ex :

Employee id or mobile / mail id can be used as primary key .

Note :….

Candidate key : Eligible to become a primary key

Primary key : Unique and Not null .

Alternate key : Eligible to become a primary key but not chosen as primary key.

when “p k” is created out of more than one column then

it is called as composite primary key.

C k = P k + A k

A k = C k – P k

Foreign key : It is referential integrity constraints which created the relationship

between the table . To create a foreign key in a child table , Master

table should have a primary key on the common column .

composite primary key in master table is used as composite foreign key in child table .Foreign key can take both null and duplicate value .There can be more than one foreign key in

Table .

child table Master tableF.K P.K

RDBMS : Relational database management system2-dimensional architecture

If client is directly connected to database then it is 2-tier

Database management system (DBMS) .

Databaseserverclient

SQL STATEMENT :One language with 5 sub languages : -

1. DQL (Data query language )

2. DML (Data manipulation language)

3. DDL (Data definition language)

4. TCL/DTL (Data transaction language / Transaction control language)

5. DCL (Data control language)

Sub language Action performed

DQL (Data query language)

1. Select

DML (Data manipulation language)

1. Insert2. Update3. Delete

DDL (Data definition language)

1. Create2. Alter3. Drop

4. Rename5. Truncate

DTL (Data transaction language)

1. Commit2. Rollback

3. Save point

DCL (Data control language)

1. Grant2. Revoke

Select (DQL) : Capabilities of SQL SELECT statement :

Projection : selecting column Selection : selecting rows

tab 1 tab 2

Join

Basic select statement :SELECT * |{ [ DISTINCT ] Column / Expression [ alias ]….} |FROM Table ;

Ex : select empno ,ename ,sal from emp ;

ENAME EMPNO SAL

---------- ---------- ----------

SMITH 7369 800

ALLEN 7499 1600

WARD 7521 1250

JONES 7566 2975

MARTIN 7654 1250

BLAKE 7698 2850

CLARK 7782 2450

SCOTT 7788 3000

KING 7839 5000

9 rows selected.

SELECT identifies what column.

FROM identifies which table.

Writing SQL Statement :o SQL Statement are not case sensitive .

o SQL Statement can be one or more lines .

o Keyword can not be abbreviated or split across lines .

o Clauses are usually placed on separate lines .

o / (forward slash) is used in the place of semi colon but if it is in the next line

o SQL also use arithematic operations .

like :

select ename,sal,sal+500

from emp ;

select ename ,sal ,sal *12

from emp ;

Arithmetic expression :Create expression with number and date data by using arithmetic operators .

Question :

Calculate a salary increase of 300 for all employees and display

Both old and new salary ?

Answer :

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

Operator Precedence :

Multiplication and Division take priority before addition and subtraction

Operator of the same priority are evaluated from left to right . Parentheses (Brackets) are used to force prioritized evaluation

and to clarify statements . If we are doing any arithmetic operation with null then it is

always null . If we are adding two column and value of one column is null

then the final value is null .

* , / ,+ , -

DDLDATA DEFINATION LANGUAGE

The create table statement :

You must have : CREATE TABLE privilege

You specify :

Table name column name ,column data type and column size and constraints .

CREATE TABLE [ schema.] table (column data type [DEFAULT Expression][;……] ) ;

Creating tables

Create table dept 1 ( dept no number (2), dname varchar2 (14), loc varchar2 (13));

Create table emp 1(emp no number (4) primary key,Ename varchar2 (10) not null,Age number (10) check (age > 14),Mobile number (10) unique,Dept no number (2) reference dept (dept no));