Beruflich Dokumente
Kultur Dokumente
Dynamic insert:
Data retrieving:
2. Specific record:
8. create table emp4 as select * from emp2 where name='hello'; \\we need copy structure then
give any false coloumn data
Data modification:
Alter
1.command is used to add new coloumn for existing table.
DROP:
TRUNCATE:
It is used to delete all rows from table,much faster than delete,release storage,can't remove
specific rows without changing the structure of the table
RENAME:
It is used to renaming the table
script:
save droptable.sql;
@droptable;
host vi droptable.sql;
Date: 10-08-2016
Today
While entering the data type for anything u doesn’t mention the range then it will take range 38
as default.
DATA TYPES:
Number:
Ex: number(10),varchar(25)
Number(5,2) ----- in this 5 means precession 2 means scope length after decimal
If number(2,3) --- means .00999 first 2 zeros and then values 3
CHAR:-
We always use varchar when length of the column value is not fixed.
1. CHAR allocates memory static memory, and used for fixed length
2. VARCHAR allocates memory dynamically , used for dynamic length max size 2000;
3. VARCHAR2 allocates memory dynamically, used for dynamic length max size 4000;
4. Char,varchar,varchar2 have different max size earlier means before 10g
5. But now char max limit 2000 and varchar,varchar2 max limit 4000;
DATE:
TIMESTAMP:
OPERATORS:
ARITHMATIC OPERATORS:
+,-,/,*
Ex: select hello hcl from emp; --- it will show an error called invalid identifier.
RELATIONAL OPERATOR:
= , > , < , <= , >= ,!= .
Q:Retrieve information of all employees whose salary is >10000 but <20000 and
he is not living in delhi.
11-08-2016
SQL CLAUSES/KEYWORDS :
1.Between:
Ex: select * from emp where salary between 10000 and 20000;
(or)
2. IN :
It is used to search the data for multiple conditions on single or multiple column.
(or)
Ex: select * from emp3 where id=100 or id=110 or id=120 or id=130 or id=140;
3.ORDER BY:
It is used for sorting of data in ascending or descending order, default order is ascending.
Q: sort the data in ascending order for 1st column and then subgroup(column2) is sorted in descending
order?
NOTE : If there are 3 columns in a table . we sorted 2 columns then 3rd column will automatically sorted
4.DISTINCT/UNIQUE:
It is used to set user defined default value to a column if value is not available for that column.
We can use default keyword at table creation time;
Ex: 1.create table nid (id number default 0,name varchar(20),pan varchar(10) default 'N/A',passport
varchar(20) default 'Not Applicable',email varchar(30) default 'Not there');
Output:
Table created.
Output:
SQL>insert all
TCL:
1.COMMIT:
Rollback:
It is used to undo last transaction until last commit or rollback, database connection is performed. and
ROLLBACK, CAN’T work simultaneously;
Ex: rollback;
Savepoint:
When a save point is used with rollback we a part of transaction wil be rollback. Because stand alone
rollback and do the entire transcation.
Types of privileges:
Q: How to give data retrieving permission to HR user on emp1 table where table is owned by scott
user?
Q: Give all data modification permissions to hr user on emp1 table where table owned by scott user:
(give permission through DBA) ?
SQL> Grant all on scott.emp1 to hr; \\ all means any operation can be perform
REVOKE:
Ex: Block the data removing permission from hr user from emp1 table, Table is owned by scott user.
A. show user;
SQL> revoke delete on scott.emp1 from hr cascade; \\ all means any operation can be perform
Rowid :
A. delete from nid where rowid not in(select min(rowid) from nid group by id,name,pan,passport,email);
Q: How to give permission to another user and that user also can give permission to anyother user?
Constraints:
Integrity constraints:
Integrity constraints are the rules and regulations or restrictions applied on tables column
Types:
1. Entity constraints:
Primary key:
1. It is used to uniquely identify a row or record.
2. It does not allow duplicate values.
3. It will not allow null values.
4. Only one primary key allow on a table.
5. We can create single primary key on single column or combination of multiple columns
(Known as composite primary key).
Unique key:
1. It is used to uniquely identified a row same as primary key.
2. Unique column will not allow duplicate values.
3. Unique columns will allow null values.
4. We can have more than one unique column in a table.
5. We can create composite unit constraint up to 38 columns
Ex: create table tb2(acno number primary key,name varchar(25),atmno number unique, balance
number);
Ex: create table tb2(acno number primary key,name varchar(25),atmno number unique,id
number unique ,balance number);
Foreign key
1. It is a column whose values are derived from primary key or unique column of some
other table.
2. Note : In foreign key column we can insert data that is available in referenced primary
key or unique column.
3. It will allow duplicate values.
4. Foreign key column value allow null values.
5. We can create composite foreign key up to 38 columns.
Ex: create table dept (dept_id number,dept_name varchar(50), location varchar(20) primary
key(dept_id));
Check :
It’s a user defined constraint used to restrict data value of a column.
Ex: If we want mobile numbers must be in 10 digits , email must be in its standard format,
Employee_id must start with hcl etc.,
Not Null :
Is used to create mandatory columns.
Naming conventions :
Naming conventions used to provide user defined name for a constraint
NOTE: Directly we can’t remove foreign key with alter drop command, Only when we mention
the constraint then only it will be remove
Set Operators :
LIKE:
It is used for string matching In user defined pattern.
Output:
ID EMAIL
---------- ------------------------------
100 nagur.vali@hcl.com
110 kadiyam.m-scholar@hcl.com
SoundEX:
Union:
It is used to merge the data from multiple column from different tables in a single column.
Common values will be listed only ones.
Column name and data type must be same to perform the union.
Ex: select loc_name from loc1 union select loc_name from loc2 union select loc_name from
loc3;
Union all:
In this duplicate values will be listed out.
Ex: SELECT LOC_NAME FROM LOC1 UNION ALL SELECT LOC_NAME FROM LOC2;
Intersect:
It will return common values from the multiple columns of the different tables.
Ex: select loc_name from loc1 intersect select loc_name from loc2;
Minus :
It is used to return data values from column, those are not available in another column of
different table.
Ex: select loc_name from loc1 minus select loc_name from loc2;
Character Operators:
Concatenation:
It is used to combine multiple strings and generates single string.
With the help of || (vertical bars), we can perform string concatenation.
Ex: select ‘Hello’ || ‘ ‘ || ‘world’ from dual;
SQL FUNCTIONS:
Function is a group of 1 or more statements that takes input from user, process it and returns
output/results.
Types of functions:
Aggregate functions:
Also Known as Multi value function or multi row function.
1. SUM: we can add the value of entire column
2. Max : we can get max value of column
3. Min: we can get min value of column
4. AVG: we can get average value of column
5. COUNT: In this null values are not countable.
Options in Aggregate functions:
1. Group BY: It is used to grouping of data and apply aggregate functions on grouped data.
Ex: select dept,sum(salary) from emp1 group by dept;
Ex: select dept,count(*) from emp1 group by dept;
2. Roll up: It is used to generate subtotal and grand total of the grouped data.
Ex: select dept,sum(salary) from emp1 group by rollup(dept);
3. Having : It is used to filter grouped data as where clause is used to filter normal tables data.
Ex: Check maximum salary from each department, where no of employees in a department
must be greater than equal to 2.
A. select dept , max(salary) from emp1 group by dept having count(id) >1;
Dept Max(salary)
IT 45000
SALES 22000
Numeric functions:
Round:
SQL> select round(54.44,1) from dual;
SQL> 54.4
SQL> select round(54.45,1) from dual;
SQL>54.5
SQL>select round(27848,-3) from dual;
SQL>28000
SQL> select round(27448,-3) from dual;
SQL>27000
TRUNC:
MOD:
Exercise:
Character Functions:
SINGLE ROW FUNCTIONS:
SUBSTR:
SQL> hello
SQL> Select substr(‘helloworld’,-3,2) from dual;
SQL> rl
SQL> null
SQL> null
SQL> helloworld
LENGTH:
INSTR:
SQL>2
SQL> 9
SQL> 12
SQL> 3
LPAD:
SQL>#####50000
RPAD:
SQL>50000#####
REPLACE:
SQL> TSS
SQL> IS
SQL> TCS
SQL> IS
SQL> gllo
SQL> g llo
SQL>aello
SQL> abello
TRIM:
SQL> oca
SQL>oca
SQL> ocal
(OR )
SQL> select trim(leading ’l’ from ‘local’) from dual;
SQL> ocal
SQL> select rtrim(‘local’,’l’) from dual;
SQL>loca
(OR)
SQL>loca
Date Functions:
Months_Between:
Add_months:
Next_day:
Last_day:
SQL> select last_day (sysdate) from dual; \\ it will gives the months last date
Functions:
To_char():
SQL> select to_char(to_date(’25-JUL-16’),’Day ddth “of” month year’) date_info from dual;
SQL> select to_char(to_date('25-jul-16'),'day ddspth "of" month yyyy') date_a from dual;
SQL> select to_char(to_date('25-jul-16'),'day ddspth "of" month yyyy HH12:MI:SS ') date_a
from dual;
SQL> select to_char(to_date('25-jul-16'),'day ddspth "of" month yyyy HH12:MI:SS AM')
format from dual;
NVL:
NVL2:
SQL>
In place of where we can use Having when we are using GROUP FUNCTIONS:
Escape:
SQL> SELECT LAST_NAME FROM EMP WHERE LAST_NAME LIKE ‘%\_%’ ESCAPE ‘\’;
QUOTATION OPERATOR:
NATURAL JOINS:
When we want to join columns then datatype of column, position of the column, data also should match
value of the column, column name and everything should match in both tables.
If column name does not match with each other tables then it will do cross join(Cartesian product of the
two tables output—in this time natural join behaves like cross join);
Using clause:
Use the using clause to match only one column when more than one column matches
SQL> select l.city, d.department_name, from locations l join departments d using (location_id) where
location_id=1400;
Self join:
Non-Equi Join:
SQL>
OUTER JOIN:
Left outer join: it will give the data of the left side table columns(e) e.employee_id=d.employee_id.
Right outer join: it will give the data of the right side table columns(d) e.employee_id=d.employee_id.
Cross join :
SUB QUERIES:
Select e.last_name,e.salary from employees e where 1=(select count(distinct salary) from employees
m where e.salary<m.salary);
E M
4000 4000
6500 6500
5822 5822
9000 9000
select last_name,salary from employees where salary in ((select max(salary) from employees where
salary in(select distinct salary from employees where salary<(select salary from employees where
last_name='Abel'))), (select min(distinct salary) from employees where salary in(select distinct salary
from employees where salary>(select salary from employees where last_name='Abel'))),(select salary
from employees where last_name='Abel')) order by salary;
select last_name,salary from employees where salary in ((select max(salary) from employees where
salary in(select distinct salary from employees where salary<(select salary from employees where
last_name='Abel'))), (select min(distinct salary) from employees where salary in(select distinct salary
from employees where salary>(select salary from employees where last_name='Abel')))) or last_name
like ‘Abel’;
select last_name,salary from employees where salary = (select max(salary) from employees where
salary in(select distinct salary from employees where salary<(select salary from employees where
last_name='Abel')));
To Know the source code;
select text from user_source where name=’QUERY_EMPLOYEES’ order by line;