Documente Academic
Documente Profesional
Documente Cultură
Experiments
Introduction to DBMS. Introduction to ORACLE. To study different constraints. Explanation of DDL, DML and DCL.
Remark
2.
3.
4.
5.
6.
A DBMS (Database Management System) is a software program used to manage a database. These programs enable users to access and modify database A DBMS is a complex set of software programs that controls the organization, storage, management, and retrieval of data in a database. A DBMS includes four main components, which are: Modeling Language, Data Structures, DB Query Language and Report Writer, and Transaction Mechanism. Each of these components can be further broken down into smaller and more specific pieces, but it is the sum of these parts which are combined to create the management system around the particular database to be utilized.
A database management system, or DBMS, gives the user access to their data and helps them transform the data into information. Such database management systems include dBase, Paradox, IMS, and Oracle. These systems allow users to create, update, and extract information from their databases. Compared to a manual filing system, the biggest advantages to a computerized database system are speed, accuracy, and accessibility. A database is a structured collection of data. Data refers to the characteristics of people, things, and events.
DATA STRUCTURES Data structures (fields, records, files and objects) optimized to deal with very large amounts of data stored on a permanent data storage device (which implies relatively slow access compared to volatile main memory). DB QUERY LANGUAGE AND REPORT WRITER A database query language and report writer to allow users to interactively interrogate the database, analyze its data and update it according to the users privileges on data. TRANSACTION MECHANISM A transaction mechanism, that ideally would guarantee the ACID properties, in order to ensure data integrity, despite concurrent user accesses (concurrency control), and faults (fault tolerance).
Database advantages include the following: shared data; centralized control; disadvantages of redundancy control; improved data integrity; improved data security, and database systems; and, flexible conceptual design.
Disadvantages of database system Disadvantages are as follows: a complex conceptual design process; the need for multiple external databases; the need to hire database-related employees; high DBMS acquisition costs; a more complex programmer environment; potentially catastrophic program failures; a longer running time for individual applications; and, highly dependent DBMS operations.
This data type is used to store character strings values of fixed length. The size in brackets determines the number of characters the cell can hold. The maximum number of characters (i.e. the size) this data type can hold is 255 characters. Syntax is CHAR(SIZE) Example is CHAR (20) VARCHAR :
This data type is used to store variable length alphanumeric data. The maximum this data type can hold is 2000 characters. One difference between this data type and the CHAR data type is ORACLE compares VARCHAR values using non-padded
comparison semantics i.e. the inserted values will not be padded with spaces. Syntax is VARCHAR(SIZE) Example is VARCHAR (20) OR VARCHAR2 (20) NUMBER :
The NUMBER data type is used to store numbers (fixed or floating point). Numbers of virtually any magnitude maybe stored up to 38 digits of precision. Numbers as large as 9.99 * 10 to the power of 124, i.e. followed by 125 zeros can be stored. The precision, (P), determines the maximum length of the data, whereas the scale, (S), determines the number of places to the right of the decimal. If scale is omitted then the default is zero. If precision is omitted values are stored with their original precision up to the maximum of 38 digits. Syntax is NUMBER (P, S) Example is NUMBER (10, 2) LONG :
This data type is used to store variable length character strings containing up to 2GB. LONG data can be used to store arrays of binary data in ASCII format. LONG values cannot be indexed, and the normal character functions such as SUBSTR cannot be applied to LONG values. Syntax is LONG (SIZE) Example is LONG (20) DATE :
This data type is used to represent data and time. The standard format id DD-MMYY as in 13-JUL-85. To enter dates other than the standard format, use the appropriate functions. Date Time stores date in the 24-hour format. By default, the time in a date field is 12:00:00 am, if no time portion is specified. The default date for a date field is the first day of the current month. Syntax is DATE LONG RAW :
LONG RAW data types are used to store binary data, such as Digitized picture or image. Data loaded into columns of these data types are stored without any further conversion. LONG RAW data type can contain up to 2GB. Values stored in columns having LONG RAW data type cannot be indexed. Syntax is LONGRAW (SIZE) RAW :
It is used to hold strings of byte oriented data. Data type can have a maximum length of 255 bytes. Syntax is RAW(SIZE)
FEATURES OF ORACLE:
Client/Server Architecture Large database and Space Management Concurrent Processing High transaction processing performance High Availability Many concurrent database users Controlled availability Openness industry standards Manageable security Database enforced integrity Distributed systems Portability Compatibility
Updates the data Retrieves the data sharing Manages the data sharing Accepts the query statements PL/SQL and SQL Enforce the transaction consistency
This constraint ensures that the column contains no null values. As column without the NOT NULL constraint can contain NULL values BY DEFAULT. Here, On table STUDENT NOT NULL constraint is added to the LAST_NAME i.e. now LAST_NAME cant be NULL If we try to add NULL value in this column then following message will be displayed as ERROR. Whereas we can add NULL value to the NAME column as there exist no NOT NULL constraint.
PRIMARY KEY:
A PRIMARY KEY constraint creates a primary key for a table. Only one primary key can be created for each table. A PRIMARY KEY constraint is a column or a set of columns that are uniquely identifies each row in the table. This constraint enforces UNIQUENESS of the column or column combination and ensures that no column that is part of the primary key can contain a NULL value. NULL VALUES are NOT ALLOWED & ALREADY existing values are not replaced. In this Department_id is a PRIMARY KEY. i.e.
It do not contain DUPLICATE entries & NO NULL values. If we try to add DUPICATE values than following ERROR is encountered. If we try to add NULL value than following ERROR is encountered. PRIMARY KEY constraint can be defined at the COLUMN LEVEL or TABLE LEVEL. A composite PRIMARY KEY is created by using the TABLE-LEVEL definition. A table can have only one PRIMARY KEY constraint but can have several UNIQUE constraints. We can call them ALTERNATE KEYS.
FOREIGN KEY:
The FOREIGN KEY, or referential integrity constraint, designates a column or combination of columns as a foreign key and establishes a relationship between a primary key or a unique key in the same table or a different table. A FOREIGN KEY value must match an existing value in the parent table or be NULL FOREIGN KEYS are based on data values and are purely logical, not physical, pointers. SYNTAX: SQL> CREATE TABLE table2 ( department_id NUMBER(4) CONSTRAINT emp_deptid_fk REFERENCES table1(department_id), ) The FOREIGN KEY is defined in the child table, and the table containing the referenced column is the Parent table. The FOREIGN KEY is defined using the combination of the following keywords: FOREIGN KEY: is used to define the column in the child table at the table constraint level. REFERENCES: identifies the table and column in the parent table. ON DELETE CASCADE: indicates that when the row in the parent table is deleted, the dependent rows in the chile table will also be deleted. ON DELETE SET NULL: converts foreign key values to the NULL when parent value is REMOVED. Here, 5 NOT ALLOWED value as 5 doesnt EXIT
CHECK CONSTRAINT
The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
SQL CHECK CONSTRAINT ON CREATE TABLE
The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0. CREATE TABLE Persons ( P_Id int NOT NULL , LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (P_Id>0) )
SQL CHECK CONSTRAINT ON ALTER TABLE
To create a CHECK constraint on the "P_Id" column when the table is already created, use the following SQL: MySQL / SQL Server / Oracle / MS Access: ALTER TABLE Persons ADD CHECK (P_Id>0) To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
TO DROP A CHECK CONSTRAINT
To drop a CHECK constraint, use the following SQL: SQL Server / Oracle / MS Access: ALTER TABLE Persons DROP CONSTRAINT chk_Person
The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:
CREATE TABLE Persons (P_Id int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255)) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: CREATE TABLE Persons (P_Id intNOTNULL,LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT uc_Person ID UNIQUE (P_Id,LastName))
SQL UNIQUE CONSTRAINT ON ALTER TABLE
To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the following SQL: ALTER TABLE Persons ADD UNIQUE (P_Id) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
TO DROP A UNIQUE CONSTRAINT
To drop a UNIQUE constraint, use the following SQL: ALTER TABLE Persons DROP CONSTRAINT uc_PersonID
EXAMPLE:
The Persons table:
P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger
"P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons" table. The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table. The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents that invalid data form being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.
Alter table & Create table & drop table Create index & drop index Create view & drop view
Retrieval of information stored in database. Insertion of new information into database. Deletion of information from database. Modification of data stored in database.
A Data Control Language (DCL) is a computer language and a subset of SQL, used to control access to data in a database. Examples of DCL commands include:
GRANT to allow specified users to perform specified tasks. REVOKE to cancel previously granted or denied permissions.
EXPERIMENT NO: 5 AIM: TO USE ORACLE FUNCTIONS VIZ AGGREGATE, NUMERIC ,CONVERISON, STRING FUNCTIONS. THEORY: Aggregate Functions COUNT : This function returns the number of rows or non-null values for column x.
SYNTAX: COUNT([DISTINCT|ALL]COLUMN NAME) EXAMPLE:
700
Hansen
Now we want to count the number of orders from "Customer Nilsen".We use the following SQL statement:
AS
CustomerNilsen
FROM
Orders
WHERE
The result of the SQL statement above will be 1, because the customer Nilsen has made 1 order in total: CustomerNilsen 1
SUM : This
SYNTAX
function ireturns the sum of values for the column x. This function is applied on columns having numeric datatype and it returns the numeric value.
SUM([DISTINCT|ALL]COLUMN NAME)
EXAMPLE:
The following "Orders" table: O_Id 1 2 3 4 OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 OrderPrice 1000 1600 700 300 Customer Hansen Nilsen Hansen Hansen
5 6
2008/08/30 2008/10/04
2000 100
Jensen Nilsen
Now we want to find the sum of all "OrderPrice" fields". We use the following SQL statement:
Now to find the sum of all "OrderPrice" fields", we use the following SQL statement:
AVG : Ths function returns the average of values for the column x. It ignores the null
values in the column x. SYNTAX: AVG([DISTINCT|ALL]COLUMN NAME) EXAMPLE:
The following "Orders" table: O_Id 1 2 3 4 5 6 OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen
Now to find the average value of the "OrderPrice" fields, we use the following SQL statement:
MIN : This function returns the minimum of values for the column x for all the rows.
SYNTAX: MIN([DISTINCT|ALL]COLUMN NAME)
EXAMPLE:
The following "Orders" table:
O_Id 1 2 3 4 5 6
Now to find the minimum value of the "OrderPrice" column, we use the following SQL statement:
MAX : This function returns the maximum of values for the column x for all the rows.
SYNTAX: MAX([DISTINCT|ALL]COLUMN NAME) EXAMPLE:
The following "Orders" table: O_Id 1 2 3 4 5 6 OrderDATE 2008/11/12 2008/10/23 2008/09/02 2008/09/03 2008/08/30 2008/10/04 OrderPrice 1000 1600 700 300 2000 100 Customer Hansen Nilsen Hansen Hansen Jensen Nilsen
Now to find the maximum value of the "OrderPrice" column, we use the following SQL statement:
NUMERIC FUNCTIONS:
Numeric functions are used to perform operations on numbers. They accept numeric values as input and return numeric values as output. Few of the Numeric functions are: Function Name Return Value ABS (x) CEIL (x) FLOOR (x) TRUNC (x, y) ROUND (x, y) Absolute value of the number 'x' Integer value that is Greater than or equal to the number 'x' Integer value that is Less than or equal to the number 'x' Truncates value of number 'x' up to 'y' decimal places Rounded off value of the number 'x' up to the number 'y' decimal places
The following examples explains the usage of the above numeric functions Function Name Examples ABS (1) ABS (x) ABS (-1) CEIL (2.83) CEIL (x) CEIL (2.49) CEIL (-1.6) FLOOR (2.83) FLOOR (x) FLOOR (2.49) FLOOR (-1.6) ROUND (125.456, 1) TRUNC (x, y) ROUND (125.456, 0) ROUND (124.456, -1) TRUNC (140.234, 2) TRUNC (-54, 1) ROUND (x, y) TRUNC (5.7) TRUNC (142, -1) These functions can be used on database columns. Return Value 1 -1 3 3 -1 2 2 -2 125.4 125 120 140.23 54 5 140
ROUND FUNCTION:
SYNTAX: SQL> SELECT ROUND(45.923, 2), ROUND(45.923,0), ROUND(45.923,-1) FROM DUAL;
TRUNC FUNCTION:
SYNTAX: SQL> SELECT TRUNC(45.923, 2), TRUNC(45.923,0), TRUNC(45.923,-1) FROM DUAL;
MOD FUNCTION:
SYNTAX: SQL> SELECT MOD(300,10), MOD(23,9) FROM DUAL; SQL> SELECT ename, MOD(sal,200) FROM emp;