Basic Database Concept
Basic Database Concept
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.
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:-
Database Objects
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
EMP_ID INT 4
EMP_NAME VARCHAR 30
EMP_ACTIVE CHAR 1
EMAIL VARCHAR 30
From the above EMPLOYEE table delete a record whose EMP_ID is 1002
DROP A TABLE
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
Example
Retrieve the records from department table where the location is Delhi
DEPT_ID SUM(SALARRY)
FIN 49000
SWD 23000
HR 15000
HAVING CLAUSE
Suppose we need to do the output based on the aggregate function.
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
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.