Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

BMS Institute of Technology and Management: Department of Master of Computer Applications

This document provides an overview of SQL (Structured Query Language) and its basic components and commands. It describes DDL, DML, and DCL commands and how they are used to define, manipulate, and manage database objects and data. Examples are given for SQL statements to create tables, insert, select, update, delete, and modify data. Functions for aggregation, math operations, strings are also summarized. The document is intended as an introduction to SQL basics for a database management laboratory course.

Uploaded by

Rupak Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

BMS Institute of Technology and Management: Department of Master of Computer Applications

This document provides an overview of SQL (Structured Query Language) and its basic components and commands. It describes DDL, DML, and DCL commands and how they are used to define, manipulate, and manage database objects and data. Examples are given for SQL statements to create tables, insert, select, update, delete, and modify data. Functions for aggregation, math operations, strings are also summarized. The document is intended as an introduction to SQL basics for a database management laboratory course.

Uploaded by

Rupak Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BMS Institute of Technology and Management

(Affiliated to VTU)
Post Box No. 6443, Doddaballapura Main Road, Yelahanka, Bengaluru – 560 064.

Department of Master of Computer Applications


(Accredited by NBA, New Delhi)

DBMS Laboratory - SQL Basics

SQL – Structured Query Language – provides an interface to the relational database system. It is
developed by IBM in 1970s.

Components of SQL
1. DDL: Data Definition Language – is a set of SQL commands used to create, modify, delete the
database structure but not the data.

DDL Commands
1. Create – Creates the objects in the database
2. Alter – Alters the structure of the database
3. Drop – Deletes the objects from the database
4. Truncate – Remove all the records from the table including all the spaces allocated for record.
5. Grant – Give the users access privileges to the database.
6. Revoke – Withdraw the access privileges

2. DML: Data Manipulation Language is a set of SQL commands that allows changing the data within
the database.

DML Commands
1. Insert – Insert data into the table
2. Update – Update the existing data within the table
3. Delete – Delete all the records from the table, the space allocated remains

DCL Commands
1. Commit – Save the work done
2. Savepoint – Identify the point in a transaction
3. Rollback – Restore the database to the original position since the last commit

DQL Commands – Data Query Language


1. Select – Retrieves the data from the database

Creation of a table
Syntax: create table <tablename> (<col-name1><datatype>(size), <col-name2><datatype>(size));

Dr. Aparna K, Assoc. Prof, Dept. of MCA, BMSIT&M Page 1


Insertion of data
Syntax1: insert into <tablename>(col-name1, col-name2) values (expr1, expr2,…. , exprn);
Syntax2: insert into <tablename> values (‘&colname1’, ‘&colname2’, …. , ‘&colname-n’);

Select command
1. To select all columns and selected rows
select * from <tablename> where <condition>;
2. To select all rows and selected columns
select <colname1, colname2,…> from <tablename>;
3. To select all rows and all columns
select * from <tablename>;

Eliminate the duplicate data


A table can hold duplicate rows. In such a case, to view only unique rows, the distinct keyword can
be used with the select statement. Distinct clause allows removing duplicate from the result set.
Syntax: select distinct <colname> from <tablename>;

Sort the data


Ascending order: select * from <tablename> order by <col-name>;
Descending order: select * from <tablename> order by <col-name> desc;

Delete the data


1. All rows: delete * from <tablename>;
2. Selected rows: delete * from <tablename> where <condition>;

Add column to the existing table


Syntax: alter table <tablename> add (<new col name> <datatype>(size));

Drop column of the existing table


Syntax: alter table <tablename> drop column <col-name>;

Add values to the new column


Syntax: update <tablename> set <col-name> = ‘expr’ where <condition>;

Truncate the table


Syntax: truncate table <tablename>;

Add primary key to the existing table


Syntax: alter table <tablename> add primary key (col-name);

Drop the table


Syntax: drop table <tablename>;

Dr. Aparna K, Assoc. Prof, Dept. of MCA, BMSIT&M Page 2


Modify the size of the table
Syntax: alter table <tablename> modify (col-name datatype(size));

Rename the table


Syntax: rename <old tablename> to <new tablename>;

Logical Operators
1. AND – select <col-name1, col-name2> from <tablename> where <cond1> and <cond2>;
2. OR – select <col-name1, col-name2> from <tablename> where <cond1> or <cond2>;
3. NOT – select * from <tablename> where not (cond1 or cond2);

Range searching
1. Between – select <colname1, colname2> from <tablename> where <colname1> between ‘expr1’
and ‘expr2’;
2. select <colname1, colname2> from <tablename> where <colname1> not between ‘expr1’ and
‘expr2’;

Pattern matching
The like predicate allows for comparison of one string value with the other string value, which is not
identical. This is achieved by using wild card characters % and _
% sign – select * from <tablename> where <colname> like ‘2%’; -------------------------Example
_ sign – select * from <tablename> where color like ‘_ _ _ e%; ---------------------------Example

Aggregate functions
1. avg(n) – select avg (<colname>) from <tablename>;
2. min(n) - select min (<colname>) from <tablename>;
3. max(n) - select max (<colname>) from <tablename>;
4. count(expr) – select count (<colname>) from <tablename>;
5. sum(expr) - select sum (<colname>) from <tablename>;

Numeric functions
1. abs(n) – select abs(-15) from dual;
2. power(m,n) – select power(4, 4) as power from dual;
3. round (n,[m]) – select round(23.765) from dual;
4. sqrt(n) – select sqrt(36) from dual;

String functions
1. lower(char) – select lower(‘XYZ’) from dual;
2. initcap(char) – select initcap(‘lab’) from dual;
3. upper(char) – select upper(‘dbms’) from dual;
4. length(char) – select length(‘dbms’) from dual;
5. substr(char,m,[n]) – select substr(‘woman’, 3,5) from dual;
6. ltrim(char, [set]) – select ltrim (‘bread’, ‘b’) from dual;

Dr. Aparna K, Assoc. Prof, Dept. of MCA, BMSIT&M Page 3


7. rtrim(char, [set]) – select rtrim(‘sweety’, ‘y’) from dual;
8. lpad(char1,n,[char2]) – select lpad(‘sweet’, 9, ‘sour’) from dual;
9. rpad(char1, n, [char2]) – select rpad(‘sweet’, 9, ‘sour’) from dual;

To remove primary key from the existing table


Syntax: alter table <tablename> drop primary key;

Dr. Aparna K, Assoc. Prof, Dept. of MCA, BMSIT&M Page 4

You might also like