You are on page 1of 25

SQL SERVER

DATABASE

Prepared By
Bhagirathi Muduli
DEFINITION
A database is an ordered collection of related data elements intended to
meet the information needs of an organization and designed to be shared by
multiple users.

A database is a collection of data elements. Not just a random assembly of


Order data structures, but a collection of data elements put together deliberately with
Collection proper order. The various data elements are linked together in the most logical
manner
Related Data The data elements in a database are not disjointed structures without any
Elements relationships among them. These are related among themselves and also
pertinent to the particular organization.

Information Needs The collection of data elements in a database is there for a specific purpose.
That purpose is to satisfy and meet the information needs of the organization.

All authorized users in an organization can share the information stored in its
Shared database. Integrated information is kept in the database for the purpose of
sharing so that all user groups may collaborate and accomplish the
organization’s
objectives.
To Design A Database we need the following things:-

Table/Relation Is a 2 dimensional structure used to hold related information.

Row Is a collection of instance of one thing.

Column/Filed All the information of a single type.

Database Objects

Tables, Views Indexes, Stored Procedures, Triggers


etc.
SQL (STRUCTURED QUERY LANGUAGE)
Language through which we can interact with the database .
Types of SQL

DDL (Data Definition Language)


(A) CREATE (B) ALTER (C) TRUNCATE (D) DROP

DML (Data Manipulation Language)


(A) SELECT (B) INSERT (C) UPDATE (D) DELETE

TCL (Transaction Control Language)


(A) COMMIT (B) ROLLBACK (C) SAVE POINT

DCL (Data Control Language)


(A) GRANT (B) REVOKE
SQL DATATYPES
Each column value and constant in a SQL statement has a data type, which is associated
with a specific storage format, constraints, and a valid range of values.

1. CHARACTER DATA TYPES 2. DATE & TIME DATA TYPES


• CHAR( • DATETIM
• N)
VARCHAR( • E
SMALLDATETIM
N) E
3. NUMERIC DATA TYPES 4. INTEGER DATA TYPES

• DECIMAL • FLOAT( • TINYINT • SAMLLIN


• (P,S)
NUMERIC(P,S • N)
REA • INT • T
BIGIN
) L T
CONSTRAINT
Prevent invalid data entry into the table.

Types of Constraints
Not null
Unique
Primary Key
Foreign Key
Check
Default
CREATION OF NEW TABLE
CREATE TABLE TABLE_NAME(
COLUMN_NAME_1 DATATYPE
[CONSTRAINT CONSTARINT_NAME CONSTRAINT_TYPE]
COLUMN_NAME_2 DATATYPE
……………….
COLUMN_NAME_N DATATYPE
)

Example
CREATE TABLE EMPLOYEE
(
EMP_ID INT(4) CONSTRAINT PK_EMP_ID PRIMARY KEY,
EMP_NAME VARCHAR(30) CONSTRAINT NN_EMP_NAME NOT NULL,
EMP_ACTIVE CHAR(1) CONSTRAINT CHK_ACTIVE
CHECK(EMP_ACTIVE IN(‘A’, ‘D’)),
EMAIL VARCHAR(30) UNIQUE
)
ALTER COMMAND

To add a column:
ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE

To delete a column:
ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME

To change the data type of an existing column:


ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAME
DATATYPE

To add a constraint for an existing column:


ALTER TABLE TABLE_NAME ADD CONSTRAINT
CONSTARINT_NAME CONSTRAINT_TYPE
EMPLOYEE
EMP_ID EMP_NAME EMP_ACTIVE EMAIL

Add a column called “E_CONTACT” with data type of NUMERIC(12,2)


ALTER TABLE EMPLOYEE ADD E_CONTACT NUMERIC (12,2)

EMP_ID EMP_NAME EMP_ACTIVE EMAIL E_CONTACT

Change the data type of E_CONTACT column with INT(10)


ALTER TABLE EMPLOYEE ALTER COLUMN E_CONTACT INT(10)

Delete the E_CONTACT column


ALTER TABLE EMPLOYEE DROP COLUMN E_CONTACT
COLUMN NAME DATATYPE SIZE

EMP_ID INT 4
EMP_NAME VARCHAR 30

EMP_ACTIVE CHAR 1

EMAIL VARCHAR 30

EMPLOYEE TABLE STRUCTURE


INSERT A RECORD INTO A TABLE
INSERT INTO TABLE TABLE_NAME VALUES (VALUE_LIST)

INSERT INTO TABLE EMPLOYEE VALUES


(1001, ‘James Smith’, ‘A’, ‘jamesh@uco.co.in’)

INSERT INTO TABLE TABLE_NAME (COLUMN_LIST)


VALUES (VALUE_LIST)

INSERT INTO TABLE EMPLOYEE (EMP_ID, EMP_NAME,


EMP_ACTIVE ) VALUES (1002,’Katty Tony’, ‘I’)
EMP_ID EMP_NAME EMP_ACTIVE EMAIL
1001 James Smith A jamesh@uco.co.in
1002 Katty Tony I
UPDATE A RECORD IN A TABLE

UPDATE TABLE_NAME SET


COLUMN_NAME1=NEW_VALUE1, COLUMN_NAME2=NEW_VALUE2,
…………………
COLUMN_NAMEN=NEW_VALUEN,
[WHERE CONDITION]

Update the EMAIL column filed to ‘k.tony@people.com’ of


EMPLOYEE table whose EMP_ID is 1002

UPDATE EMPLOYEE SET EMAIL=‘k.tony@people.com’


WHERE EMP_ID=1002
EMP_ID EMP_NAME EMP_ACTIVE EMAIL
1001 James Smith A jamesh@uco.co.in
1002 Katty Tony I k.tony@people.com
DELETE A RECORD FROM A TABLE
DELETE FROM TABLE_NAME [WHERE CONDITION]

EMP_ID EMP_NAME EMP_ACTIVE EMAIL


1001 James Smith A jamesh@uco.co.in
1002 Katty Tony I k.tony@people.com

From the above EMPLOYEE table delete a record whose EMP_ID is 1002

DELETE FROM EMPLOYEE WHERE EMP_ID=1002

EMP_ID EMP_NAME EMP_ACTIVE EMAIL


1001 James Smith A jamesh@uco.co.in
TRUNCATE A TABLE

TRUNCATE TABLE TABLE_NAME

TRUNCATE TABLE EMPLOYEE

DROP A TABLE

DROP TABLE TABLE_NAME

DROP TABLE EMPLOYEE


PS_NAMES
PS_DEPT_TBL
EMPLID
FIRST_NAME DEPT_ID
MIDDLE_NAME DESCR
LAST_NAME LOCATION
EMAIL_ID

PS_JOBCODE PS_JOB
JOB_CODE EMPLID
DESCR DEPT_ID
JOB_CODE
SALARY
RETRIVE RECORD FROM A TABLE
SELECT FILED_LIST FROM TABLE_NAME EMPLID FIRST_NAME
1001 James
SELECT EMPLID, FIRST_NAME FROM PS_NAMES 1002 Katty

EMPLID FIRST_NAME MIDDLE_NAME LAST_NAME

1001 James Jon Smith


1002 Katty Tony

SELECT * FROM PS_NAMES


RETRIVE RECORD BY GIVING CONDITION
SELECT FILED_LIST FROM TABLE_NAME
[WHERE CLAUSE]
[GROUP BY CLAUSE]
[HAVING CLAUSE]
[ORDER BY CLAUSE]

Example
Retrieve the records from department table where the location is Delhi

SELLECT * FROM PS_DEPT_TBL


WHERE UPPER(LOCATION) =UPPER(‘DELHI’ )
GROUP BY CLAUSE
Is used to summarize or group the data.
The GROUP BY keyword is used when we are selecting multiple columns from a table
(or tables) and at least one arithmetic operator appears in the SELECT statement. When
that happens, we need to GROUP BY all the other selected columns, i.e., all columns
except the one(s) operated on by the arithmetic operator.
EMPLID DEPT_ID JOB_CODE SALARY
Example 1001 FIN ASC 25000
Calculate the total salary 1002 SWD ASC 23000
of individual department 1003 FIN ASC 24000
1004 HR ASC 15000

SELECT DEPT_ID, SUM(SALARY) FROM PS_JOB GROUP BY DEPT_ID

DEPT_ID SUM(SALARRY)
FIN 49000
SWD 23000
HR 15000
HAVING CLAUSE
Suppose we need to do the output based on the aggregate function.

The HAVING clause is reserved for aggregate functions.

A SQL statement with the HAVING clause may or may not have include the
GROUP BY clause. EMPLID DEPT_ID JOB_CODE SALARY
Example 1001 FIN ASC 25000
1002 SWD ASC 23000
Show the departments with
1003 FIN ASC 24000
salary below 25000
1004 HR ASC 15000

SELECT DEPT_ID, SUM(SALARY) DEPT_ID SUM(SALARRY)


FROM PS_JOB GROUP BY DEPT_ID SWD 23000
HAVING SUM(SALARY)<25000 HR 15000
ORDER BY CLAUSE
To list the output in a particular order such as in ascending order
or in descending order could be based on numerical value or text
value.
ORDER BY COLUMN_NAME ASC, DESC
EMPLID DEPT_ID JOB_CODE SALARY
Example
1001 FIN ASC 25000
Show the records in 1002 SWD ASC 23000
descending order by salary. 1003 FIN ASC 24000
1004 HR ASC 15000

SELECT * FROM PS_JOB ORDER BY SALARY DESC


EMPLID DEPT_ID JOB_CODE SALARY
1001 FIN ASC 25000
1003 FIN ASC 24000
1002 SWD ASC 23000
1004 HR ASC 15000
OPERATORS

Arithmetic Assignment operator


+,operators
-, *, /, and % =
Bitwise operators Comparison operators
&, | and ^ =, <>, <, >, >=, <=, !=, !<, and !>

Logical operators String concatenation operator


AND, BETWEEN, IN, LIKE,
NOT, and OR +
Unary operators Set Operator

+, -, and ~ Union, Union All,


Intersect, Minus
JOINS
 JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETE
data query statements to simultaneously affect rows from multiple tables.
 Joined tables must each include at least one filed in both tables that contain
comparable data.
Self Join

Inner Join

Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Cross Join
SELF JOIN
In this circumstance, the same table is specified twice with two different aliases in
order to match the data within the same table.

INNER JOIN
Match rows between the two tables specified in the INNER JOIN statement based
on one or more columns having matching data.  

OUTER JOIN
An OUTER JOIN is a join operation that includes rows that have a match, plus
rows that do not have a match in the other table.
LEFT OUTER JOIN
Returns all the rows from the left table in conjunction with the matching rows from
the right table. If there are no columns matching in the right table, it returns NULL
values.

RIGHT OUTER JOIN


Returns all the rows from the right table in conjunction with the matching rows
from the left table. If there are no columns matching in the left table, it returns
NULL values.

FULL OUTER JOIN


Combines left outer join and right after join. It returns row from either table when
the conditions are met and returns null value when there is no match.
CROSS OUTER JOIN
Does not necessitate any condition to join. Itcontains records that are multiplication
of record number from both the tables.

You might also like