Xiicsl12 em

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

N.S.Hr.Sec.

School, Theni

Welcomes You…

1
Prepared by : S. Ganesh Kumar, B.Sc., B.Ed., M.S.I.T.,
• The components of SQL.
• To create a table by specifying the fields and records.
• To apply various manipulations like inserting, updating records.
• To learn about various constraints
• To generate queries in the table
• To modify the structure of an existing table.
• The commands to delete records, table etc…,
Introduction to SQL

SQL stands for Structured Query Language.

It is used for storing and managing data in RDBMS.

Developed at IBM

Initially it was called as sequel

SQL became a standard of the American National Standards Institute in 1986

Latest SQL standard as of now is SQL 2008, released in 2008


Role of SQL in RDBMS

• SQL can execute queries against a database


• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can set permissions on tables, procedures, and views
• All the RDBMS like MySQL, Informix, Oracle, MS Access and
SQL Server use SQL as their standard database language
Processing Skills of SQL

1. Data Definition Language (DDL) :


2. Data Manipulation Language (DML) :
3. Embedded Data Manipulation Language :
4. View Definition :
5. Authorization :
6. Integrity :
7. Transaction control
What is WAMP ?

WAMP is an acronym that stands for Windows, Apache, MySQL, and PHP.
It’s a software stack which means installing WAMP installs Apache, MySQL,
and PHP

“W” stands for Windows, there’s also LAMP (for Linux) and MAMP (for Mac).
“A” stands for Apache. It is the server software, responsible for serving web pages.
“M” stands for MySQL. MySQL’s job is to be the DBMS for your server.
“P” stands for PHP. Alongside, PhpMyAdmin allows you to manage easily your DB.

https://www.wampserver.com/en/ https://sourceforge.net/projects/wampserver/
DATA DEFINITION LANGUAGE

The Data Definition Language (DDL) consist of SQL statements used to


define the database structure or schema. It simply deals with descriptions of
the database schema and is used to create and modify the structure of
database objects in databases.

CREATE To create tables in the database.


ALTER Alters the structure of the database.
DROP Delete tables from database.
Remove all records from a table, also release the space
TRUNCATE
occupied by those records.
DATA MANIPULATION LANGUAGE

A Data Manipulation Language (DML) is used for adding (inserting),

removing (deleting), and modifying (updating) data in a database.

INSERT Inserts data into a table


UPDATE Updates the existing data within a table.
Deletes all records from a table, but not the space occupied by
DELETE
them.
DATA CONTROL LANGUAGE

A Data Control Language (DCL) is used to control the access of


data stored in a database. It is used for controlling privileges in the
database (Authorization).

GRANT Grants permission to one or more users to perform specific tasks.


REVOKE Withdraws the access permission given by the GRANT statement.
TRANSACTIONAL CONTROL LANGUAGE

Transactional control language (TCL) commands are used to


manage transactions in the database. These are used to manage
the changes made to the data in a table by DML statements.

COMMIT Saves any transaction into the database permanently.


ROLLBACK Restores the database to last commit state.
SAVEPOINT Temporarily save a transaction so that you can rollback.
DATA QUERY LANGUAGE

The Data Query Language consist of commands used to query or

retrieve data from a database. One such SQL command in Data

Query Language is select.

SELECT : It displays the records from the table.


Data Types
Data Type Description
char Fixed width string value. Values of this type is enclosed in single quotes.
(Character) For ex. Anu’s will be written as “Anu’s”.
Variable width character string. This is similar to char except the size of the
varchar
data entry vary considerably.
It represents a fractional number such as 15.12, 0.123 etc. Here the size
dec (Decimal)
argument consist of two parts : precision and scale.
It is same as decimal except that the maximum number of digits may not
numeric
exceed the precision argument.
It represents a number without a decimal point. Here the size argument is
int (Integer)
not used.
smallint It is same as integer but the default size may be smaller than Integer.
It represents a floating point number in base 10 exponential notation and
float
may define a precision up to a maximum of 64.
It is same as float, except the size argument is not used and may define a
real
precision up to a maximum of 64.
double Same as real except the precision may exceed 64.
SQL Commands and their Functions

They have a special meaning in SQL. They are understood


Keywords
as instructions.
They are instructions given by the user to the database also
Commands
known as statements.
They begin with a keyword and consist of keyword and
Clauses
argument.
Arguments They are the values given to make the clause complete.
MySQL is a freely available open source Relational Database
Management System (RDBMS) that uses Structured Query
Language (SQL).
16
What is MySQL?

•MySQL is a database system used on the web.


•MySQL is a database system that runs on a server.
•MySQL is ideal for both small and large applications.
•MySQL is very fast, reliable, and easy to use.
•MySQL uses standard SQL.
•MySQL is free to download and use.
•It is developed, distributed, and supported by Oracle Corporation.
•MySQL is named after co-founder Monty Widenius's daughter: My

17
Creating Database
DDL Commands

CREATE TABLE Command

CREATE TABLE <table-name>


(<column name><data type>[<size>]
<column name><data type>[<size>]……
);

CREATE TABLE Student


(Admno integer,
Name char(20),
Gender char(1),
Age integer,
Place char(10),
);
ALTER command

alter command is used for altering the table structure, such as,
• to add a column to existing table
• to rename any existing column
• to change data type of any column or to modify its size.
• to drop a column from the table.

ALTER TABLE <table-name> ADD <column-name><data type><size>;

Ex:- ALTER TABLE student ADD( Address CHAR(20) );


ALTER <table-name> MODIFY<column-name><data type><size>;

Ex:- ALTER TABLE Student MODIFY Address char (25);

ALTER <table-name> RENAME old-column-name TO new-column-name;

Ex:- ALTER TABLE Student RENAME Address TO Location;

ALTER <table-name> DROP COLUMN <column-name>;

Ex:- ALTER TABLE Student DROP COLUMN City;


TRUNCATE command

The TRUNCATE command is used to delete all the rows from the table,

the structure remains and the space is freed from the table.

TRUNCATE TABLE table-name;

Ex:- TRUNCATE TABLE Student;


DROP TABLE command

The DROP TABLE command is used to remove a table from the database. If
you drop a table, all the rows in the table is deleted and the table structure
is removed from the database. Once a table is dropped we cannot get it
back.

DROP TABLE table-name;

Ex:- DROP TABLE Student;


SQL Constraints

Constraints are the rules that we can apply on the type of data in a table.
This ensures the accuracy and reliability of the data in the database.
Constraints could be either on a column level or a table level. The column
level constraints are applied only to one column, whereas the table level
constraints are applied to the whole table.

Column-level constraint

Column-level constraints refer to a single column in the table and do not


specify a column name (except check constraints). They refer to the
column that they follow.
Column level constraints

NOT NULL - Ensures that a column cannot have a NULL value


UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
CHECK - Ensures that all values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column when no value is specified
NOT NULL Constraint

By default, a column can hold NULL values.


The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value.

CREATE TABLE Student(adno integer NOT NULL,


Name varchar(60), Age integer);

The above query will declare that the adno field of Student table will not
take NULL value
UNIQUE Constraint

This constraint ensures that no two rows have the same value in the specified
columns. For example UNIQUE constraint applied on Admno of student table
ensures that no two students have the same admission number

CREATE TABLE Student(adno integer NOT NULL UNIQUE,


Name varchar(60), Age integer);

The above command will declare that the adno field of Student table will only
have unique values and wont take NULL value.
PRIMARY KEY Constraint
Primary key constraint uniquely identifies each record in a table. A Primary
Key must contain unique value and it must not contain null value. You can
have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.

CREATE TABLE Student (adno integer PRIMARY KEY,


Name varchar(60) NOT NULL, Age integer);

In the above example the adno field has been set as primary key and
therefore will help us to uniquely identify a record.
DEFAULT Constraint

The DEFAULT constraint is used to assign a default value for the field. When
no value is given for the specified field having DEFAULT constraint,
automatically the default value will be assigned to the field.

CREATE TABLE Student (adno integer PRIMARY KEY,


Name varchar(60) NOT NULL, Age integer DEFAULT=17);

In the above example the “Age” field is assigned a default value of 17, therefore
when no value is entered in age by the user, it automatically assigns 17 to Age.
CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a
column. If you define a CHECK constraint on a single column it allows only
certain values for this column.

CREATE table Student( adno integer NOT NULL CHECK(adno < 19),
Name varchar(60) NOT NULL, Age integer );

In the above example the check constraint is set to Age field where the value
of Age must be less than 19
a table-level constraint

Table-level constraints refer to one or more columns in the table. Table-


level constraints specify the names of the columns to which they apply. We
cannot use NOT NULL as a table level constraint.

CREATE TABLE Student(Adno integer NOT NULL ,


Name varchar(60), Age integer, Aadhar integer table
PRIMAY KEY(Adno, Aadhar) level
constraint
);
Data Manipulation Language
Commands
DML COMMANDS

(i) INSERT command

The INSERT command helps to add new data to the table or add new
records to the table. The command is used as follows:

INSERT INTO table_name (column, column1, column2, column3, ...)


VALUES (value, value1, value2, value3 ...);

Ex:- INSERT INTO Student (Admno, Name, Gender, Age, Place)


VALUES (100,’ Ramesh’,’ M’, 17,’ Chennai’);

INSERT INTO Student VALUES ( 102, ‘Balaji’, ‘M’, ‘17,’ ‘Bangalore’);


DML COMMANDS

(ii) DELETE COMMAND

The DELETE command permanently removes one or more records from the table.
It removes the entire row.

DELETE FROM table-name WHERE condition;

DELETE FROM Student WHERE Admno=104; Single record

DELETE FROM Student WHERE Age = 20; multiple records


Ex:-
DELETE * FROM Student; All the records
(the table will be empty now)
DML COMMANDS

(iii) UPDATE COMMAND

UPDATE command is used to update any record of data in a table. Following


is its general syntax.

UPDATE table_name
SET column=value, column1=value1,...
WHERE someCondition;

Ex:- UPDATE Student SET Age = 20 WHERE Place = “Bangalore”;

UPDATE Student SET Age=18, Place = ‘Chennai’ WHERE Admno = 102;


SELECT query

DQL COMMAND– SELECT

The SELECT command is used to query or retrieve data from a


table in the database. It is used to retrieve a subset of records
from one or more tables.

SELECT <column-list> FROM <table-name>;


SELECT query
Example table :-

Admno Name Gender Age Place


100 Arun M 17 Chennai
101 Balaji M 18 Delhi
102 Chandru M 17 Bangalore
103 Durai Raj M 18 Delhi
104 Mathan M 18 Chennai
105 Revathi F 19 Chennai
106 Devika F 19 Bangalore
107 Hema F 17 Chennai

Ex:- SELECT Admno, Name FROM Student;


SELECT * FROM STUDENT;
SELECT query
(i) DISTINCT Keyword

The DISTINCT keyword is used along with the SELECT command to eliminate
duplicate rows in the table. This helps to eliminate redundant data.

SELECT DISTINCT Place FROM Student;

(ii) ALL Keyword

The ALL keyword retains duplicate rows. It will display every row
of the table without considering duplicate entries.

SELECT ALL Place FROM Student;


SELECT query
(iii) The WHERE clause
The WHERE clause in the SELECT command specifies the criteria for getting
the desired result.

SELECT <column-name>[,<column-name>,….] FROM <table-name>


WHERE condition>;

SELECT Admno, Name, Place FROM Student WHERE Place =”Chennai”;

The relational operators like =, <, <=, >, >=, <> and the logical operaors OR, AND
and NOT can also be used to connect search conditions in the WHERE clause.

SELECT Name, Age, Place FROM Student WHERE (Age>=18 AND Place = "Delhi");
SELECT query
(iv) BETWEEN and NOT BETWEEN Keywords

The BETWEEN keyword defines a range of values the record must fall into
to make the condition true.

SELECT Admno, Name, Age FROM Student WHERE Age BETWEEN 18 AND 19;

(v) IN Keyword IS NULL

The IN keyword is used to specify a list of values which must be


matched with the record values.

SELECT Name, Place FROM Student WHERE Place IN (“Chennai”, “Delhi”);

SELECT Name, Place FROM Student WHERE Place NOT IN (“Chennai”, “Delhi”);
SELECT query

(vi) ORDER BY clause

The ORDER BY clause in SQL is used to sort the data in either ascending or
descending based on one or more columns.

DESC  descending order ; ASC  ascending order.

SELECT * FROM Student ORDER BY Name;

SELECT * FROM Student WHERE Age>=18 ORDER BY Name DESC;

The ORDER BY clause does not affect the original table.


Sorting can be done on multiple fields.
SELECT query

(vii) GROUP BY clause

The GROUP BY clause groups rows that have the same values into summary rows.
It is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group
the result-set by one or more columns.

SELECT Gender FROM Student GROUP BY Gender;

SELECT Gender, count(*) FROM Student GROUP BY Gender;


SELECT query

(viii) HAVING clause

The HAVING clause can be used along with GROUP BY clause in the
SELECT statement to place condition on groups and can include
aggregate functions on them.

SELECT Gender , count(*) FROM Student GROUP BY Gender


HAVING Place = ‘Chennai’;
(i) COMMIT command
The COMMIT command is used to permanently save any transaction to the
database. When any DML commands like INSERT, UPDATE, DELETE commands
are used, the changes made by these commands are not permanent. It is marked
permanent only after the COMMIT command is given from the SQL prompt. Once
the COMMIT command is given, the changes made cannot be rolled back.

COMMIT;

(ii) ROLLBACK command

The ROLLBACK command restores the database to the last commited state. It is
used with SAVEPOINT command to jump to a particular savepoint location.

ROLL BACK TO save point name;


(iii) SAVEPOINT command

The SAVEPOINT command is used to temporarily save a transaction so


that you can rollback to the point whenever required.

SAVEPOINT savepoint_name;

INSERT INTO Student VALUES(106, ‘Kamali', 'F', 19, 'Delhi');


SAVEPOINT B;
ROLLBACK TO A;
YOU ARE

WE ARE

48
Prepared by : S. Ganesh Kumar, B.Sc., B.Ed., M.S.I.T.,

You might also like