DBMS II BCA IVthMod
DBMS II BCA IVthMod
DBMS II BCA IVthMod
II Semester BCA
Question Bank
(Unit IV)
Prepared By
Ms. Sheethal Abraham
Asst. Professor, Department of Science & Computer Applications
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));
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’);
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.
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
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;
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;
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’;
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;
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.