Professional Documents
Culture Documents
Tuple (Row)
Attribute (Column)
Relation (Table)
Integrity Constraints
Primary Key
Alternate Key
Foreign Key
Introduction to Databases.
.
Introduction to RDBMS
.
CODDS RULES
Systematic Treatment of Nulls Rule: The RDBMS handles records that have
unknown or inapplicable values in a pre-defined fashion
View Updating Rule: Any view that is theoretically updateable can be updated using
the RDBMS. Data consistency is ensured since the changes made in the view are
transmitted to the base table and vice-versa
High-Level Insert, Update and Delete: The RDBMS supports insertions, updation
and deletion at a table level. .
Physical Data Independence: The execution of adhoc requests and application programs is
not affected by changes in the physical data access and storage methods
Logical Data Independence: Logical changes in tables and views such adding/deleting
columns or changing fields lengths need not necessitate modifications in the programs or in
the format of adhoc requests.
Integrity Independence: Like table/view definition, integrity constraints are stored in the
on-line catalog and can therefore be changed without necessitating changes in the application
programs
Distribution Independence: Application programs and adhoc requests are not affected by
change in the distribution of physical data
No subversion Rule: If the RDBMS has a language that accesses the information of a record
at a time, this language should not be used to bypass the integrity constraints. This is
necessary for data integrity
.
Oracle Datatypes
Fixed-length character data of length size bytes or Fixed for every row in the table (with trailing
Char characters blanks); maximum size is 2000 bytes per row
VARCHAR2 Variable-length character data Variable for each row, up to 4000 bytes per row
NVARCHAR2 Variable-length Unicode character data The upper limit is 4000 bytes per row
NCLOB Unicode national character set (NCHAR) data. Up to 232 - 1 bytes, or 4 gigabytes.
.
NUMBER (p, s) Variable-length numeric dataVariable-length The maximum space required for
numeric data. Maximum precision p and/or scale a given column is 21 bytes per
s is 38. row
DATE Fixed-length date and time data Fixed at 7 bytes for each row
.
Database Objects
CREATE statements
A CREATE statement in SQL creates an object inside of a relational database management
system (RDBMS). Like table, index, or stored query
Column definition: [column name] [data type] {NULL | NOT NULL} {column options}
Primary key definition: PRIMARY KEY ( [comma separated column list] )
CONSTRAINTS: {CONSTRAINT} [constraint definition]
The default data type must match the column data type.
Drop a column
Rename a column
ALTER statements
Alter - To modify an existing database object
.
Renaming a table
Data integrity allows to define certain data quality requirements that the data
in the database needs to meet
Not Null
A column in a table can be specified not null. It's not possible to insert a null
in such a column.
.
Unique Key
.
Composite Unique Key
.
Foreign key with more than one field
.
Disabling Constraints
create table foo (bar number, baz number, unique (bar, baz));
.
TRUNCATE - remove all records from a table,
including all spaces allocated for the records are
removed
.
Data Manipulation language (DML)
DELETE - deletes all records from a table, the space for the records remain
.
Examples tables for DML
.
Insert statement
insert into emp values( 7369, 'SMITH', 'CLERK', 7902 ,'17-DEC-80' ,800, 20);
insert into emp values(7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-81', 1600, 30);
insert into emp values(7521, 'WARD', 'SALESMAN', 7698, '22-FEB-81', 1250, 30);
insert into emp values(7902, 'FORD', 'ANALYST', 7566, '03-DEC-81', 3000, 10);
.
dept
.
SALGRADE
.
Select Statement
.
Selecting Columns
Arithmetic operators
.
Sorting
.
Filtering the Data
Operators
=, != or <>,<, >,<=, =>
.
Set Conditions:
select EMPNO, ENAME, SAL from EMP where SAL between 1500
and 2500; .
select JOB, SAL from EMP
where (MGR = 7698 or MGR = 7566) and SAL > 1500;
.
String functions
.
Conversion functions
.
Group Functions
SUM(expression) Adds the value for all rows in the query or for all rows
with the same values for columns listed in the GROUP BY
clause
.
Numeric functions
.
update EMP set SAL = SAL 1.15 where DEPTNO in (10,30);
.
Deletions
.
TCL command?
Commit
Rollback
Savepoint
.
Joining Relations
.
Self Join
.
Sub Query
.
List all employees who are working in a department located
in BOSTON:
select from EMP where SAL >= any(select SAL from EMP
where DEPTNO = 30) and DEPTNO = 10;
.
List all departments that have no employees:
.
Set operators
.
Views
The following view contains the name, job title and the
annual salary of employeesworking in the department 20:
create view DEPT20 as
.
DECODE
.
Nvl function
.
CASE
CASE
WHEN C1 THEN R1
WHEN C2 THEN R2
...
WHEN CN THEN RN
ELSE RD
END
.
SELECT TO_CHAR(order_dt, 'Q') sales_quarter,
SUM(sale_price) tot_sales FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-
YYYY)AND order_dt < TO_DATE('01-JAN-2002','DD-MON-
YYYY') GROUP BY TO_CHAR(order_dt, 'Q)ORDER BY 1;
S TOT_SALES
- ----------
1 9739328
2 10379833
3 9703114
4 9772633
.
SELECT
SUM(DECODE(TO_CHAR(order_dt, 'Q'), '1', sale_price, 0)) Q_1,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '2', sale_price, 0)) Q_2,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '3', sale_price, 0)) Q_3,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '4', sale_price, 0)) Q_4
FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-YYYY')
AND order_dt < TO_DATE('01-JAN-2002','DD-MON-YYYY');
.
SELECT LNAME, EMP_ID, MANAGER_EMP_ID FROM EMPLOYEE
START WITH MANAGER_EMP_ID IS NULL CONNECT BY PRIOR
EMP_ID = MANAGER_EMP_ID;