0% found this document useful (0 votes)
17 views7 pages

MISCELLANEOUS FUNCTIONS

The document outlines various SQL functions and constraints used in database management, including miscellaneous functions like UID, USER, VSIZE, RANK, and DENSE_RANK, as well as conversion functions such as TO_NUMBER and TO_DATE. It details group functions like SUM, AVG, MAX, MIN, and COUNT, and explains different types of constraints including NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY, along with their usage and syntax. Additionally, it covers operations with constraints like enabling, disabling, enforcing, and dropping constraints.

Uploaded by

maheshtester9595
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

MISCELLANEOUS FUNCTIONS

The document outlines various SQL functions and constraints used in database management, including miscellaneous functions like UID, USER, VSIZE, RANK, and DENSE_RANK, as well as conversion functions such as TO_NUMBER and TO_DATE. It details group functions like SUM, AVG, MAX, MIN, and COUNT, and explains different types of constraints including NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY, along with their usage and syntax. Additionally, it covers operations with constraints like enabling, disabling, enforcing, and dropping constraints.

Uploaded by

maheshtester9595
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MISCELLANEOUS FUNCTIONS

 Uid
 User
 Vsize
 Rank
 Dense_rank
1) UID: This will give the integer value corresponding to the user currently logged in.
Ex: SQL> select uid from dual;

2) USER: This will give the login’s user name.


SQL> select user from dual;

3) VSIZE: This will give the number of bytes in the expression.


Ex: SQL> select vsize(123), vsize('computer'), vsize('18-Oct-22') from dual;

4) RANK: This will give the non-sequential ranking.


Ex: SQL> select rownum, sal from (select sal from emp order by sal desc) from dual;
ROWNUM SAL
1 5000
2 3000
3 3000
4 2975
5 2850
SQL> select rank(2975) within group(order by sal desc) from emp;

5) DENSE_RANK : This will give the sequential ranking.


Ex: SQL> select dense_rank(2975) within group(order by sal desc) from emp;

CONVERSION FUNCTIONS
 Bin_to_num
 Chartorowid
 Rowidtochar
 To_number
 To_char
 To_date
1) BIN_TO_NUM: This will convert the binary value to its numerical equivalent.
Syntax: bin_to_num( binary_bits)
SQL> select bin_to_num(1,1,0) from dual;

 If all the bits are zero then it produces zero.


 If all the bits are null then it produces an error.
2) CHARTOROWID
This will convert a character string to act like an internal oracle row identifier or rowid.
3) ROWIDTOCHAR
This will convert an internal oracle row identifier or rowid to character string.
4) TO_NUMBER
This will convert a char or varchar to number.
5) TO_CHAR
This will convert a number or date to character string.
6) TO_DATE
This will convert a number, char or varchar to a date.
GROUP FUNCTIONS
 Sum
 Avg
 Max
 Min
 Count
Group functions will be applied on all the rows but produces single output.
1) SUM: This will give the sum of the values of the specified column.
Syntax: sum (column)
SQL> select sum(sal) from emp;

2) AVG: This will give the average of the values of the specified column.
Syntax: avg (column)
SQL> select avg(sal) from emp;

3) MAX: This will give the maximum of the values of the specified column.
Syntax: max (column)
Ex: select max (salary) from A_SALARY;

4) MIN: This will give the minimum of the values of the specified column.
Syntax: min (column)
Ex: select min(salary) from A_SALARY;

5) COUNT: This will give the count of the values of the specified column.
Syntax: count (column)
Ex: select count (salary),count(*) from A_SALARY;

CONSTRAINTS
Constraints are categorized as follows.
Domain integrity constraints
 Not null
 Check
Entity integrity constraints
 Unique
 Primary key
Referential integrity constraints
 Foreign key
Constraints are always attached to a column not a table.
We can add constraints in three ways.
 Column level -- along with the column definition
 Table level -- after the table definition
 Alter level -- using alter command
While adding constraints you need not specify the name but the type only, oracle will internally name the
constraint. If you want to give a name to the constraint, you have to use the constraint clause.
NOT NULL
This is used to avoid null values. We can add this constraint in column level only.
SQL> create table student(no number(2) not null, name varchar(10), marks number(3));
CREATE TABLE A_CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT
NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2));

SQL> create table student (no number(2) constraint nn not null, name varchar(10), marks number(3));

CHECK
This is used to insert the values based on specified condition.
We can add this constraint in all three levels.
COLUMN LEVEL

SQL> create table student(no number(2) , name varchar(10), marks number(3) check (marks > 300));

SQL> create table student(no number(2) , name varchar(10), marks number(3) constraint ch check(marks > 300));
TABLE LEVEL

SQL> create table student(no number(2) , name varchar(10), marks number(3), check (marks > 300));

SQL> create table student(no number(2) , name varchar(10), marks number(3), constraint ch check(marks > 300));

ALTER LEVEL

SQL> alter table student add check (marks>300);

SQL> alter table student add constraint ch check (marks>300);

UNIQUE
This is used to avoid duplicates but it allow nulls.
We can add this constraint in all three levels.
COLUMN LEVEL

SQL> create table student (no number(2) unique, name varchar(10), marks number(3));

SQL> create table student (no number(2) constraint un unique, name varchar(10), marks number(3));
TABLE LEVEL

SQL> create table student (no number (2) , name varchar(10), marks number(3), unique(no));
SQL> create table student (no number(2) , name varchar(10), marks number(3), constraint un unique(no));
ALTER LEVEL

SQL> alter table student add unique(no);

SQL> alter table student add constraint un unique(no);

PRIMARY KEY
 This is used to avoid duplicates and nulls. This will work as combination of unique and not null.
 Primary key always attached to the parent table.
 We can add this constraint in all three levels.
COLUMN LEVEL
SQL> create table student(no number(2) primary key, name varchar(10), marks number(3));

SQL> create table student(no number(2) constraint pk primary key, name varchar(10), marks number(3));
TABLE LEVEL

SQL> create table student(no number(2) , name varchar(10), marks number(3), primary key(no));

SQL> create table student(no number(2) , name varchar(10), marks number(3), constraint pk primary key(no));

ALTER LEVEL

SQL> alter table student add primary key(no);

SQL> alter table student add constraint pk primary key(no);

FOREIGN KEY
 This is used to reference the parent table primary key column which allows duplicates.
 Foreign key always attached to the child table.
 We can add this constraint in table and alter levels only.
TABLE LEVEL

SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), primary key(empno), foreign

key(deptno) references dept(deptno));


SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), constraint pk primary

key(empno), constraint fk foreign key(deptno) references dept(deptno));


ALTER LEVEL

SQL> alter table emp add foreign key(deptno) references dept(deptno);

SQL> alter table emp add constraint fk foreign key(deptno) references dept(deptno);

Once the primary key and foreign key relationship has been created then you cannot remove any parent record if
the dependent Childs exist.

USING ON DELTE CASCADE


By using this clause you can remove the parent record even it Childs exists. Because whenever you remove parent
record oracle automatically removes all its dependent records from child table, if this clause is present while
creating foreign key constraint.
TABLE LEVEL

SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), primary key(empno), foreign

key(deptno) references dept(deptno) on delete cascade);


SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), constraint pk primary

key(empno), constraint fk foreign key(deptno) references dept(deptno) on delete cascade);


ALTER LEVEL

SQL> alter table emp add foreign key(deptno) references dept(deptno) on delete cascade;

SQL> alter table emp add constraint fk foreign key(deptno) references dept(deptno) on delete cascade;

COMPOSITE KEYS
A composite key can be defined on a combination of columns. We can define composite keys on entity integrity
and referential integrity constraints. Composite key can be defined in table and alter levels only.
UNIQUE (TABLE LEVEL)

SQL> create table student (no number(2) , name varchar(10), marks number(3), unique(no,name));

SQL> create table student (no number(2) , name varchar(10), marks number(3), constraint un unique(no,name));

UNIQUE (ALTER LEVEL)

SQL> alter table student add unique(no,name);

SQL> alter table student add constraint un unique(no,name);

PRIMARY KEY (TABLE LEVEL)

SQL> create table student(no number(2) , name varchar(10), marks number(3), primary key(no,name));

SQL> create table student(no number(2) , name varchar(10), marks number(3), constraint pk primary

key(no,name));
PRIMARY KEY (ALTER LEVEL)

SQL> alter table student add primary key(no,anme);

SQL> alter table student add constraint pk primary key(no,name);

FOREIGN KEY (TABLE LEVEL)

SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), dname varchar(10), primary
key(empno), foreign key(deptno,dname) references dept(deptno,dname));
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), dname varchar(10),
constraint pk primary key(empno), constraint fk foreign key(deptno,dname) references dept(deptno,dname));
FOREIGN KEY (ALTER LEVEL)

SQL> alter table emp add foreign key(deptno,dname) references dept(deptno,dname);

SQL> alter table emp add constraint fk foreign key(deptno,dname) references dept(deptno,dname);

DEFERRABLE CONSTRAINTS
Each constraint has two additional attributes to support deferred checking of constraints.
 Deferred initially immediate
 Deferred initially deferred
Deferred initially immediate checks for constraint violation at the time of insert.
Deferred initially deferred checks for constraint violation at the time of commit.
SQL> create table student(no number(2), name varchar(10), marks number(3), constraint un unique(no) deferred
initially immediate);
SQL> create table student(no number(2), name varchar(10), marks number(3), constraint un unique(no) deferred

initially deferred);
SQL> alter table student add constraint un unique(no) deferrable initially deferred;

SQL> set constraints all immediate;


This will enable all the constraints violations at the time of inserting.
SQL> set constraints all deferred;

This will enable all the constraints violations at the time of commit.
OPERATIONS WITH CONSTRAINTS

Possible operations with constraints as follows.


 Enable
 Disable
 Enforce
 Drop
ENABLE

This will enable the constraint. Before enable, the constraint will check the existing data.
SQL> alter table student enable constraint un;

DISABLE

This will disable the constraint.


SQL> alter table student enable constraint un;

ENFORCE

This will enforce the constraint rather than enable for future inserts or updates.
This will not check for existing data while enforcing data.
SQL> alter table student enforce constraint un;

DROP

This will remove the constraint.


SQL> alter table student drop constraint un;

Once the table is dropped, constraints automatically will drop.

You might also like