Uploaded by supriya more

SQL

advertisement
Introduction:
-
Data
Database
DBMS
SQL
• What is Data?
Data is nothing but raw facts.
In simple words, data can be facts related to any
object in consideration. For example, your name,
age, height, weight, etc. are some data related to
you.
Structured Vs. Unstructured Data:
• Structured data:- for which we have proper
structure to store the data.
Structure is in the form of Relation(Table) i.e
nothing but Rows & Columns
• Unstructured Data:- for which we does not
have proper structure or format to store.
Example: Webpages
• What is Database?
It is organized collection of data where data is stored and
accessed electronically from computer system.
Database is a collection of inter-related data which helps in
efficient retrieval, insertion and deletion of data from database
and organizes the data in the form of tables, views, schemas etc.
For Example, university database organizes the data about
students, faculty, and admin staff etc. which helps in efficient
retrieval, insertion and deletion of data from it.
Example:
1) Library
2) University database
3) Railway Database
History:
• In early 1960’s.:Charles Bachman was the first person to develop
the Integrated Data Store (IDS) which was based on network data
model
• In the late 1960’s, IBM (International Business Machines
Corporation) developed the Integrated Management Systems
which is the standard database system used till date in many
places.
• In year 1970 that the relational database model was developed by
Edgar Codd. Many of the database models we use today are
relational based. It was considered the standardized database
model from then.
• In year IBM developed the Structured Query Language (SQL) as
a 1980’s, part of R project. It was declared as a standard language
for the queries by ISO and ANSI.
DBMS
Database Management System:
• The software which is used to manage database is
called Database Management System (DBMS).
• DBMS is nothing but Interface between databse
and user.
Example:- MySQL(Oracle), PostgreSQL, Microsoft
Access, SQL Server(Microsoft), FileMaker, dBASE
DB2(IBM), RDBMS, Clipper, and FoxPro
RDBMS
• Relational Database Management System
• What is Table ? (relation)
In Relational database model, a table is a collection of data
elements organized in terms of rows and columns. A table is also
considered as a convenient representation of relations. Table is the
most simplest form of data storage. Below is an example of an
Employee table.
ID
Name
Age
Salary
1
Adam
34
13000
2
Alex
28
15000
3
Stuart
20
18000
4
Ross
42
19020
• A single entry in a table is called a Tuple or Record or Row
• Each record can be broken down into several smaller parts of
data known as Attributes. The above Employee table consist
of four attributes, ID, Name, Age and Salary.
• A relation schema describes the structure of the relation, with
the name of the relation(name of table), its attributes and their
names and type.
SQL
• Structure Query Language(SQL) is a database query
language used for storing and managing data in
Relational DBMS.
• SQL was the first commercial language introduced
for E.F Codd's Relational model of database.
• Today almost all RDBMS(MySql, Oracle, Infomix,
Sybase, MS Access) use SQL as the standard
database query language. SQL is used to perform all
types of data operations in RDBMS.
• SQL stands for Structured Query Language. It is used for
storing and managing data in relational database management
system (RDMS).
• It is a standard language for Relational Database System. It
enables a user to create, read, update and delete relational
databases and tables.
• All the RDBMS like MySQL, Informix, Oracle, MS Access
and SQL Server use SQL as their standard database language.
• SQL allows users to query the database in a number of ways,
using English-like statements.
Types of Structured Query Language(SQL)
SQL is basically combination of four different
languages, they are –
• DDL (Data Definition Language)
DDL is used to define table schemas.
• DML (Data Manipulation Language)
DML is used for inserting, updating and deleting data
from the database.
• DCL (Data Control Language)
DCL mainly deals with the rights, permissions and
other controls of the database system.
• TCL(Transaction Control Language)
TCL are used to manage transactions in the database.
Data types:
Data type
Use
INT
used for columns which will store integer values.
FLOAT
used for columns which will store float values.
VARCHAR(size)(20)
used for columns which will be used to store
characters and integers, basically a string.
CHAR
used for columns which will store char
values(single character).
DATE
used for columns which will store date values.
dd-mm-yyyy
Time
Time in format H:M:S
DDL Commands:
•
•
•
•
•
CREATE : to create objects in database
ALTER : alters the structure of database
DROP : delete objects from database
TRUNCATE: deletes only contenets.
RENAME : rename an objects
DDL CREATE COMMANDS:Ex:create table student
(
rollno int,
name varchar(20),
DOB date
);
DDL: ALTER COMMANDS:• To add a column to existing table
Ex:-ALTER TABLE STUDENT ADD ADDRESS VARCHAR(200) ;
• To rename any existing column
Ex:-ALTER TABLE STUDENT RENAME COLUMN ADDRESS TO
LOCATION;
• To change data type of any column or to modify its size.
Ex:-ALTER TABLE STUDENT MODIFY ROLLNO VARCHAR(10);
• To drop a column from the table.
Ex:-ALTER TABLE STUDENT DROP COLUMN ADDRESS;
• DDL:Truncate Command:• Only the contents inside data gets deleted no structure.
TRUNCATE TABLE STUDENT;
• DDL:Drop Command:•
The DROP command used to remove the database object from DBMS, and
delete the table from database.
Once we use DROP Command for database the database deleted with all the
related objects like tables, views and stored procedures.
•
The DROP Command can not be rollback.
DROP TABLE STUDENT;
DDL Rename Command:• RENAME command is used to set a new name for any existing table
Ex:-
• RENAME STUDENT TO STUDENTS_INFO;
SQL Comments:
You can add comments to a table or any column .
COMMENT ON TABLE STUDENT IS ‘STUD INFO’;
• user_tab_comments
COMMENT ON COLUMN STUDENT.ROLLNO IS ‘IDENTIFICATION NUMBER’;
user_col_comments
DML Commands:
•
•
•
•
SELECT: retrieve data from the database
INSERT: insert data into a table
UPDATE: update existing data within a table
DELETE: deletes all records from a table,
Structure/Space for the records remains as it is.
DML: Select Command:-
Ex:Select * from student;
To Insert values inside Table
• DML: Insert Command:Syntax:
INSERT INTO TABLENAME VALUES( List of values corresponding
to created structure of table);
Ex:- INSERT INTO STUDENT VALUES (01, ‘Ram’, ’1-jan-1991’);
WHERE CLAUSE
• Get only those rows which satisfy specific condition
• Syntax
SELECT columnname1, columnname2,……….
FROM tablename
WHERE condition;
• Example
SELECT employee_id,first_name,last_name
FROM employees
WHERE Employee_ID=190;
• DML:Update Command:Ex:- UPDATE STUDENT SET Name = ‘Amar’ WHERE
roll=01;
• DML:Delete Command:Ex:- DELETE FROM STUDENT WHERE StudID=1;
DCL Commands:
DCL :GRANT Command:GRANT command gives user's access privileges to the database.
This command allows specified users to perform specific tasks.
Ex:GRANT ALL ON employee
TO ABC;
In the above example, user 'ABC' has been given permission to view
and modify the records in the 'employee' table
DCL Commands
DCL:REVOKE Command:•
REVOKE command is used to cancel previously granted or denied
permissions.
• This command withdraw access privileges given with the GRANT
command.
• It takes back permissions from user.
Ex:REVOKE UPDATE
ON employee
FROM ABC;
TCL Commands:
**Transactional control commands are used only DML
Commands such as INSERT, UPDATE and DELETE.**
• COMMIT: Commit command is used to permanently
save any transaction into the database.
• ROLLBACK: This command restores the database to last
committed state. It is also used with savepoint command
to jump to a created savepoint in a transaction.
• SAVEPOINT: Savepoint command is used to temporarily
save a transaction so that you can rollback to that point
whenever necessary.
Download