Fy Sem2 Notes Unit1
Fy Sem2 Notes Unit1
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
NOT NULL - Ensures that a column cannot have a NULL value
id name
1 ram
2 ravan
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table.only one column has applied primary key,
no duplicate data.
• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
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' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output− Selects tuples from books where subject is 'database' and 'price' is 450 or
those books published after 2010.
For example−
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Notation− r U s
Where r and s are either database relations or relation result set (temporary
relation).
Output − Projects the names of the authors who have either written a book or an
article or both.
Notation− r −s
Output− Provides the name of authors who have written books but not articles.
Notation − r Χ s
Output − Yields a relation, which shows all the books and articles written by
tutorialspoint.
Notation −ρ x(E)
DQL : select.
Aggregate Functions
table- emp
Name Salary
A 5000
B 3000
C 1000
1)MIN( )
returns the smallest value in a given column
O/p
min(salary)
1000
2)MAX( )
O/p
max(salary)
5000
3) SUM( )
O/p
Sum(salary)
9000
4) AVG( )
O/p
Avg(salary)
3000
5) COUNT( )
O/p
Count(salary)
6) COUNT(*)
O/p
Count(*)
...............................
Extra info......
O/p
Name Salary
C 1000
....................................
DDL(Data Definition Language) :DDL or Data Definition Language actually
consists of the SQL commands that can be used to define the database
schema. It simply deals with descriptions of the database schema and is used
to create and modify the structure of database objects in database.
DDL is set of commands used to create ,modify and delete database structure but not data,This are
used by DBA’s(Database Administrators) ,DDL command would commit all updates ,hence they
can not rollback (not reversed).
• Create
• drop
create -create statement used for creating tables ,creating structure of table specyfying column
name,datatype and size .integrity constraints also applied like check,default,primary key ,foreign
key,
syntax
columnname datatype(size)
..........
);
example
name varchar(20)
);
1)add column
2)modify column
1)drop column
Drop table:
it is the area of sql that allows changing data within the database.
-insert
-update
-delete
Syntax-
suppose we want to add employee information like empno and ename then insert into statement will
be-
empno empname
100 Ajay patil
synatx-
update tablename
set col=value
where col=value;
suppose we want to update empname ajay patil to abheejeet patil whose empno is 100
update empl
empno empname
100 Abheejeet patil
Here old value ajay patil is changed to Abheejeet patil
here row will be deleted from empl table whose empno was 100
you can rollback data if you used savepoint for delete statement.
...............................................................................................................................................
WHERE Clause
Description
The SQL WHERE clause is used to filter the results and apply conditions in a
SELECT, INSERT, UPDATE, or DELETE statement.
Example - One Condition in the WHERE Clause
roll name
1 Ajay mane
2 manoj
3 rahul
4 Ajay sane
Enter the following SQL statement:
roll name
2 manoj
AND Condition
roll name
3 rahul
4 Ajay sane
.
OR Condition
SELECT *
FROM student
SELECT expressions
FROM tables
[WHERE conditions]
example
abc................z
roll name
1 Ajay mane
4 Ajay sane
2 manoj
3 rahul
roll name
3 rahul
2 manoj
4 Ajay sane
roll name
1 Ajay mane
Group by clause:
The SQL GROUP BY clause can be used in a SELECT statement to collect data
across multiple records and group the results by one or more columns.
Syntax
The syntax for the GROUP BY clause in SQL is:
SELECT ....
FROM tables
[WHERE conditions]
carname presentcars
tata 4
maruti 5
tata 4
maruti 10
SELECT carname,presentcars
FROM parking
GROUP BY carname;
carname presentcars
tata 4
tata 4
maruti 5
maruti 10
Using aggregate function in group by
SELECT carname,sum(presentcars)
FROM parking
GROUP BY carname;
carname presentcars
tata 4+4=8
maruti 5+10=15
HAVING clause
group by----having
The SQL HAVING clause is used in combination with the GROUP BY clause to
restrict the groups of returned rows to only those whose the condition is TRUE.
SELECT carname,sum(presentcars)
FROM parking
GROUP BY carname
having carname=’tata’;
carname presentcars
tata 4+4=8
DQL : select
Description
The SQL SELECT statement is used to retrieve records from one or more tables
in your SQL database. The records retrieved are known as a result set.
Syntax
The syntax for the SELECT statement in SQL is:
SELECT expressions
FROM tables
[WHERE conditions]
roll name
1 ram
3 raj
2 sam
Example
SELECT roll,name
FROM student
WHERE roll=’3’;
roll name
3 raj
The SQL AND conditionandOR condition can be combined to test for multiple
conditions in a SELECT, INSERT, UPDATE, or DELETE statement.
When combining these conditions, it is important to use parentheses so that
the database knows what order to evaluate each condition.
roll name
1 ram
3 raj
2 sam
AND-logic
Example
SELECT roll,name
FROM student
roll name
3 raj
OR -logic
t or t =t
SELECT roll,name
FROM student
roll name
3 raj
Relational operators:
SELECT roll,name
FROM student
roll name
3 raj
roll name
1 ram
3 raj
roll name
1 ram
2 sam
The SQL IN condition (sometimes called the IN operator) allows you to easily
test if an expression matches any value in a list of values
id carname
100 maruti
101 tata
102 swift
roll name
1 ram
2 raj
3 sam
Like operator:
The SQL LIKE condition allows you to use wildcards to perform pattern
matching in a query.
roll name
1 ram
3 sam
roll name
1 rajiv
2 jitendra
IS NULL:
The IS NULL condition is used in SQL to test for a NULL value. It returns
TRUE if a NULL value is found, otherwise it returns FALSE
roll name
1 ram
2
output:
roll name
2 NULL
NOT:
The SQL NOT condition (sometimes called the NOT Operator) is used to negate
a condition in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE
statement.
roll name
3 sam