DBMS
DBMS
By
By
Dr. Manomita Chakraborty
Assistant Professor
VIT-AP, Amaravati, AP, India
• SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in relational database.
• All relational database management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL Server use SQL as
standard database language.
• Allows users to define the data in database and manipulate that data.
• Allows to embed within other languages using SQL modules, libraries & pre-compilers.
• 1970 -- Dr. E. F. "Ted" of IBM is known as the father of relational databases. He described a relational model for databases.
• 1974 -- Structured Query Language appeared.
• 1978 -- IBM worked to develop Codd's ideas and released a product named System/R.
• 1986 -- IBM developed the first prototype of relational database and standardized by ANSI. The first relational database was
released by Relational Software and its later becoming Oracle.
VARCHAR(Size) It is used to specify a variable length string that can contain numbers, letters, and special characters. Its size can be from 0 to
65535 characters.
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size parameter specifies the column length in the bytes. Default is 1.
VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its size parameter specifies the maximum column length in bytes.
TEXT(Size) It holds a string that can contain a maximum length of 255 characters.
ENUM(val1, val2, val3,...) It is used when a string object having only one value, chosen from a list of possible values. It contains 65535 values in an ENUM
list. If you insert a value that is not in the list, a blank value will be inserted.
SET( val1,val2,val3,....) It is used to specify a string that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values at one
time in a SET list.
BLOB(size) It is used for BLOBs (Binary Large Objects). It can hold up to 65,535 bytes.
FLOAT(p) It is used to specify a floating point number. MySQL used p parameter to determine whether to use FLOAT or
DOUBLE. If p is between 0 to24, the data type becomes FLOAT (). If p is from 25 to 53, the data type becomes
DOUBLE().
DOUBLE(size, d) It is a normal size floating point number. Its size parameter specifies the total number of digits. The number of digits
after the decimal is specified by d parameter.
DECIMAL(size, d) It is used to specify a fixed point number. Its size parameter specifies the total number of digits. The number of digits
after the decimal parameter is specified by d parameter. The maximum value for the size is 65, and the default value
is 10. The maximum value for d is 30, and the default value is 0.
DATETIME(fsp) It is used to specify date and time combination. Its format is YYYY-MM-DD hh:mm:ss. Its
supported range is from '1000-01-01 00:00:00' to 9999-12-31 23:59:59'.
TIMESTAMP(fsp) It is used to specify the timestamp. Its value is stored as the number of seconds since the Unix
epoch('1970-01-01 00:00:00' UTC). Its format is YYYY-MM-DD hh:mm:ss. Its supported
range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC.
TIME(fsp) It is used to specify the time format. Its format is hh:mm:ss. Its supported range is from '-
838:59:59' to '838:59:59'
YEAR It is used to specify a year in four-digit format. Values allowed in four digit format from 1901
to 2155, and 0000.
Example:
CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255));
✓ TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are removed.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE Categories;
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query.
Example:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
✓ UPDATE: Command to change or update the present/existing data to a newer value inside the database.
UPDATE table_name SET column1 = value1, column2 = value2…., columnN = valueN WHERE [condition];
Example:
UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt’ WHERE CustomerID = 1;
✓ DELETE: Command to remove or delete the values or data information from the database’s current table.
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
Syntax:
Syntax:
Example:
grant insert, select on accounts to Ram
By the above command user ram has granted permissions on accounts database object like he can query or insert into accounts.
revoke insert, select on accounts from Ram
By the above command user ram’s permissions like query or insert on accounts database object has been removed.
✓ Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
COMMIT;
✓ Rollback: Rollback command is used to undo transactions that have not already been saved to the database.
Syntax: ROLLBACK;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;
✓ SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
✓ SELECT: This is the same as the projection operation of relational algebra. It is used to select the attribute based on the condition
described by WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
Operator Description
ALL It compares a value to all values in another value set.
BETWEEN It is used to search for values that are within a set of values.
✓ https://www.oracle.com/tools/downloads/sqldev-downloads.html
✓ Unzip the files in a folder named Sqldeveloper in c drive.
or
If all the steps are done your database is working correctly, installation done properly
3/29/2022 By Dr. Manomita Chakraborty, SCOPE, VIT-AP UNIVERSITY 23
ORACLE
2. Checking with the default databases present:
✓ Open sqldeveloper:
✓ Under the databases detected tab, press load TNS files, it will show all the default databases
✓ Right click on any of the databases under the TNS FILE and click on properties.
Open Sqldeveloper:
✓ You can also create a database with an user which you have created .
How to create user:
1. Open sqlplus
2. Log in by default id : ‘/’ as sysdba.
3. create an user:
• Syntax: create user username identified by password.
4. Grant access to the user
• Syntax: grant all privileges to username;
5. Example:
User created.
Grant succeeded.
✓ The NOT NULL constraint specifies that the column does not accept NULL values.
✓ The following SQL statement creates a table named persons with four columns, out of which three columns,
id, name and phone do not accept NULL values.
**A null value or NULL is different from zero (0), blank, or a zero-length character string such as ''. NULL means that no entry
has been made.
3/29/2022 35
SQL CONSTRAINTS
PRIMARY KEY Constraint
✓ The PRIMARY KEY constraint identify the column or set of columns that have values that uniquely identify a row
in a table.
✓ No two rows in a table can have the same primary key value. Also, you cannot enter NULL value in a primary key
column.
✓ The following SQL statement creates a table named persons and specifies the id column as the primary key.
That means this field does not allow NULL or duplicate values..
3/29/2022 36
SQL CONSTRAINTS
UNIQUE Constraint
✓ The UNIQUE constraint restricts one or more columns to contain unique values within a table..
✓ Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint
instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of a column, or combination of
columns, that is not the primary key..
✓ The following SQL statement creates a table named persons and specifies the phone column as unique.
That means this field does not allow duplicate values.
*** Multiple UNIQUE constraints can be defined on a table, whereas only one PRIMARY KEY constraint can be defined on a table. Also, unlike PRIMARY KEY
constraints, the UNIQUE constraints allow NULL values.
3/29/2022 37
SQL CONSTRAINTS
DEFAULT Constraint
✓ The DEFAULT constraint specifies the default value for the columns.
✓ A column default is some value that will be inserted in the column by the database engine when an INSERT statement
doesn't explicitly assign a particular value.
✓ The following SQL statement creates a default for the country column..
3/29/2022 38
SQL CONSTRAINTS
FOREIGN KEY Constraint
✓ A foreign key (FK) is a column or combination of columns that is used to establish and enforce a relationship between
the data in two tables.
✓ The following statement establishes a foreign key on the dept_id column of the employees table that references the dept_id
column of the departments table.
CREATE TABLE employees (
emp_id INT NOT NULL PRIMARY KEY,
emp_name VARCHAR(55) NOT NULL,
hire_date DATE NOT NULL,
salary INT,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES
departments(dept_id)
);
3/29/2022 39
SQL CONSTRAINTS
CHECK Constraint
✓ The CHECK constraint is used to restrict the values that can be placed in a column.
✓ For example, the range of values for a salary column can be limited by creating a CHECK constraint that allows values
only from 3,000 to 10,000.
3/29/2022 40
Behavior of foreign key
✓ The foreign key is defined in the child table and the parent table contains the reference column. However because of this
link we cannot update or delete the rows of the parent table.
✓ When you define a simple foreign key, the Oracle engine is by default set to ON DELETE NO ACTION clause. This
means that you are allowed to update the rows in the parent table however you cannot delete rows from the parent table.
This default behavior is called Restrict rule.
✓ In Oracle Database you get two referential actions to override the restrict rule and change the default behavior of
foreign key. These two referential actions are –
3/29/2022 41
FOREIGN KEY WITH CASCADE DELETE
✓ Because of the cascade delete, when a record in the supplier table is deleted, all records in the products table will also be
deleted that have the same supplier_id value..
**
3/29/2022 42
FOREIGN KEY WITH ON DELETE SET NULL
✓ On Delete Set Null clause sets all the records of the column which is defined as a foreign key in the child table to Null if
the corresponding record in the parent table is deleted..
✓ Because of ON DELETE SET NULL, when a record in the supplier table is deleted, all records in the products table will
be set to NULL that have the same supplier_id value.
3/29/2022 44
SQL GROUP BY
✓ The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each
country".
✓ The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-
set by one or more columns.
TABLE CUSTOMERS
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s);
EXAMPLE:
SELECT NAME, SUM(SALARY) FROM CUSTOMERS
GROUP BY NAME;
3/29/2022 45
SQL ORDER BY
✓ The ORDER BY keyword is used to sort the result-set in ascending or descending order.
✓ The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC
keyword..
3/29/2022 47
SQL HAVING
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions;
Following is an example, display the state and number of customers in each state where the salary is greater than 10000 and the
number of customers are more than or equal to 2.
SELECT state, COUNT(*) AS "Number of customers"
FROM customers
WHERE salary > 10000
TABLE customers GROUP BY state
HAVING COUNT(*) >= 2;
3/29/2022 48