Sql

27
Copyright@2012 Trident Academ y of Technology 1 RDBMS LAB-1 Learning Objectives Introduction to SQL Concept of Table and Relation Oracle Classification of SQL Table Creation,Data Insertion,Data Retrieval,Table Structure Modification,Data Deletion and Table Deletion Prepared by: Akshaya Kumar Dash Sasmita Mishra

description

TRIDENT ACADEMY OF TECHNOLOGYCSE B (GROUP 1)

Transcript of Sql

Page 1: Sql

Copyright@2012 Trident Academy of Technology

1

RDBMS LAB-1

Learning Objectives Introduction to SQL Concept of Table and Relation Oracle Classification of SQL Table Creation,Data Insertion,Data Retrieval,Table

Structure Modification,Data Deletion and Table Deletion

Prepared by:Akshaya Kumar DashSasmita Mishra

Page 2: Sql

Copyright@2012 Trident Academy of Technology

2

Structured Query Language(SQL)

SQL is a database computer language designed for the retrieval and management of data in relational database.

SQL is structured Query Language which is a computer language for storing, manipulating and retrieving data stored in relational database.

SQL is the standard language for Relation Database System. All relational database management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL Server uses SQL as standard database language.

Page 3: Sql

Copyright@2012 Trident Academy of Technology

3

Why SQLAllows users to access data in relational database management Systems.Allow users to describe the data.Allow users to define data in database and manipulate that data.Allow to embed within other languages using SQL modules, libraries & pre-compilers.Allow users to create and drop databases and tables.Allow users to create view, stored procedure, functions in a database.Allow users to set permissions on tables, procedures, and views

Page 4: Sql

Copyright@2012 Trident Academy of Technology

4

Requirements to learn Oracle SQL

A Computer with following Softwares

OS:Windows or LinuxDB:Oracle 9i or any other Higher

version

Page 5: Sql

Copyright@2012 Trident Academy of Technology

5

Books

Beginnig SQL by Paul WiltonSQL Bible by Alex KriegelSQL for dummiesHead First SQLSQL,PL/SQL The Programming Language of Oracle by Ivan Bayross(Available in Library)You need not a buy a single book(only Bikalp is the lab classes or internet)

Page 6: Sql

Copyright@2012 Trident Academy of Technology

6

Oracle has grouped the commands of SQL into the following five sublanguages.

DDL(Data Definition Language):Create,Alter,Drop,Rename,TruncateDML(Data Manipulation Language)Insert,Update,DeleteDQL(Data Query Language)SelectDCL(Data Control Language)Grant,RevokeTCL(Transcation Control Language)Rollback,Savepoint

Page 7: Sql

Copyright@2012 Trident Academy of Technology

7

Relation and Tables

In relational database systems (DBS) data are represented using tables (relations). A query issued against the DBS also results in a table. A table has the following structure:

Page 8: Sql

Copyright@2012 Trident Academy of Technology

8

A table is uniquely identied by its name and consists of rows that contain the storedinformation, each row containing exactly one tuple (or record). A table can have one or more columns.A column is made up of a column name and adata type, and it describes an attribute of thetuples. The structure of a table, also calledrelation schema, thus is dened by its attributes.The type of information to be stored in a table isdefined by the data types of the attributes at tablecreation time.

Page 9: Sql

Copyright@2012 Trident Academy of Technology

9

Oracle Data Typeschar(n): Fixed-length character data (string), n characters long. The maximum size for n is 255 bytes (2000 in Oracle8). Note that a string of type char is always padded on right with blanks to full length of n.Example: char(40)varchar2(n): Variable-length character string. The maximum size for n is 2000 (4000 in Oracle8). Only the bytes used for a string require storage. Example: varchar2(80)number(o, d): Numeric data type for integers and reals. o = overall number of digits, d = number of digits to the right of the decimal point.Maximum values: o =38, d= -84 to +127. Examples: number(8), number(5,2)Note that, e.g., number(5,2) cannot contain anything larger than 999.99 without resulting in an error. Data types derived from number are int[eger], dec[imal], smallint and real.date: Date data type for storing date and time.The default format for a date is: DD-MMM-YY. Examples: '13-OCT-94', '07-JAN-98'

Page 10: Sql

Copyright@2012 Trident Academy of Technology

10

Create Command

Create Command Syntax:

CREATE TABLE table-name

{

Column-name-1 data-type-1 [constraint],

Column-name-1 data-type-2 [constraint],

Column-name-1 data-type-3 [constraint]

};

eg->

CREATE TABLE student(regd_no number(10),student_name varchar2(30),cgpa decimal(5,3),branch varchar2(10),mob_no number(10));

Page 11: Sql

Copyright@2012 Trident Academy of Technology

11

Describe Command will Display the column names and corressponding data types present in the table.describe table-name;Desc table-name;eg:desc student

Page 12: Sql

Copyright@2012 Trident Academy of Technology

12

Insert Command

It can be executed in the following three ways Insert into table-name

values(value1,value2,....,valuen); Insert into table-name (col1,col2,...,coln) values

(value1,value2,...,valuen); Insert into table-name

values(&col1,&col2,...,&coln);

Page 13: Sql

Copyright@2012 Trident Academy of Technology

13

View the content of table

SELECT STATEMENT Syntax:

SELECT col1,col2,col3,...,coln

FROM table-1,...,table-n

[WHERE condition]

[ORDER BY col1 [ASC|DESC] [, col2 [ASC|DESC] ...]];

Page 14: Sql

Copyright@2012 Trident Academy of Technology

14

SELECT CONTINUED...

The SELECT Clause lists the columns to display.

The FROM clause lists the tables from which to obtain the data

The WHERE clause specifies the condition or conditions that need to be satisfied by the rows of the tables indicated in the FROM clause

The ORDER BY clause indicates the criterion or criteria used to sort rows that satisfy WHERE clause

Page 15: Sql

Copyright@2012 Trident Academy of Technology

15

Where Clause

The Conditions are of the following form

Column-name comparisonoperator single-value

Comparison Operators Description

= Equal to

<> Not equal to

< Less than

<= Less than or equal to

> Greater than

> = Greater than or equal to

Page 16: Sql

Copyright@2012 Trident Academy of Technology

16

ALTER COMMAND One can add a new column,drop an existing

column,modify the datatype of a column,and drop the constraints using the following commands respectively.

ALTER TABLE table_name ADD COLUMN_name datatype;

ALTER TABLE table_name DROP COLUMN column_name;

ALTER TABLE table_name MODIFY (column_name,datatype);

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Page 17: Sql

Copyright@2012 Trident Academy of Technology

17

TRUNCATE COMMAND

TRUNCATE will remove all the rows of a table

TRUNCATE TABLE table-name;

Note: We can't truncate the rows of a table if there are

referential integrity constraints for which this table is parent table.

We can't roolback a TRUNCATE statement

Page 18: Sql

Copyright@2012 Trident Academy of Technology

18

DROP COMMAND

DROP command will permanently delete the table with all its data

DROP TABLE table-name [CASCADE CONSTRAINT];

Page 19: Sql

Copyright@2012 Trident Academy of Technology

19

Lab Assignment-1Create the follwing tables branch(branch_name,branch_city,assets) customer(customer_name,customer_street,customer

_city) loan(loan_number,branch_name,amount) borrower(customer_name,loan_number) account(account_number,branch_name,balance) depositor(customer_name,account_number)

Page 20: Sql

Copyright@2012 Trident Academy of Technology

20

Assignment Continued.. Populate the branch table with the following data

branch_name branch_city assets

Brighton Brooklyn 7100000

Downtown Brooklyn 9000000

Mianus Horseneck 400000

North Town Rye 3700000

Perryridge Horseneck 1700000

Pownal Bennington 300000

Redwood Palo Alto 2100000

Round Hill Horseneck 8000000

Page 21: Sql

Copyright@2012 Trident Academy of Technology

21

Assignment Continued.. Populate the customer table with the following data

Customer_name Customer_street Customer_city

Adams Spring Pittsfield

Brooks Senator Brooklyn

Curry North Rye

Glenn Sand Hill Woodside

Green Walnut Stamford

Hayes Main Harrison

Johnson Alma Palo Alto

Jones Main Harrison

Lindsay Park Pittsfield

Smith North Rye

Turner Putnam Stamford

Williams Nassau Princeton

Page 22: Sql

Copyright@2012 Trident Academy of Technology

22

Assignment Continued.. Populate the loan table with the following data

loan_number branch_name amount

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

Page 23: Sql

Copyright@2012 Trident Academy of Technology

23

Assignment Continued...

Populate the borrower realtion the following data

Customer_name loan_number

Adams L-16

Curry L-93

Hayes L-15

Jackson L-14

Jones L-17

Smith L-11

Smith L-23

Williams L-17

Page 24: Sql

Copyright@2012 Trident Academy of Technology

24

Assignment Continued...

Populate account relation with the following data

account_number branch_name balance

A-101 Downtown 500

A-215 Mianus 700

A-102 Perryridge 400

A-305 Round Hill 350

A-201 Brighton 900

A-222 Redwood 700

A-217 Brighton 750

Page 25: Sql

Copyright@2012 Trident Academy of Technology

25

Assignment Continued...

Populate the depositor relation with the following data

Customer_name account_number

Hayes A-102

Johnson A-101

Johnson A-201

Jones A-217

Lindsay A-222

Smith A-215

Turner A-305

Page 26: Sql

Copyright@2012 Trident Academy of Technology

26

Create One more table Create a student table with the following data

Regdno Student_name Branch

1001 Surya CSE

1002 Binaya ETC

1003 Arup CSE

Add the CGPA Column, to the student table (the type must be a floating point type) Drop CGPA columnModify the existing type of regdno.Rename student to student_tridentNow delete all the data present in the student_trident table.Drop the table

Page 27: Sql

Copyright@2012 Trident Academy of Technology

27

Thank You