Professional Documents
Culture Documents
Nahida Nazir
Drop UNIQUE KEY
❖ALTER TABLE Persons
❖DROP CONSTRAINT uk;
Nahida Nazir
NotNull constraint
❖By default, a column can hold NULL
values.
❖The NOT NULL constraint enforces a
column to NOT accept NULL values.
❖This enforces a field to always contain a
value, which means that you cannot insert
a new record, or update a record without
adding a value to this field.
Nahida Nazir
Check
❖ 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.
❖ If you define a CHECK constraint on a table it
can limit the values in certain columns based
on values in other columns in the row.
Nahida Nazir
Check constraint
❖CREATE TABLE CUSTOMERS
❖(
❖ID INT,
❖PRIMARY KEY(ID) ,
❖NAME VARCHAR(20),
❖AGE INT ,
❖Constraint ck CHECK(AGE >= 18),
❖ADDRESS CHAR(25) ,
❖SALARY DECIMAL
❖);
Nahida Nazir
Adding check constraint
❖ALTER TABLE Customers
❖ADD Constraint ck CHECK(AGE >=
18);
Nahida Nazir
Default
❖The DEFAULT constraint is used to
provide a default value for a column.
❖The default value will be added to all new
records IF no other value is specified.
Nahida Nazir
Code
⚫CREATE TABLE jk (
⚫ ID int NOT NULL,
⚫ LastName varchar(255) NOT NULL,
⚫ FirstName varchar(255),
⚫ Age int,
⚫ City varchar(255) DEFAULT 'Sandnes'
⚫);
⚫insert into jk values(67,'k','l',32, default);
⚫select *from jk;
Nahida Nazir
Drop a default constraint
⚫ALTER TABLE Persons
⚫Modify City Default Null;
Nahida Nazir
Drop Constraint
❖ALTER TABLE Customers
❖Drop Constraint ck;
Nahida Nazir
Foreign Key
❖A FOREIGN KEY is a key used to link
two tables together.
❖A FOREIGN KEY is a field (or collection
of fields) in one table that refers to the
PRIMARY KEY in another table.
❖The table containing the foreign key is
called the child table, and the table
containing the candidate key is called the
referenced or parent table.
Nahida Nazir
Code of foreign Key
create table laptop33(id int not null primary key,name varchar(20),
brand varchar(22), processor varchar(20));
insert into laptop33 values(77,'xolo','chinies','i7');
insert into laptop33 values(87,'samsung','chinies','i7');
insert into laptop33 values(97,'sony','chinies','i7');
insert into laptop33 values(47,'hp','ch','i7');
select*from laptop33;
create table dept313(dep_id int, name varchar(20), branch varchar(20),
company varchar(21) ,id int, foreign key(id) references laptop33(id));
insert into dept313 values (89,'physics','science','sw',77);
insert into dept313 values (99,'chemistry','sci','rw', 87);
insert into dept313 values (79,'biology','sci','qw',97);
insert into dept313 values (9,'abc','sci','qw',47);
select*from dept313;
Nahida Nazir
Result
Nahida Nazir
Candidate Key
❖A candidate key is a set of attributes (or
attribute) which uniquely identify the
tuples in relation or table. As we know
that Primary key is a minimal super key,
so there is one and only one primary key
in any relationship but there is more than
one candidate key can take place
Nahida Nazir
Candidate key
Nahida Nazir
Composite Key
❖ A key that has more than one attributes is
known as composite key. It is also known
as compound key.
❖ Any key such as super key, primary key,
candidate key etc. can be called composite
key if it has more than one attributes.
Nahida Nazir
Composite Key
Nahida Nazir
Alternate Key
As we have seen in the candidate key
guide that a table can have multiple
candidate keys. Among these candidate
keys, only one key gets selected as
primary key, the remaining keys are
known as alternative or secondary keys.
Nahida Nazir
Super Key
❖A super key is a set of one or more
attributes (columns), which can uniquely
identify a row in a table.
❖ Candidate keys are selected from the set
of super keys, the only thing we take care
while selecting candidate key is: It should
not have any redundant attribute. That’s
the reason they are also termed as
minimal super key
Nahida Nazir
SQL operators
Nahida Nazir
Arithmetic operator
Nahida Nazir
Suubtraction
Nahida Nazir
Multiplication
Nahida Nazir
Division
Nahida Nazir
Relational Operator
❖< less than
❖ > greater than
❖ <= less than or equal to
❖ >= greater or equal to
❖ = equal to
❖ != not equal to
Nahida Nazir
syntax
create table stu_details( id int, name varchar(20),
branch varchar(21), section varchar(23), fee float);
insert into stu_details values(67,'raj','cse','gf',4300)
insert into stu_details
values(97,'rohan','electrical','gf',5500)
insert into stu_details
values(87,'vikas','cse','gh',4679)
insert into stu_details
values(47,'riyan','civil','rt',8000)
select name from stu_details where fee<8000;
Nahida Nazir
output
Nahida Nazir
Not Null operator
Select*from stu_details where fee!=4679
Nahida Nazir
And
select*from stu_details where branch='cse'
and section='gf';
Nahida Nazir
OR
select*from stu_details where id=67 or
branch ='cse';
Nahida Nazir
Not
select*from stu_details where not
section='rt';
Nahida Nazir
Sorting
Sorting helps to sort the records that are
retrieved. By default, it displays the
records in ascending order of primary key.
If we need to sort it based on different
columns, then we need to specify it in
ORDER BY clause
Nahida Nazir
Nahida Nazir
Nahida Nazir
Row id
❖ ROWID:For each row in the database, the ROWID
pseudocolumn returns a row\’s address. The ROWID
contains 3 information about row
address:FileNo : FileNo means Table Number.
❖ DataBlockNo : DataBlockNo means the space assigned
by the oracle sql engine to save the record.
❖ RecordNo : Oracle engine mantains the record number
for each record.
Nahida Nazir
Syntax
❖ SELECT ROWID, address FROM neha4
WHERE id = 5;
Nahida Nazir
Aggregate Functions
An aggregate function is a function where
the values of multiple rows are grouped
together as input on certain criteria to
form a single value of more significant
meaning.
Nahida Nazir
Aggregate functions
1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()
Nahida Nazir
Count(*)
Select COUNT(*) from epm2 where da >
2000;
Nahida Nazir
Group by
Nahida Nazir
Having clause
Nahida Nazir
Joins
Nahida Nazir
Types of Joins
Nahida Nazir
Natural join
Nahida Nazir
Inner join
Nahida Nazir
Outer join
Nahida Nazir
Cross join
Nahida Nazir
Self join
Nahida Nazir