-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_Commands.txt
161 lines (111 loc) · 5.01 KB
/
SQL_Commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
create table employee2
(
empno number(6) constraint pk_eid0 primary key,
ename varchar2(30) not null,
ejob varchar2(10),
grade char(3) default 'a',
dob date check(dob>'01-Jan-2003'),
esal number(7),
deptno number(2)
);
insert into employee2 values(11,'Sunil','Clerk','b','1-Jan-2008',10000,11);
insert into employee2 values(102,'Rajesh','Salesman',null,null,null,10);
//to add column:
alter table employee2 add(email varchar(20));
//to add data in column:
update employee2 set email='[email protected]' where empno=101;
//to add constraint in data in column:
alter table employee2 add constraint emp2_email_uk unique(email);
//to add column:
alter table employee2 add (salary number(10));
//to add column:
alter table employee2 add (salary2 number(10));
//to drop column:
alter table employee2 drop column salary2;
//to modify size of datatype:
alter table employee2 modify(grade char(6));
//to rename column:
alter table employee2 rename column ename to First_Name;
//to add constraint
alter table employee2 add constraint emp2_email_pk primary key(email);
//to delete constraint
alter table employee2 drop constraint pk_eid2;
//to disbale constraint
alter table employee2 disable constraint pk_eid2;
//to enable constraint
alter table employee2 enable constraint pk_eid2;
=============================================================================================================
DML COMMAMDS:
---------------------------------------------------------------------------------------------------------------
INSERT COMMANDS:
//insert simple data:
insert into employee2 values(101,'Sunil','Clerk','b','1-Jan-2008',10000,10);
//insert less values than available columns:
insert into employee2 (empno,ename,ejob,deptno)values(104,'Farkhanda','CEO',10);
//insert values from user:
insert into employee2 values(&empno,&ename,&ejob,&grade,&dob,&esal,&depno);
//insert less values than columns and take from user:
insert into employee2 (empno,ename,esal) values(&empno,&ename,&esal) ;
//insert data from 1 table to another:
insert into emp copy(select * from employee2);
----------------------------------------------------------------------------------------------------------------
UPDATE COMMANDS:
//update 1 Value
update employee2 set ename='Kumar' where empno=101;
//update 2 values ar one time
update employee2 set ejob='President',ename='Sunil' where empno=102;
//set data from 1 row to other
update employee set esal=(select esal from employee2 where empno=200) where empno=101;
------------------------------------------------------------------------------------------------------------------
DELETE COMMANDS:
//delete using 1 reference:
delete from employee2 where deptno=10;
//delete row:
delete from employee2 where empno=101;
//delete all data from table:
delete from employee2;
-------------------------------------------------------------------------------------------------------------------
commit;
savepoint;
rollback;
create sequence demo_seq
start with 10
increment by 1
minvalue 5
maxvalue 15
nocache
nocycle
--------------------------------------------------------------------------------------------------------------------
====================================================================================================================
DQL COMMANDS:
//to view only desired colums from table:
select employee_id,salary from employees;
//to view only desired columns from table with allies(ie.shortforms we created for column names):
select employee_id"emp_id",salary"sal" from employees;
//to view data in desired columns within a desired range:
select employee_id,first_name,salary from employees where salary>5000 and salary<15000;
OR
select employee_id,first_name,salary from employees where salary between 5000 and 15000;
//to view desired columns where condition=given value:
select employee_id,first_name,salary from employees where salary in(10000,12000,21000);
OR
select employee_id,first_name,salary from employees where salary=10000 or salary=12000 or salary=21000;
//to view desired columns outside given range:
select employee_id,first_name,salary from employees where salary not between 5000 and 15000;
OR
select employee_id,first_name,salary from employees where salary not in(10000,12000,21000);
//to view data in desired columns by asc order:
select employee_id,first_name,salary from employees order by salary;
//to view data in desired columns by descending order:
select employee_id,first_name,salary from employees order by salary desc;
//salary will print in ascending order and employee_id in desc order:
select employee_id,first_name,salary from employees order by salary,employee_id desc;
select employee_id,first_name from employees
//to view data which is null:
select * from employees where COMMISSION_PCT is null;
//to view data which is not repeated:
select distinct manager_id from employees;
//to view max value:
select max(employee_id) from employees;
//to view min values:
select min(employee_id) from employees;