By Balraj and Ziya
By Balraj and Ziya
By
Balraj and Ziya
RDBMS
RDBMS is an acronym for Relational Database
Management System. The data in RDBMS is stored in
database objects called tables. The database tables
are the primary data storage for every RDBMS and
essentially they are collections of related data entries.
For example a table called Users might store
DBMS store the data into group of tables, which might or might not
be related by common fields (database table columns).
CREATE
USE
ALTER
DROP
Create -SQL
Syntax:
CREATE TABLE tablename
(column_name data_ type constraint, column_name data_ type
constraint);
CREATE TABLE Customer_Details
(Cust_ID Number(5),
Cust_Last_Name VarChar(20),
Cust_Mid_Name VarChar(4),
Cust_First_Name VarChar(20),
Account_No Int,
Account_Type VarChar(10),
Bank_Branch VarChar(25),
Cust_Email VarChar(30));
SQL - ALTER TABLE
Syntax
ALTER TABLE tablename (ADD/MODIFY/DROP) column_name
TRUNCATE:
The TRUNCATE statement is similar to a DELETE statement
without a WHERE clause, except for the following:
TRUNCATE is very fast on both large and small tables. DELETE will
generate undo information, in case a rollback is issued, but
TRUNCATE will not generate undo.
DELETE does not release the memory TRUNCATE releases the memory
occupied by the records of the table occupied by the records of the table
Select-command
Select: It has 5 main clauses to choose from
FROM table1[,table2]
[WHERE condition]
[GROUP BY column-list]
[HAVING conditions]
Note:[]-Optional
Group by -example
O_id Order date Order price customer
Ram 2000
Ravi 1700
John 2000
Having Clause
The Having clause was added to SQL, because the
where keyword can not be used with aggregate
functions.
The same example with Having clause.
Select customer, sum(order price) from orders
Group by customer
Having sum(order price) >1700.
Customer Order price
Ram 2000
John 2000
Update and Delete
Update:
Syntax:
UPDATE table name
SET Column name=[new value]
WHERE {condition}
Delete:
Syntax:
DELETE from table name
WHERE {condition}
DCL-Commands
DCL statements are used for securing the database
and control the access to database.
Grant
Revoke
USE Dtabase_name
Grant select on table name
to public
USE Dtabase_name
Revoke select on table name
to public
Aggregate functions
1. JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETE
data query statements to simultaneously affect rows from multiple tables.
There are several distinct types of JOIN statements that return different data
result sets.
2. Joined tables must each include at least one field in both tables that contain
comparable data.
For example, if you want to join a Customer table and a
Transaction table, they both must contain a common element, such as
CustomerID column, to serve as a key on which the data can be matched. Tables
can be joined on multiple columns so long as the columns have the potential to
supply matching information. Column names across tables don't have to be the
same, although for readability this standard is generally preferred.
The basic JOIN statement
1. SELECT Customer.CustomerID, TransID, TransAmt
FROM Customer JOIN Transaction
ON Customer.CustomerID = Transaction.CustomerID;
For example, if we want to list all employees and their employee number, but not all
employees have a number, then we could say (in SQL):
The "*=" means "left outer join" and means that all rows from the "employee" table will
appear in the result, even if there is no match for their ID in the empnum table.
The notorious CROSS JOIN
The CROSS JOIN has earned a bad reputation because it’s very resource
intensive and returns results of questionable usefulness. When you use the
CROSS
JOIN, you're given a result set containing every possible combination of the rows
returned from each table. Take the following example:
With the CROSS JOIN, you aren’t actually free to limit the results, but you can
use
The ORDER BY clause to control the way they are returned. If the tables joined in
this example contained only five rows each, you would get 25 rows of results.
Every Customer Name would be listed as associated with every TransDate and
TransAmt.
I really did try to come up with examples where this function was useful, and
they
were all very contrived. However, I’m sure someone out there is generating lists
of
all their products in all possible colors or something similar, or we wouldn’t
have
this wonderful but dangerous feature.
The OUTER JOIN can include mismatched rows
The first function performed is an INNER JOIN. The second function includes
the rows that the INNERJOIN would have dropped. Which rows are included
depends on the type of OUTER JOIN that is used and the order the tables
were presented.
There are three types of an OUTER JOIN: LEFT, RIGHT, and FULL. As you’ve
Probably guessed, the LEFT OUTER JOIN keeps the stray rows from the “left”
table (the one listed first in your query statement). In the result set, columns
from the other table that have no corresponding data are filled with NULL
values.
Similarly, the RIGHT OUTER JOIN keeps stray rows from the right table, filling
columns from the left table with NULL values. The FULL OUTER JOIN keeps all
stray rows as part of the result set. Here is your example:
Continued..
SELECT Customer Name, TransDate, TransAmt FROM Customer LEFT
OUTER JOIN Transaction ON Customer. CustomerID =
Transaction. CustomerID;
The clauses LEFT JOIN, RIGHT JOIN, and FULL JOIN are equivalent to
LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN,
respectively.
THE END