DATABASES MANAGEMENT SYSTEM AND SQL
DBMS & Structured Query Language
Basic Database concepts
Data : Raw facts and figures which are useful to an organization. We cannot make decisions on
the basis of data.
Information: Well processed data is called information. We can take decisions on the basis of
information
Field: Set of characters that represents a specific data element.
Record: Collection of fields is called a record. A record can have fields of different data types.
File: Collection of similar types of records is called a file.
Table: Collection of rows and columns that contains useful data/information is called a table. A
table generally refers to the passive entity which is kept in a secondary storage device.
Relation: Relation (collection of rows and columns) generally refers to an active entity on which
we can perform various operations.
Database: Collection of logically related data along with its description is termed as database.
Tuple: A row in a relation is called a tuple.
Attribute: A column in a relation is called an attribute. It is also termed as a field or data item.
Degree: Number of attributes in a relation is called degree of a relation.
Cardinality: Number of tuples in a relation is called cardinality of a relation.
Primary Key: Primary key is a key that can uniquely identify the records/tuples in a relation.
This key can never be duplicated and NULL.
Foreign Key: Non key attribute of a table acting as primary key in some other table is known as
Foreign Key in its current table. This key is used to enforce referential integrity in RDBMS.
SQL is used to create, manipulate and process the databases(relations).
1. Data Definition Language (DDL)
DDL contains commands that are used to create the tables, databases, indexes, views,
sequences and synonyms etc.
e.g: Create table, create view, create index, alter table, drop table etc.
2. Data Manipulation Language (DML)
DML contains commands that can be used to manipulate the database objects and to query the
databases for information retrieval.
e.g Select, Insert, Delete, Update etc.
3. Transaction Control Language (TCL)
TCL includes commands to control the transactions in a database system. The commonly used
commands in TCL are COMMIT, ROLLBACK etc.
- Operators in SQL: The following are the commonly used operators in SQL
1. Arithmetic Operators +, -, *, /
2. Relational Operators =, <, >, <=, >=, <>
1
3. Logical Operators OR, AND, NOT
-Data types of SQL: Just like any other programming language, the facility of defining data of
various types is available in SQL also. Following are the most common data types of SQL.
1) int
2) VARCHAR(SIZE)
4) DATE
5) Float
- Constraints: Constraints are the conditions that can be enforced on the attributes of a
relation. The constraints come into play whenever we are trying to insert, delete or update a
record in a relation.
Primary key constraint means that a column can not have duplicate values and not even a null
value.
e.g. Roll_no int primary key
The main difference between unique and primary key constraint is that a column specified as
unique may have null value but primary key constraint does not allow null values in the column.
Foreign key is used to enforce referential integrity and is declared as a primary key in some
other table.
e.g. cust_id varchar(5) references master(cust_id)
it declares cust_id column as a foreign key that refers to cust_id field of table master.
That means we cannot insert that value in cust_id filed whose corresponding value is not present
in cust_id field of the master table. Moreover we can’t delete any row in the master table , if a
corresponding value of cust_id field is existing in the dependent table.
Steps to open SQL in Open Office Base-
1. Open the software Open office base.
2. Go to Tools Menu and Click SQL.
3. Type SQL commands in Command to Execute box and Press Execute button.
SQL COMMANDS :
1. Create Table command is used to create a table . The syntax of this Command is:
CREATE TABLE <Table_name>
( column_name 1 data_type1 [(size) column_constraints],
column_name 1 data_type1 [(size) column_constraints],
:
:
[<table_constraint> (column_names)] );
For eg:
CREATE TABLE STUDENT
2
(
Rno int,
Name Varchar(30),
Age int,
DOB date
);
Note- After executing create table command , you need to restart your open office base ,
then only your created table will be visible. In open office base further SQL queries can
be applied only on that table which is created using SQL query only.
2. The ALTER Table command is used to change the definition (structure) of an existing table.
ALTER TABLE <Table_name> ADD/MODIFY <Column_defnition>; For Add or modify
column
For eg.
ALTER TABLE STUDENT ADD address varchar(100);
ALTER TABLE STUDENT MODIFY Name varchar(50);
ALTER TABLE <Table_name> DROP COLUMN <Column_name>; For Deleting a column
For eg. ALTER TABLE STUDENT DROP address;
3. The INSERT Command: The rows (tuples) are added to a table by using the INSERT
command. The syntax of Insert command is:
INSERT INTO <table_name> [(<column_list>)] VALUES (<value_list>); e.g.,
For eg. INSERT INTO EMP (empno, ename, sex, sal, deptno) VALUES(1001, ’Ravi’, ’M’,
4500.00, 10);
If the order of values matches the actual order of columns in table then it is not required to
give the column_list in INSERT command. e.g.
INSERT INTO EMP VALUES(1001, ’Ravi’, ’M’, 4500.00, 10);
4. The SELECT command is used to make queries on database. A query is a command that is
given to produce certain specified information from the database table(s). The SELECT
command can be used to retrieve a subset of rows or columns from one or more tables. The
syntax of Select Command is:
3
SELECT <Column-list>
FROM <table_name>
[WHERE <condition>]
[GROUP BY <column_list>]
The select clause lists the attributes desired in the result of a query. e.g.,To
display the names of all Employees in the emp relation:
select ename from emp;
To force the elimination of duplicates, insert the keyword distinct after select. Find the
number of all departments in the emp relations, and remove duplicates
select distinct deptno from emp;
An asterisk (*) in the select clause denotes “all attributes”
SELECT * FROM emp;
The select clause can contain arithmetic expressions involving the operation, +, –, *, and /, and
operating on constants or attributes of tuples. The query:
SELECT empno, ename, sal * 12 FROM emp;
would display all values same as in the emp relation, except that the value of the attribute sal is
multiplied by 12.
The WHERE clause in the SELECT statement specifies the criteria for selection of rows to be
returned.
SELECT * FROM emp Where sal = 4500;
SELECT * FROM emp Where ename = ‘Ravi’;
SELECT * FROM emp Where sal = 4500 AND deptno = 10 ;
SELECT * FROM emp Where sal = 4500 OR deptno = 10 ;
SELECT * FROM emp Where sal <> 4500 ;
-Conditions based on a range (BETWEEN Operator): The Between operator defines a range
of values that the column values must fall in to make the condition true . The range includes both
lower value and upper value.
e.g., Find the empno of those employees whose salary between 90,000 and 100,000 (that is,
90,000 and 100,000)
SELECT empno FROM emp WHERE sal BETWEEN 90000 AND 100000;
- ORDER BY Clause: Whenever a select query is executed the resulting rows are displayed in
the order in which they exist in the table. You can sort the result of a query in a specific order
using the ORDER BY clause. The ORDER BY clause allows sorting of query results by one or
more columns. The sorting can be done either in ascending or descending order.
4
SELECT * FROM emp ORDER BY sal ASC;
SELECT * FROM emp ORDER BY sal DESC;
SELECT * FROM emp ORDER BY sal;
Note:- If order is not specified that by default the sorting will be performed in ascending order.
5. The Update command is used to change the value in a table. The syntax of this command is:
UPDATE <table_name>
SET column_name1=newvalue1/expression [,column_name2=newvalue2/expression,……]
WHERE <condition>;
e.g., to increase the salary of all the employees of department No 10 by 10% , then command
will be:
UPDATE emp
SET sal=sal*1.1
WHERE Deptno=10;
6. The DELETE command removes rows from a table. This removes the entire rows, not
individual field values. The syntax of this command is
DELETE FROM <table_name>
[WHERE <condition>];
e.g., to delete the tuples from EMP that have salary less than 2000, the following command is
used:
DELETE FROM emp WHERE sal<2000;
To delete all tuples from emp table:
DELETE FROM emp;
7. The DROP Command : The DROP TABLE command is used to drop (delete) a table from a
database. But there is a condition for dropping a table ; it must be an empty table i.e. a table with
rows in it cannot be dropped.The syntax of this command is :
DROP TABLE <Table_name>;
e.g.,
DROP TABLE EMP;