Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS II BCA IVthMod

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

INDO ASIAN ACADEMY DEGREE COLLEGE & PG CENTER

KALYAN NAGAR, BANGALORE


(Affiliated to Bangalore University, Accredited by NAAC)

DATABASE MANAGEMENT SYSTEMS

II Semester BCA

Question Bank
(Unit IV)

Prepared By
Ms. Sheethal Abraham
Asst. Professor, Department of Science & Computer Applications

Database Management Systems Indo Asian Academy Degree College 1


Unit IV
Relational Database Language
SQL
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards Institute) standard
SQL is a declarative (non-procedural)language. SQL is (usually) not case-sensitive,
but we’ll write SQL keywords in upper case for emphasis.
Some database systems require a semicolon at the end of each SQL statement.
A table is database object that holds user data. Each column of the table will have
specified data type bound to it. Oracle ensures that only data, which is identical to the datatype of the
column, will be stored within the column.

SQL DML ,DDL. DCL and TCL


SQL can be divided into four parts:
The Data Definition Language (DDL) and the Data Manipulation Language (DML), Data Control
Language (DCL) and Transaction Control Language (TCL)...

Data Definition Language (DDL)


It is a set of SQL commands used to create, modify and delete database structure
but not data. It also define indexes (keys), specify links between tables, and
impose constraints between tables.
DDL commands are auto COMMIT.
The most important DDL statements in SQL are:
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
TRUNCATE TABLE- deletes all records from a table
DROP TABLE - deletes a table

Data Manipulation Language (DML)


It is the area of SQL that allows changing data within the database. The query
and update commands form the DML part of SQL:
• INSERT - inserts new data into a database
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database

Data Control Language (DCL)


It is the component of SQL statement that control access to data and to the
database. Occasionally DCL statements are grouped with DML Statements.
COMMIT –Save work done.
Database Management Systems Indo Asian Academy Degree College 2
SAVEPOINT – Identify a point in a transaction to which you can later rollback.
ROLLBACK – Restore database to original since the last COMMIT.

Transaction Control Language (TCL)

GRANT – gives user’s access privileges to database.


REVOKE – withdraw access privileges given with GRANT command.

Basic Data Types

Database Management Systems Indo Asian Academy Degree College 3


The CREATE TABLE Command:

The CREATE TABLE command defines each column of the table uniquely. Each column has a minimum of
three attributes, name, datatype and size(i.e column width).each table column definition is a single clause in
the create table syntax. Each table column definition is separated from the other by a
comma. Finally, the SQL statement is terminated with a semi colon.
Rules for Creating Tables
 A name can have maximum upto 30 characters.
 Alphabets from A-Z, a-z and numbers from 0-9 are allowed.
 A name should begin with an alphabet.
 The use of the special character like _(underscore) is allowed.
 SQL reserved words not allowed. For example: create, select, alter.
Syntax:
CREATE TABLE <tablename>
(<columnName1> <Datatype>(<size>),
<columnName2> <Datatype>(<size>), ……. );
Example:
CREATE TABLE gktab
(Regno NUMBER(3),
Name VARCHAR(20),
Gender CHAR,
Dob DATE,
Course CHAR(5));

Inserting Data into Tables

Once a table is created, the most natural thing to do is load this table with data to
be manipulated later.
When inserting a single row of data into the table, the insert operation:
 Creates a new row(empty) in the database table.
 Loads the values passed(by the SQL insert) into the columns specified.
Syntax:
INSERT INTO <tablename>(<columnname1>, <columnname2>, ..)
Values(<expression1>,<expression2>…);

Example:
INSERT INTO gktab(regno,name,gender,dob,course)
VALUES(101,’Varsh G Kalyan’,’F’,’20-Sep-1985’,’BCA’);
Or you can use the below method to insert the data into table.
INSERT INTO gktab VALUES(102,’Mohith G Kalyan’,’M’,’20-Aug-1980’,’BBM’);
INSERT INTO gktab VALUES(106,’Nisarga’,’F’,’15-Jul-1983’,’BCom’);
INSERT INTO gktab VALUES(105,’Eenchara’,’F’,’04-Dec-1985’,’BCA’);
INSERT INTO gktab VALUES(103,’Ravi K’,’M’,’29-Mar-1989’,’BCom’);

Database Management Systems Indo Asian Academy Degree College 4


INSERT INTO gktab VALUES(104,’Roopa’,’F’,’17-Jan-1984’,’BBM’);
Whenever you work on the data which has data types like CHAR,VARCHAR/VARCHAR2, DATE should be used
between single quote(‘)

Viewing Data in the Tables

Once data has been inserted into a table, the next most logical operation would be to view what has been
inserted. The SELECT SQL verb is used to achieve this. The SELECT command is used to retrieve rows selected
from one or more tables.

All Rows and All Columns

SELECT * FROM <tablename>


SELECT * FROM gktab;
It shows all rows and column data in the table

Filtering Table Data

While viewing data from a table it is rare that all the data from the table will berequired each time. Hence, SQL
provides a method of filtering table data that is not required.
The ways of filtering table data are:
 Selected columns and all rows
 Selected rows and all columns
 Selected columns and selected rows

Selected Columns and All Rows

The retrieval of specific columns from a table can be done as shown below.
Syntax
SELECT <columnname1>, <Columnname2> FROM <tablename>
Example
Show only Regno, Name and Course from gktab.
SELECT Regno, Name, Course FROM gktab;

Database Management Systems Indo Asian Academy Degree College 5


Selected Rows and All Columns
The WHERE clause is used to extract only those records that fulfill a specified
criterion.
When a WHERE clause is added to the SQL query, the Oracle engine compares each record in the table with
condition specified in the WHERE clause. The Oracle engine displays only those records that satisfy the
specified condition.

Syntax
SELECT * FROM <tablename> WHERE <condition>;
Here, <condition> is always quantified as <columnname=value>
When specifying a condition in the WHERE clause all standard operators such as logical, arithmetic and so on,
can be used.

Example-1:
Display all the students from BCA.
SELECT * FROM gktab WHERE Course=’BCA’;

Example-2:
Display the student whose regno is 102.
SELECT * FROM gktab WHERE Regno=102;

Selected Columns and Selected Rows

To view a specific set of rows and columns from a table


When a WHERE clause is added to the SQL query, the Oracle engine compares each record in the table with
condition specified in the WHERE clause. The Oracle engine displays only those records that satisfy the
specified condition.

Database Management Systems Indo Asian Academy Degree College 6


Syntax
SELECT <columnname1>, <Columnname2> FROM <tablename>
WHERE <condition>;

Example-1:
List the student’s Regno, Name for the Course BCA.
SELECT Regno, Name FROM gktab WHERE Course=’BCA’;

Example-2:
List the student’s Regno, Name, Gender for the Course BBM.
SELECT Regno, Name, Gender FROM gktab WHERE Course=’BBM’;

Eliminating Duplicate Rows when using a SELECT statement

A table could hold duplicate rows. In such a case, to view only unique rows the
DISTINCT clause can be used.
The DISTINCT clause allows removing duplicates from the result set. The
DISTINCT clause can only be used with SELECT statements.
The DISTINCT clause scans through the values of the column/s specified and
displays only unique values from amongst them.
Syntax
SELECT DISTINCT <columnname1>, <Columnname2>
FROM <Tablename>;

Example:
Show different courses from gktab
SELECT DISTINCT Course from gktab;

Sorting Data in a Table

Oracle allows data from a table to be viewed in a sorted order. The rows retrieved from the table will be sorted
in either ascending or descending order depending on the condition specified in the SELECT sentence.

Database Management Systems Indo Asian Academy Degree College 7


Syntax
SELECT * FROM <tablename>
ORDER BY <Columnname1>,<Columnname2> <[Sort Order]>;
The ORDER BY clause sorts the result set based on the column specified. The ORDER BY clause can only be used
in SELECT statements. The Oracle engine sorts in ascending order by default

Database Management Systems Indo Asian Academy Degree College 8


Database Management Systems Indo Asian Academy Degree College 9
Database Management Systems Indo Asian Academy Degree College 10
Database Management Systems Indo Asian Academy Degree College 11
Database Management Systems Indo Asian Academy Degree College 12
Database Management Systems Indo Asian Academy Degree College 13
Database Management Systems Indo Asian Academy Degree College 14
Database Management Systems Indo Asian Academy Degree College 15
Database Management Systems Indo Asian Academy Degree College 16
Database Management Systems Indo Asian Academy Degree College 17
Database Management Systems Indo Asian Academy Degree College 18
Database Management Systems Indo Asian Academy Degree College 19
Database Management Systems Indo Asian Academy Degree College 20
Database Management Systems Indo Asian Academy Degree College 21
Database Management Systems Indo Asian Academy Degree College 22
Database Management Systems Indo Asian Academy Degree College 23
Database Management Systems Indo Asian Academy Degree College 24
Database Management Systems Indo Asian Academy Degree College 25
Database Management Systems Indo Asian Academy Degree College 26
Database Management Systems Indo Asian Academy Degree College 27
Database Management Systems Indo Asian Academy Degree College 28
Database Management Systems Indo Asian Academy Degree College 29
Database Management Systems Indo Asian Academy Degree College 30
Database Management Systems Indo Asian Academy Degree College 31
Database Management Systems Indo Asian Academy Degree College 32
Database Management Systems Indo Asian Academy Degree College 33
Database Management Systems Indo Asian Academy Degree College 34
Database Management Systems Indo Asian Academy Degree College 35
Database Management Systems Indo Asian Academy Degree College 36
Database Management Systems Indo Asian Academy Degree College 37
Database Management Systems Indo Asian Academy Degree College 38

You might also like