DBMS SQL COMMANDS

Lec - 4

create table

content_copy
create table table_name(attr_1 data_type,
                        attr_2 data_type
                        ...
                        ...
                        );

Constraints (Field level and Table level)

content_copy
create table table_name(attr_1 data_type primary key,
                        attr_2 data_type not null,
                        attr_3 datay_type default 'default_value',
                        attr_4 data_type check(attr_4 < 100)
                        );

Some important tables

content_copy
select constraint_name, constraint_type from user_constraints where table_name="OUR_TABLE";

creating a table with foreign key and creating a composite key

content_copy
create table doctor(docid int primary key,
                    docname char(40) not null
                    );
content_copy
create table appt(apptdate timestamp,
                  doctorid int refereces doctor(docid),
                  patid int references patient(patid),
                  primary key(appdate, doctorid, patid)  
                  );
content_copy
create table visit(visitid int,
                   vdt timestamp,
                   docid int,
                   patid int,
                   foreign key(vdt, docid, patid) refereces appt(appdate, doctorid, patid));

desc table

content_copy
desc table_name;

drop table

content_copy
drop table table_name;

insert

content_copy
insert into table_name values(null, default, ...); 

select

content_copy
select * from table_name;
content_copy
select attr_1||attr_2||attr_3.. from table_name; 
content_copy
select trim(attr_1) from table_name;

delete

content_copy
delete from table_name
content_copy
delete from table_name where conditions;

update

content_copy
update table_name set attr=new_value, attr_2=new_value where attr=old_value

Lec-5 and Lec-6

Generalised select statement

content_copy
select distinct field/*/function/expression
from table_name/subquery
where condition_on_record_level
group by fieldnames/expression/function
having condition_on_grp/condition_on_aggregate_function
order by fieldname/expression/function

String functions(Record based)

content_copy
select  upper(patname),
	      lower(patname),
	      trim(patname),
	      rtrim(patname),           //trim empty space from right
	      ltrim(patname),
	      initcap(patname),         // First letter capital
	      substr(patname,2,2)       // From 2nd pos display 2 char 
        from patient where patid = 3;

Some conversions functions:

Date formats:

content_copy
select stdno, to_char(birtdate, 'dd') from students2021;
content_copy
select stdno, to_char(birtdate, 'mm') from students2021;
content_copy
select stdno, to_char(birtdate, 'yy') from students2021;
content_copy
select stdno, to_char(birtdate, 'yy-mm-dd') from students2021;
content_copy
select stdno, to_char(birtdate, 'month') from students2021;
content_copy
select stdno, to_char(birtdate, 'dd month yyyy') from students2021;
content_copy
select stdno, to_char(birtdate, 'ddth month yyyy') from students2021;
content_copy
select stdno, to_char(birtdate, 'ddsp') from students2021; // spelling(sp)

Special operators(after where clause):

in

content_copy
select * from patients where patid in (list...);
content_copy
select * from patients where patid in (select patid from appt);

like

between

content_copy
select * from patient where patid between 1 and 10; // including 1 and 10

null / not null

exists / not exists

content_copy
select * from patient where exists(select * from appnt);

Aggregate functions:

content_copy
select avg(field/*), min(field/*), ... ; 

Joins

cross join

content_copy
select * from patient, appnt; //(patient * appnt)

simple / equi / natural join

content_copy
select * from patient, appnt where patient.patid=appt.patid;

Multiple join

content_copy
select patname, docname from patient, doctor where patient.patid = appt.patid and appt.patid = doc.patid;

Inner join

content_copy
select (list of fields, ... ) form table_1 inner join table_2 on table_1.common_field=table_2.common_field

Outer join

Left Outer Join
content_copy
select * table_1 left join table_2 on common_field_condition
Right Outer Join
content_copy
select * table_1 right join table_2 on common_field_condition

left join of table1 and table2 = right join of table2 and table1 \boxed{\text{left join of table1 and table2 = right join of table2 and table1}}

Full Outer Join
content_copy
select * table_1 right join table_2 on common_field_condition
Alias (To avoid using long table names)
content_copy
select a.field_1, b.field_2 from table_1 a, table_2 b
Self Join
content_copy
select distinct d1.docname, d2.docid form doctor d1, doctor d2 where
(d1.docname=d2.docname) and (d1.gender <> d2.gender)

Intersection, Union, Minus

content_copy
select (union_compatible fields, ... ) from table_1 
interset 
select (union_compatible fields, ...)  from table_2
content_copy
select (union_compatible fields, ... ) from table_1 
union 
select (union_compatible fields, ...)  from table_2
content_copy
select (union_compatible fields, ... ) from table_1 
minus 
select (union_compatible fields, ...)  from table_2

Group-By

content_copy
select group_field, count(*) from table group by group_field

group by ... having

content_copy
select group_field, count(*) from table
group by group_field
having our_condition