Você está na página 1de 12

Data types:

1. Int

2. Varchar(255)

3. Date :- YYYY-MM-DD

4. Time :- HH-MM-SS

5. Timestamp :- YYYY-MM-DD HH-MM-SS

Create table:
CREATE TABLE Table_name(column1 data type,column2 data type.....);

Insert Data Into Table:


INSERT INTO Table_name(column1,column2.....) values (a,b.....);

Insert Multiple Data using one command:


INSERT INTO Table_name With names as(

select 01,'a','b' from dual union all

select 02,'c','d' from dual

select * from names;

To have a data only once:


select distinct * from Table_name;

Create new table using existing table:


CREATE TABLE new_tablename as select column1,column2... from existing_tablename;

To Add Column to table:


Alter table table_name add Column_name datatype;

To Drop Column from table:


Alter table Table_name drop column column_name;

To Rename Column:
Alter table Table_name Rename column exiting_name to new_name;

To Modify Column:
Alter table Table_name modify column_name datatype;

To view data in table:


select column1,column2... table_name; or

select from * table_name;

Selected columns and rows:


Select<columnname1>,<columnname2>,from<tablename>;

example: Select Name,Roll_no from Student;

Eliminating distinct rows while using select statement:


select distinct <columnname1>,<columnname2>from<tablename>;
Sorting data in a table:
select * from <tablename> order by <columnname1>,<columnname2><[sort order];

Delete Operations:
delete from tablename;

Delete record as well as memory occupied:


truncate table table_name;

Deleted data recovery:


rollback;

Delete specific row:


delete form employer where eid='101';

Update Record:
Update Employer set depy='CSE' where Eid='101';

Select command with arithmetic operators:


1. +operation: salary will be incremented by n

syntax: select ename,salary+n from emp;

2. -operation: salary will be decremented by n

syntax: select ename,salary-n from emp;


3. *operation: salary will be multiplied by n

syntax: select ename,salary*n from emp;

4. /operation: salary will be divided by n

syntax: select ename,salary/n from emp;

Alias name(Nick name for column):


Name is not updated

select col1 alias_name1. col2 alias_name2 from tablename;

Two values combined together without space:


select col1 || col2 from tablename;

Two values combined together with have salary in between the two columns:
select col1 || 'has salary' || col2 from tablename;

Aggregate Operators:
1. Min :- select min(column_name) from table_name;

2. Max:- select max(column_name) from table_name;

3. Avg:- select avg(column_name) from table_name;

4. Sum:- select sum(column_name) from table_name;

5. Count:- calculates no of rows in table

:- select count(*) from table_name : counts all including nos., null values

:- select count(column_name) from table_name : counts only non-null values of that particular
column
Comparison Operators:
1. = :- select * from emp where column_name=xyz;

2. > :- select * from emp where column_name>xyz;

3. >=

4. <

5. <=

6. <> :-Not equal to

7. between :- used to specify range

:- select * from emp where column_name xyz and abc;

8. Like :-

like 'R%' :- displays names starting with R

like '%R' :- displays names ending with R

like '_%' :- displays all records

like '_ _ _R%' :- displays 4 letter records ending with R

syntax :- select * from table_name where column_name like 'R%'

9. In :- set of values if some record matches then it will display

syntax :- select * from table_name column_name in(value1,value2,value3);

syntax for values except the values in brackets :- select * from table_name column_name not
in(value1,value2,value3);

Logical Operators :
1. Not

2. And :- Both conditions should be fulfilled

syntax :- select * from table_name where column_name=xyz and column_name2 like 'E%';

3. Null :- gives all records whose deptId is null

syntax :- select * from Table_name where deptId is null;


Precedence of Operators:-
1.Arithmetic

2.Concatenation

3.Comparision

4.IS,NULL,like,IN

5.between

6.and

7.or

8.not

Constraints:-
1.NOT NULL:- restrict user to enter null values.

2.Unique:- when we want all values to be different. eg. registration no.

3.Primary key:- combination of not null and unique. THERE IS ONLY ONE .

4.check:-to check whether a column satisfies a particular condition or not.

5.Foriegn key:-

#Multiple columns
create table person{

ID int not null,

Ln varchar(10),

age int,

constraint idLastName unique(id,number)


}

ER(Entity Relationship) Model came into existence in 1976 by PP Chen

ER model is used to design our database

Entity:- principle data object about which information is to be collected.

Attributes: Properties of the entities.

Relationship:-

Terminologies

1)Relation: Tables

2) Attributes: Columns

3) Tuple: Rows

4) Degree of relation : No of columns

5) Cardinality of relation

6) Data Value

7) Domain

Keys

1) Candidate key
2) Primary Key
3) Alternate key
4) Composite key

Integrity Rules

1)Entity IR :Should satisfies not null property

2) Referential IR
Normalization

In order to reduce redundancy(Duplication of data).

Case Conversion

1.Lower

Select LOWER(name) from tablename where name=xyz;

2.Upper

Select upper(name) from tablename where name=xyz;

Character Manipulation

1.Concat

2.Substr

3.length

4.instr- the postion of the letter

5.Lpad

6. Rpad

7.replace

8.Trim- Remove the letter from string

Character Manipulation functions in Oracle SQL


Function 1: UPPER

Purpose : Returns the string in uppercase

Syntax : UPPER(str)

Example : SELECT UPPER(karuvachi) from Dual;

Output:KARUVACHI

-
Function 2: lower

Purpose : Returns the string in lowercase

Syntax : lower(str)

Example : SELECT LOWER(KaRuVaChi) FROM DUAL;

Output:karuvachi

Function 3: Initcap

Purpose : Returns the string with first letter in uppercase and rest of the letters in lowercase

Syntax : Initcap(str)

Example : SELECT Initcap(KaRuVaChi) FROM DUAL;

Output:Karuvachi

Function 4: Concat

Purpose : Concatenate two strings

Syntax : concat(str1,str2)

Example : SELECT CONCAT(Karu,Nand) FROM DUAL;

Output:KaruNand

Function 5: Lpad

Purpose : Pad in the left side of the string for given times length of the string

Syntax : Lpad(str1,n,str2)

Example : SELECT Lpad(Karu,6,?) FROM DUAL;

Output:??Karu
-

Function 6: Rpad

Purpose : Pad in the right side of the string for given times length of the string

Syntax : Rpad(str1,n,str2)

Example : SELECT Rpad(Karu,6,?) FROM DUAL;

Output:Karu??

Function 7: trim

Purpose : Trim the whitespaces in both the sides of the string

Syntax : trim(str)

Example : SELECT TRIM( karu ) FROM DUAL;

Output:karu

Function 8: Ltrim

Purpose : Trim the whitespaces in left the side of the string

Syntax : Ltrim(str)

Example : SELECT LTRIM( karu ) FROM DUAL;

Output:karu.(. dot are spaces)

Function 9: Rtrim

Purpose : Trim the whitespaces in right the side of the string

Syntax : Rtrim(str)

Example : SELECT RTRIM( karu ) FROM DUAL;


Output:.karu(. dot are spaces)

Function 10: Length

Purpose : length of the string

Syntax : length(str)

Example : SELECT LENGTH(karuvachi) FROM DUAL;

Output:9

Function 11: Instr

Purpose : Find the position of the string in another string

Syntax : Instr(str1,str2)

Example : SELECT INSTR(karuvachi,ka) FROM DUAL;

Output:1

Function 12: substr

Purpose : get a sub string from string

Syntax : substr(str,start_pos,number_of_chars)

Example : SELECT substr(karuvachi,2,4) FROM DUAL;

Output:aruv

Function 13: SOUNDEX

Purpose : translates a source string into its soundex code.

Syntax : SOUNDEX(str)
Example : SELECT SOUNDEX(karu) FROM DUAL;

Output:K600

To retrieve system date from system

Select sysdate from dual;

Você também pode gostar