Professional Documents
Culture Documents
SQL 1
SQL 1
Chittaranjan Pradhan
Client/Server
Laboratory 1 Database
SQL
SQL Statements
Data Types
Table Creation
Viewing Table
Structure
Record Insertion
Inserting Time
Querying Data
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
1.1
Introduction To SQL
Introduction To SQL
Chittaranjan Pradhan
1 RDBMS
RDBMS
Client/Server
2 Client/Server Database Database
SQL
Data Types
SQL*PLUS
Table Creation
Naming Conventions
Viewing Table
Structure
4 Data Types Record Insertion
Inserting Time
7 Record Insertion
Inserting Time
8 Querying Data
1.2
Introduction To SQL
RDBMS
Chittaranjan Pradhan
RDBMS
Client/Server
• Relational database is a collection of related information Database
that has been organized into tables. Each table contains SQL
SQL Statements
rows and columns SQL*PLUS
Naming Conventions
• The tables are stored in the database in structures known Data Types
as schemas Table Creation
Viewing Table
Structure
• Each row is called an entity, thus table as entity set
Record Insertion
• Row is known as Tuple Inserting Time
Querying Data
• Columns are the properties called Attributes
• The facts describing an entity are known as data
1.3
Introduction To SQL
Employee Table
Chittaranjan Pradhan
RDBMS
Client/Server
Database
SQL
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO SQL Statements
SQL*PLUS
7369 SMITH CLERK 7902 17-DEC-80 800 20
Naming Conventions
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 Data Types
1.4
Introduction To SQL
Examples of RDBMS
Chittaranjan Pradhan
• Oracle
RDBMS
• DB2 Client/Server
Database
• Microsoft SQL-Server
SQL
• SQLite
• Vertica
• IBM Informix
• Ingres
• IBM Lotus
• SQL Anywhere
1.5
Introduction To SQL
Client/Server Database
Chittaranjan Pradhan
RDBMS
Client/Server
Database
SQL
• Client/Server databases run the DBMS as a process on SQL Statements
SQL*PLUS
the server and run a client database application on each Naming Conventions
Table Creation
• The client application sends a request for data over the
Viewing Table
network to the server Structure
Record Insertion
• When the server receives the client request, the DBMS Inserting Time
retrieves data from the database, performs the required Querying Data
1.6
Introduction To SQL
SQL
Chittaranjan Pradhan
RDBMS
• SQL( Structured Query Language) is the standard Client/Server
language for relational database Database
SQL
• IBM developed the original version of SQL in early 1970s, SQL Statements
SQL*PLUS
with name Sequel Naming Conventions
• In 1986, the ANSI & ISO published the SQL standard, Data Types
Table Creation
SQL-86
Viewing Table
• ANSI published extended standard, SQL-89 in 1989 Structure
Record Insertion
• Next versions are SQL-92, SQL-1999, SQL-2003, Inserting Time
1.7
Introduction To SQL
SQL Statements
Chittaranjan Pradhan
RDBMS
Data Definition Language(DDL)
Client/Server
Database
Defines the data structure that make up a database
SQL
• CREATE statement SQL Statements
SQL*PLUS
Data Types
• DROP statement Table Creation
Querying Data
Data Manipulation Language(DML)
Modifies the contents of tables
• INSERT statement
• UPDATE statement
• DELETE statement
1.8
Introduction To SQL
SQL Statements...
Chittaranjan Pradhan
Query Statement
RDBMS
Retrieves data from the database Client/Server
Database
• SELECT statement SQL
SQL Statements
SQL*PLUS
Naming Conventions
Transaction Control Language(TCL)
Data Types
Permanently records the changes made to the rows stored in a Table Creation
• SAVEPOINT statement
1.9
Introduction To SQL
SQL*PLUS
Chittaranjan Pradhan
RDBMS
Client/Server
Database
Table Creation
• Two versions of SQL*PLUS Viewing Table
Structure
• Graphical version
Record Insertion
• Start->All Programs->Oracle Database 10g Express Inserting Time
Edition->Go to Database home page(SQL->SQL Commands) Querying Data
• Command-Line version
• Start->All Programs->Oracle Database 10g Express
Edition->Run SQL Command Line(Connect)
1.10
Introduction To SQL
Naming Conventions
Chittaranjan Pradhan
RDBMS
Client/Server
Database
• When you create a table, you must specify the table name, SQL*PLUS
Naming Conventions
name of each column, data type of each column, and size Data Types
of each column Table Creation
1.11
Introduction To SQL
Data Types
Chittaranjan Pradhan
RDBMS
Data type specifies the type of data that will be stored in the
Client/Server
column. Data types also help to optimize storage space Database
SQL
• CHAR(n) SQL Statements
SQL*PLUS
• Stores fixed-length alphanumeric data in a column Naming Conventions
• Default and minimum size is one character Data Types
• Maximum allowable size is 2000 characters (previously 255) Table Creation
• If a string of a smaller length is stored, it is padded with Viewing Table
Structure
spaces at the end
Record Insertion
Inserting Time
• VARCHAR(n)/VARCHAR2(n) Querying Data
• Stores variable-length alphanumeric data in a column
• Default and minimum size is one character
• Maximum allowable size is 4000 characters (previously
2000)
• If the data are smaller than the specified size, only the data
value is stored; no padding is done
1.12
Introduction To SQL
Data Types...
Chittaranjan Pradhan
RDBMS
• DATE
Client/Server
• Stores date and time values Database
Table Creation
Querying Data
number; scale is the total number of digits to the right of the
decimal point(if used). The precision can range from 1 to 38
• If neither precision nor scale is specified, any number may
be stored up to a precision of 38 digits
• INTEGER
• Stores integer number
1.13
Introduction To SQL
Data Types...
Chittaranjan Pradhan
• NUMERIC(p,d)
RDBMS
• Stores fixed-point number with user specified precision Client/Server
Database
• Similar to NUMBER data type
SQL
SQL Statements
• LONG SQL*PLUS
Naming Conventions
• Stores variable length character strings containing up to
Data Types
2GB Table Creation
• Similar to VARCHAR
Viewing Table
• There can be one LONG data type per table Structure
Record Insertion
Querying Data
• Stores binary data such as digitized picture or image
• Maximum allowable size is 2000 Bytes (previously 255
Bytes)
• LONG RAW
• It is the higher range of RAW
• There can be one LONG data type per table
• Maximum allowable size id 2GB
1.14
Introduction To SQL
Data Types...
Chittaranjan Pradhan
RDBMS
Client/Server
Database
SQL
• LOB(Large Object) SQL Statements
SQL*PLUS
• Stores large volume of data Naming Conventions
• BLOB Data Types
• Used for binary data such as graphics, video clips and audio Table Creation
files up to 4GB Viewing Table
• CLOB Structure
1.15
Introduction To SQL
Table Creation
Chittaranjan Pradhan
RDBMS
Client/Server
Database
CREATE TABLE statement SQL
SQL Statements
CREATE statement is used for table creation. The syntax is: SQL*PLUS
Naming Conventions
Data Types
CREATE TABLE table_name( column datatype, Table Creation
column datatype, ... column datatype); Viewing Table
Structure
For example, create a table for STUDENT ( Roll, Name, Record Insertion
Inserting Time
Gender, Age, CGPA) Querying Data
1.16
Introduction To SQL
Viewing Table Structure
Chittaranjan Pradhan
RDBMS
DESCRIBE statement
Client/Server
DESCRIBE statement is used for viewing table structure. The Database
SQL
syntax is: SQL Statements
SQL*PLUS
Naming Conventions
DESCRIBE table_name; or Data Types
DESC table_name; Table Creation
Viewing Table
For example: DESCRIBE STUDENT; Structure
Querying Data
Name Null? Type
Roll NUMBER(6)
Name VARCHAR2(20)
Gender CHAR(1)
Age NUMBER(3)
CGPA NUMBER(4,2)
1.17
Introduction To SQL
Record Insertion
Chittaranjan Pradhan
INSERT statement
INSERT statement is used to insert a new row/record into a RDBMS
SQL
INSERT INTO table_name (column1, column2,..) VALUES SQL Statements
SQL*PLUS
(value1, value2,..); Naming Conventions
Data Types
Table Creation
• Column names are optional
Viewing Table
• Numeric data is not enclosed within quotes; while Structure
Record Insertion
character and date values are enclosed within single Inserting Time
RDBMS
Substitution variables
Client/Server
Database
• Substitution variables enable you to create an interactive SQL
SQL script, which prompts you to enter a value for the SQL Statements
SQL*PLUS
substitution variable Naming Conventions
Data Types
• In command line version, & character is used before the
Table Creation
substitution variable in the query; whereas : character is Viewing Table
used in graphical version Structure
Record Insertion
• Substitution variables for character and date columns are Inserting Time
1.19
Introduction To SQL
Record Insertion...
Chittaranjan Pradhan
RDBMS
• INSERT INTO STUDENT(Roll, Name, Gender, Age, Client/Server
Database
CGPA) VALUES (705129, ’Uday’, ’M’, 19, 9.2);
SQL
SQL Statements
SQL*PLUS
• INSERT INTO STUDENT VALUES (705129, ’Uday’, ’M’, Naming Conventions
Table Creation
Viewing Table
• INSERT INTO STUDENT(Roll, Name, CGPA) VALUES Structure
Querying Data
1.20
Introduction To SQL
Record Insertion...
Chittaranjan Pradhan
Customized Prompts
RDBMS
• ACCEPT Roll PROMPT ’Please enter the Roll of Student:’ Naming Conventions
Data Types
• ACCEPT Name PROMPT ’Please enter the Name of
Table Creation
Student:’ Viewing Table
Structure
• ACCEPT Gender PROMPT ’Please enter the Gender of
Record Insertion
Student:’ Inserting Time
Querying Data
• ACCEPT Age PROMPT ’Please enter the Age of Student:’
• ACCEPT CGPA PROMPT ’Please enter the CGPA of
Student:’
INSERT INTO STUDENT VALUES (&Roll, ’&Name’,
’&Gender’, &Age, &CGPA);
RDBMS
Client/Server
Database
Data Types
INSERT INTO STUDENT(dob) VALUES (TO_DATE( Table Creation
’12-JAN-1990 10:34:45 P.M.’, ’DD-MON-YYYY HH:MI:SS Viewing Table
Structure
P.M.’));
Record Insertion
Inserting Time
• If only the date value is entered in a date-type column, the Querying Data
1.22
Introduction To SQL
Querying Data
Chittaranjan Pradhan
RDBMS
SELECT statement Client/Server
Database
SELECT statement is used to retrieve data from the underlying
SQL
table. The syntax is: SQL Statements
Data Types
If the user wants to see all the columns in a table, * can be Table Creation
Record Insertion
SELECT * FROM STUDENT; Inserting Time
Querying Data
Roll Name Gender Age CGPA
705129 Uday M 19 9.2
705170 Ram M 20
705171 Kim F 19 8.6
705172 Raji 20 7.5
1.23
Introduction To SQL
Querying Data...
Chittaranjan Pradhan
RDBMS
Client/Server
Database
SQL
SQL Statements
Data Types
Roll Name CGPA
Table Creation
705129 Uday 9.2 Viewing Table
705170 Ram Structure
Record Insertion
705171 Kim 8.6 Inserting Time
1.24