Вы находитесь на странице: 1из 4

select * from staff;

ed // open the buffer then can edit


// without semicolon in the buffer
// after exit
r / run / /
// without / in the buffer will not let you to enter sql command after exit , you
must then press enter or ;
// range condition:
between: select sno, bno, fname, salary from staff where salary between 5000 and
14000; //
select sno, bno, fname, salary from staff where salary not between 5000 and 14000
//membership condition(in clouse):
select sno, bno, fname, salary from staff where salary in(3000,14000, 30000) // =
or
select sno, bno, fname, salary, position from staff where position
in('Assistant','Manager') // not in is possible
// pattern condition(like clause)
select sno, fname, position from staff where fname like 'J%'
select sno, fname, position from staff where fname like 'J_hn' // not like is
possible
// defined variable
select fname, salary from staff where salary>&salary // when run, it will let you
enter valur for the variables // any where you can use variables
// when press run again , will also let you enter the value of the variables
// when we must to fix the varible so enter the value once -> &&variable -> the
first time for running willl enter the value
then for the next runs will take the first value for &&variable
//Calculations ...
select fname, sno, pno, salary*12 from staff // the last col will be labled as
salary*12 so we can change the col name.ex. as annual
//date
select sysdate from staff
// 6 rows selectd , because select query implemnted on all records
select sysadate-dob from staff; // staff ages (in days)
select (sysadate-dob)/364 from staff; // staff ages (in years)
// functions
single rows
ex:round

groub function(multiple row selection)


ex.select max salary from staff
ex.select count(salary) from staff
ex.select avg(salary), bno form staff; // error-> bno returns multi values and
avg() return one value
select avg(salary) from staff groub by bno
ex. select sno, salary from staff where salary>10000 order by salary // default=
ascending
select sno, salary from staff where salary>10000 order by salary desc
select sno, salary from staff where salary>10000 order by salary, bno desc; ->
b12 7000
b15 9000 =>
b70 5000
b12 5000

b12 7000
b15 9000
b12 5000
b70 5000
select salary*12 as anual from staff where anual > 10000 order by annual // invalif
identifier
compiler when execute
from -> where -> colname -> order by .. so edit the last statment:
select salary*12 as annual from staff where salary*12 > 10000 order by annual //
true
//exception
select max(salary), bno from staff groub by max(salary)>=12000 groub by bno;
//error because groub by visit tables by rows
select bno, max(Salary) from staff where Bno not in ('B3') having max(salary)>9000
group by bno // true

---Lecture---

initcap

number->char->date
number<-char<-date
number->date //false
date->number //false

//ddl
-creat
we must know master table and child table
here parent table is branch/comopney and the child is employee/staff -> because
the one ot many relation
company(id:number,name:varchar2(20),address:varchar2(40),tel:varchar2(7))
create table company(id number(5) primary key,
2 name varchar2(20),
3 address varchar2(40),
4 tel varchar2(7), constraint comp_tel_uq unique(tel),
5 constraint com_add_name_uq unique(name,address));

create table employee(Eid number(5) primary key,


2 fname varchar2(20),
3 lname varchar2(20),
4 salary number(7,2),
5 gender char(1),
6 dob date,
7 Cid number(5),
8 constraint emp_cid_fk references company[(id)](Cid));
create employee(Eid number(5) primary key,
*
ERROR at line 1:
ORA-00901: invalid CREATE command

SQL> constraint emp_cid_fk references company[(id)](Cid));


**********

create table employee(


eid number constraint emp_id_pk primarykey(eid),
name varchar2(10), salary number(7,2) constraint emp_sal_ch
check(salary)>5000,
address varchar2(20),
dob date default sysdate,
position varchar2(20) default 'Manager',
cid number references company,
constraint emp_pos_add_uq unique(position,address)
);
***altr tables ***
rename oldname to newname
rename staff to st;

DROP table branch


error => drop table branch cascade constraints; // because of the forign key
// we wanna return all tables that we droped
@d:\dream
<alter table>
drop/add/modify/disable/enable
alter table tablename add(-,-,-)
alter table branch drop (tel) //the column that we deleted it will never back
alter table branch drop (bno) // can not drop parentkey
alter table tablename modify(salary number(5,2)) // error - the col must be empty
alter table branch diable primary key // error
=> alter table branch diable primary key cascade;
alter table branch enable primary key;
alter table tablename disable constraint_name;
//so we must know the constraint_name
=> select * from user_constraints; // it will return about all tables
=> select * from user_constraints where table_name = 'staff'; // no row selected =>
STAFF
// we don't need everythind=> select constraint_name from user_constraints where
table_name = 'STAFF'
inserting=>
insert into employee values(..,..,..,..); // ordered cols
insert into employee values(15,'sami','Assistant',5000);
insert into e(eid,name) values (2,'ali');

*****************************

update tablename set col1 = val1, col2 = val2.. where condition;

update staff set salary = null where sno = '..';


//i wanna to back -> set salary=5000 where salary is null;

delete tablename; or delete from tableName; // delete data from table // i can add
where
delete branch // can't because the is refrence refrences on branch
when we create the child and when we write the refrence , we can write refrences
branch on delete cascade // or on delete set null

<advanced query>

select staff.sno,staff.fname,branch.bno,branch.telno .. from staf,branch where


staff.bno = branch.bno
select s.sno,s.fname,b.bno,b.telno from staff s,branch b;

***************************
subquery => select fname,salary from staff where salary = (select max(salary) from
staff);
select fname,salary from staff where salary in (select max(salary) from staff group
by bno);
select fname,salary from staff where salary in (select max(salary) from staff group
by bno);
**********************
<sequence>
create sequence seq_name start with value increment by value maxvalue value min
[cycle]/[nocycle] //default no cycle
create sequence seq_name

seq_name.currval
seq_name.nextval
// to can use currval we must do nextval befor currval;
insert into emp values(seq.nextval,'Ibrahem');
// there all in table which name is user_sequences
select sequence_name from user_sequences;
drop sequence seq;
************************
<views>

Вам также может понравиться