Dbms 2
Dbms 2
Dbms 2
3. customers(customer_id,customer_name,address,city,postalcode,
country)
Structured Query Language(SQL)
An open source Domain Specific language developed to create,
maintain and retrieve data from relational databases, e.g. MySQL,
Oracle, SQL server etc.
Used to work with Structured data
Not a case sensitive language. But in routine practice keywords are
written using Capital letters and user defines values are written in small
letters
The data types and syntax in SQL may slightly vary from vendor to
vendor
Highly scalable and flexible
Can manage numerous transactions and heavy workload
Provides high security through various authorization constraints
It works for every small or large organization
SQL Data Types
1. Numeric data type
Integer types: INTEGER, SMALL INT, TINY INT, MEDIUM INT, BIG INT
Fixed point values: DECIMAL
Floating point values: FLOAT, DOUBLE
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Eg. ALTER TABLE Persons
MODIFY COLUMN email varchar(150);
The DROP TABLE statement is used to drop an existing table in a database.
Syntax
DROP TABLE table_name;
Eg. Drop table dept;
The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.
Eg.
TRUNCATE TABLE Categories;
Try it Yourself
RENAME command is used to set a new name for any existing table.
syntax.
RENAME TABLE old_table_name to new_table_name
RENAME TABLE
RENAME table command is used to rename or change the name of
existing table.
Eg. Rename emp to employee
Data Manipulation Language(DML)
The SELECT DISTINCT statement is used to return only distinct (different) values.
Select distinct country from customers;
(to select distinct country and to eliminate duplicate country)
NOT NULL: It ensures that a column will never hold a NULL value
e.g. Create table student (roll_no integer NOT NULL)
UNIQUE : It ensures that a column will have unique value for all
tuples in a relation. It allows NULL value
e.g. Create table employee (phoneno integer Unique)
Referential Integrity Constraints
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The Group By clause is used for organizing similar data into groups. It
means, if different rows in a precise column have the same values, it
will arrange those rows in a group.
Syntax :
SELECT column1, function_name(column2) FROM table_name
WHERE condition GROUP BY column1;
e.g. Select deptno,sum(sal) from emp
group by deptno;
Select deptno,count(empno) from emp
group by empno;
Having Clause
The HAVING clause was added to SQL because the WHERE clause cannot be used
with aggregate functions .It is used to filter records after group by clause
5. RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
eg. select empno,ename,dept.deptno,dname,location from emp right
outer join dept on emp.deptno=dept.deptno;
6. FULL (OUTER) JOIN: Returns all records matching in both tables and also
non matching records from both tables
eg. select empno,ename,dept.deptno,dname,location from emp
full outer join dept on emp.deptno=dept.deptno;
Views in SQL
View is a Virtual Table which is created from original table/tables in
database
View can be created by fetching attributes from one or more tables
Views are created in order to simplify complex queries and to
maintain security by providing restricted access to database
It may consist of either all rows of a original table or only limited rows
that satisfy condition specified in query
View does not have physical existence i.e. It does not occupy
physical memory(except Materialized view),Only the query creating
view is stored in data dictionary
View is created dynamically each time user executes query using
view name.
Types of Views
1. Simple View: It is created on a single table.It does not contain any function or
Group By clause.
eg. Create view empview as
select empno,salary from emp where salary>50000;
2. Complex View: It is created from by fetching data from one or multiple tables. It
contains functions, JOIN conditions, Order by, Group by clause.
eg. Create view emp_dept as
select deptno,count(empno) from emp group by deptno;
Select
Project
Union
Set difference
Cartesian product
Rename
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation.
p is prepositional logic formula which may use connectors like and,
or, and not. These terms may use relational operators like − =, ≠, ≥, <
, >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and
'price' is 450.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated
For example − ∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Candidate Key: Minimal Super key is candidate key. there is one and
only one primary key in any relationship but there is more than one
candidate key . Candidate key’s attributes can contain a NULL value
which opposes to the primary key.