Professional Documents
Culture Documents
Syntax:
---------------------
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pets |
| sakila |
| studentinfo |
| sys |
| world |
+--------------------+
SQL USE Statement
The USE Statement is used to select a database and perform SQL
operations into that database.
The database remains default until end of session or execution of
another USE statement with some other database.
Syntax:
----------------
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| sid | char(20) | YES | | NULL | |
| name | char(25) | YES | | NULL | |
| login | char(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gpa | double | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
If you change the object's name any reference to the old name
will be affected.
SQL Truncate
The SQL TRUNCATE command is used to delete all the rows from
the table and free the space containing the table.
SQL Drop DataBase
The DROP DATABASE Statement is used to drop or delete a
database.
if you want use the table again it has to be recreated with the
integrity constraints, access privileges and the relationships with
other tables should be established again.
For example, when you want to see the information about students
in class 10th only then you do need the information about the
students in other class.
The condition you provide in the WHERE clause filters the rows
retrieved from the table and gives you only those rows which you
expected to see.
NOTE: The group by clause should contain all the columns in the
select list expect those used along with the group functions.
SQL ORDER BY
SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
SQL GROUP Functions
SQL MIN( ): This function is used to get the minimum value from a
column.
Comparison Operators:
Comparison operators are used to compare the column data with
specific values in a condition.
When retrieving data using a SELECT statement, you can use logical
operators in the WHERE clause, which allows you to combine more than
one condition.
Logical Operators Description
For the row to be
selected at least one of
OR
the conditions must be
true.
For a row to be selected
AND all the specified
conditions must be true.
For a row to be selected
NOT the specified condition
must be false.
SQL Comparison Keywords
There are other comparison keywords available in sql which are
used to enhance the search capabilities of a sql query. They are
"IN", "BETWEEN...AND", "IS NULL", "LIKE".
Comparision
Description
Operators
column value is similar
LIKE
to specified character(s).
column value is equal to
IN any one of a specified
set of values.
column value is between
two values, including
BETWEEN...AND
the end values specified
in the range.
column value does not
IS NULL
exist.
SQL INSERT Statement
UPDATE table_name
SET column_name1 = value1,
column_name2 = value2, ...
[WHERE condition]
To delete all the rows from the employee table, the query would be like,
DELETE Statement: This command deletes only the rows from the
table based on the condition given in the where clause or deletes
all the rows from the table if no condition is specified. But it does
not free the space containing the table.
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
privilege_name : is the access right or privilege granted to the user. Some
of the access rights are ALL, EXECUTE, and SELECT.
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
TCL command
Commit command:
Commit command is used to permanently save any transaction into
database.
Following is Commit command's syntax,
Syntax: commit;
Rollback command:
This command restores the database to last committed state. It is also
use with savepoint command to jump to a savepoint in a transaction.
Following is Rollback command's syntax,
Syntax: rollback to savepoint-name;
Savepoint command:
savepointcommand is used to temporarily save a transaction so that
you can rollback to that point whenever necessary.
Following is savepoint command's syntax,
Syntax: savepoint savepoint-name;
Example of Savepoint and Rollback:
ID NAME
1 abhi
2 adam
4 Alex
rollback to B;
SELECT * from class;
The resultant table will look like
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
8 Chris
Now rollback to savepoint A
rollback to A;
SELECT * from class;
The result table will look like
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
Integrity constraints
An integrity constraints is a condition specified on a database
scheme and restrict the data that can be stored in an instance of the
database.
A legal instance of a relation is one that satisfies all specified ICs.
Key constraints
Key constraints:
Key constraints is a statement that a certain minimum subset of the fields which
uniquely identifies the tuple.
Key constraints are of two types:
1. primary key constraints
2. foreign key constraints
Candidate key is the set of fields that uniquely identifies the tuple according to key
constraints .A relation can have multiple Candidate key .
One of the candidate key is called as primary key by database designer.
Ex: login and age for student relation is also a key {login , age}
Primary key:
A primary key is one of the candidate keys chosen by the database designer to
uniquely identify the tuple.
Ex: sid
Foreign key:
A foreign key is a column or group of columns in a relational database table that
provides a link between data in two tables.
the foreign key is defined in a second table, but it refers to the primary key in the
first table.
Specifying primary key:
Create table students (sid char(20) primary key, name char(20), login
char(30), age int, gpa real)
OR
name
ssn lot
Employees
CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
Relationship Sets to
Tables CREATE TABLE Works_In(
ssn CHAR(1),
In translating a did INTEGER,
relationship set to a since DATE,
relation, attributes of the PRIMARY KEY (ssn, did),
relation must include: FOREIGN KEY (ssn)
Keys for each
REFERENCES Employees,
FOREIGN KEY (did)
participating entity set
REFERENCES Departments)
(as foreign keys).
This set of attributes
forms a superkey for
the relation.
All descriptive attributes.
Review: Key
Constraints
since
name dname
Each dept has at
most one ssn lot did budget
manager,
according to the Employees Manages Departments
key constraint
on Manages.
Translation to
relational model?
Works_In
since
Participation Constraints
in SQL
We can capture participation constraints
involving one entity set in a binary relationship,
but little else (without resorting to CHECK
constraints).
CREATE TABLE Dept_Mgr(
did INTEGER,
dname CHAR(20),
budget REAL,
ssn CHAR(11) NOT NULL,
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE NO ACTION)
Review: Weak Entities
A weak entity can be identified uniquely only by
considering the primary key of another (owner)
entity.
Owner entity set and weak entity set must participate
in a one-to-many relationship set (1 owner, many weak
entities).
Weak entity set must have total participation in this
identifying relationship set.
name
cost pname age
ssn lot
hourly_wages hours_worked
ISA
As in C++, or other PLs, attributes contractid
are inherited.
If we declare A ISA B, every A entity Hourly_Emps Contract_Emps
is also considered to be a B entity.