Xiicsl12 em
Xiicsl12 em
Xiicsl12 em
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
Developed at IBM
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
17
Creating Database
DDL Commands
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.
The TRUNCATE command is used to delete all the rows from the table,
the structure remains and the space is freed from the table.
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.
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
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
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.
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.
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
The INSERT command helps to add new data to the table or add new
records to the table. The command is used as follows:
The DELETE command permanently removes one or more records from the table.
It removes the entire row.
UPDATE table_name
SET column=value, column1=value1,...
WHERE someCondition;
The DISTINCT keyword is used along with the SELECT command to eliminate
duplicate rows in the table. This helps to eliminate redundant data.
The ALL keyword retains duplicate rows. It will display every row
of the table without considering duplicate entries.
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;
SELECT Name, Place FROM Student WHERE Place NOT IN (“Chennai”, “Delhi”);
SELECT query
The ORDER BY clause in SQL is used to sort the data in either ascending or
descending based on one or more columns.
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.
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.
COMMIT;
The ROLLBACK command restores the database to the last commited state. It is
used with SAVEPOINT command to jump to a particular savepoint location.
SAVEPOINT savepoint_name;
WE ARE
48
Prepared by : S. Ganesh Kumar, B.Sc., B.Ed., M.S.I.T.,