SQL
SQL> select max(sal), deptno from emp group by deptno;
MAX(SAL) DEPTNO
———- ———-
8000 10
12910 20
11910 30
SQL> select max(sal), deptno from emp group by deptno having count(*) > 3;
MAX(SAL) DEPTNO
———- ———-
12910 20
11910 30
SQL> select max(sal), deptno from emp group by deptno;
MAX(SAL) DEPTNO
———- ———-
8000 10
12910 20
11910 30
SQL> select max(sal), deptno from emp group by deptno having count(*) > 3;
MAX(SAL) DEPTNO
———- ———-
12910 20
11910 30
Count Function
In order to count the number of rows, count function is used. It can take three different arguments.
Count(*), Count(Col_name), Count(distinct col_name).
Count(*)
It counts all rows, inclusive of duplicates and Nulls.
select count(*) from emp; 13
Count(colname)
It counts the number of values present in the column without including Nulls.
select count(deptno) from emp; 13
Count(distinct colname)
It is similar to count(col_name) but eliminates duplicate values while counting.
select count(distinct deptno) from emp; 3
Next_day
SQL> select next_day(d,’tuesday’) from mtemp;
NEXT_DAY(
———
Round
SQL> select round(d,’year’) from mtemp (
2 ;
ROUND(D,’
———
select trunc(d,’year’) from mtemp
TRUNC(D,’
———
Greatest
1* select greatest(sysdate,d) from mtemp
SQL> /
GREATEST(
———
Numeric Functions
select abs(-14.54) from dual;14.54
select ceil(14.82) from dual;15
select floor(14.82) from dual;14
select power(4,2) from dual;16
select mod(4,3) from dual;1
select trunc(100.256,2) from dual;100.25
select sqrt(2) from dual;1.414
Privilege Command
The Grant & Revoke are called as the privilege commands.
Grant Command
Grant privileges on <object-name > to <user name>;
Grant all on customer to user1;
Grant insert, update on customer to user1;
Revoke privileges on <object-name > from <user name>;
Revoke all on customer from user1;
Revoke insert, update on customer from user1;
Select Command
Select column_name …. From table_name;
Select * from emp;
Select no from emp;
Select distinct no from emp;
Select * from emp where no = 101;
Select no,name,dno from emp order by name;
Select no,name,dno from emp order by name,dno desc;
Update Command
Update table_name set field = value,… where condition;
Update emp set salary = 5000 where no = 101;
Delete Command
Delete from <table_name> where condition;
Delete from emp where dno = 10;
Integrity Constraints.
It is a mechanism used by oracle to prevent invalid data entry into the table. It is enforcing rule for the column in a table.
Not Null
SQL> create table mtemp(no number(3) constraint kk not null,cname varchar2(10));
Table created.
Check
SQL> create table mtemp(no number(3) check(no >10));
Table created.
Unique
SQL> create table mtemp(no number(3) unique);
Table created.
Primary Key
SQL> create table mtemp(no number(3),name varchar2(10),primary key(no));
Table created.
Foreign Key
SQL> create table mtemp1(no number(3),name varchar2(10), primary key(no))
Table created.
SQL> create table mtemp2(eno number(3) primary key,
2 no number(3),foreign key(no) references mtemp1(no))
Table created.
1 create table mtemp2(eno number(3) primary key,
2* no number(3),foreign key(no) references mtemp1(no) on delete cascade)
Table created.
Number
Date
Long Raw
Character :
Varchar2
Long
Hi everybody…..Hereafter you will find some informative articles on SQL basics. I hope this will be interesting.
Database Models:
Object based logical model: It describes data and data relationship constraints.
Record based logical model: It describes data structure and access techniques.
Hierarchical Model
It has a simple structure and it is easy to use
The relationship is parent child.
It reduces the redundancy
Drawbacks
Leads to wastage of storage space
Many-to-Many relationship is not possible
Deletion of parent results in deletion of children which leads to anomaly.
Network Model
- It has improvement over Hierarchical model.
- Multiple parent-child relationships is possible.
- Many-to-Many relationship is possible
- Parent(node) & link.
- The speed of retrieving is more when compared to Hierarchical Model.
Drawbacks
- Complex to construct
- The pointer maintenance leads to drawbacks.
Relational Model
- The data is stored in rows & columns.
- Each column has a unique name called the attribute.
- Relational operators are allowed
- Mathematical theory
- Single (Restrict, Project)
- Binary (union, intersect, product, join, difference, divide)