Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL.

Similar presentations


Presentation on theme: "SQL."— Presentation transcript:

1 SQL

2 Introduction to SQL What is SQL?
When a user wants to get some information from a database file, he can issue a query. A query is a user–request to retrieve data or information with a certain condition. SQL is a query language that allows user to specify the conditions. (instead of algorithms)

3 Introduction to SQL Concept of SQL
The user specifies a certain condition. The program will go through all the records in the database file and select those records that satisfy the condition.(searching). Statistical information of the data. The result of the query will then be stored in form of a table.

4 Create table command This command is use for creating a table.
Rules for creating tables A name can have maximum upto 30 characters. A-Z , a-z , 0-9 allowed A name should be begin with an alphabet The use of special character like _ allowed and also recommaended SQL reversed words not allowed.(ex. create , select and so on.)

5 Create new table CREATE TABLE TABLENAME (<ATTRIBBUTES> <DATATYPE> <SIZE>); EX: create table persons(P_ID number(10),LASTNAME varchar2(20),FIRSTNAME varchar2(20),ADDRESS varchar2(20),CITY varchar2(15)); Table Created. Select * from persons; P_ID LASTNAME FIRSTNAME ADDRESS CITY

6 Now Display all the table
Query : Select * from tab; Display list of tables. Displaying Table Structure Query : DESC PRSONS;

7 INSERT DATA INTO TABLE SYNTAX :
INSERT INTO table_name VALUES (value1, value2, value3,...); Now we want to insert values in table persons QUERY INSERT INTO persons values(1,’SHAH’,’CHINTAN’,’SATELLITE’,’AHMEDABAD’); 1 row created

8 INSERT multiple rows in table
QUERY INSERT INTO persons values(&ID,’&LASTNAME,’&FIRSTNAME’,’&ADDRESS’,’&CITY’); Enter value for ID : 1 . 1 row created Press / and enter

9 VIEWING DATA IN THE TABLES
SELECT * FROM PERSONS;

10 Filtering Table Data Selected Columns And All Rows Syntax :
SELECT <columnname1>,<columnname2> FROM Table Name ; Example : SELECT P_ID , LASTNAME FROM PERSONS; P_ID LASTNAME 1 SHAH 2 PATEL 3 PANDYA 4

11 Filtering Table Data Selected Rows and All Columns Syntax :
SELECT * FROM <Table Name> WHERE <Condition> ; Example : SELECT * FROM PERSONS WHERE NAME=‘NITIN’; P_ID LASTNAME FIRSTNAME ADDRESS CITY 3 Nitin Pandya Gandhinagar

12 Distinct Eliminating duplicate rows
-The DISTINCT clause removing duplicates from the result set. -The DISTINCT clause can only used with select statements. -SELECT DISTINCT Syntax SELECT DISTINCT <COLUMNNAME1> FROM <TABLENAME>;

13 We use the following SELECT Statement
We use the following SELECT Statement. SELECT DISTINCT city FROM persons; P_ID LASTNAME FIRSTNAME ADDRESS CITY 1 Chintan Shah Satellite Ahmedabad 2 Sanjay Patel Gandhinagar 3 Nitin Pandya CITY Ahmedabad Gandhinagar

14 SORTING DATA IN A TABLE Orcle allows data from a table to be viewed in a sorted orders. The rows retrieved from the table will be sorted in either ascending or descending order. The ORDER BY keyword is used to sort the result-set by specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.

15 SQL ORDER BY Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC
P_ID LASTNAME FIRSTNAME ADDRESS CITY 1 SHAH CHINTAN SATELLITE AHMEDABAD 2 PATEL SANJAY GANDHINAGAR 3 PANDYA NITIN TALOD 4 VISHAL GHATLODIA

16 We use the following SELECT statement: SELECT
We use the following SELECT statement: SELECT * FROM persons ORDER BY LASTNAME; P_ID LASTNAME FIRSTNAME ADDRESS CITY 3 PANDYA NITIN TALOD GANDHINAGAR 2 PATEL SANJAY 4 VISHAL GHATLODIA AHMEDABAD 1 SHAH CHINTAN SATELLITE

17 ORDER BY DESC example SELECT * FROM persons ORDER BY LASTNAME DESC;
P-ID LASTNAME FIRSTNAME ADDRESS CITY 1 SHAH CHINTAN SATELLITE AHMEDABAD 4 PATEL VISHAL GHATLODIA 2 SANJAY GANDHINAGAR 3 PANDYA NITIN TALOD

18 DELETE Statement Removal of all ROWS -Syntax DELETE FROM PERSONS;
-All rows deleted

19 Question: - If I want to retrieve data of persons table.
What will happen? What will be an answer?

20 UPDATING CONTENS OF A TABLE
The UPDATE command is used to change or modify data values in a table. SQL UPDATE Syntax UPDATING ALL ROWS UPDATE <TABLENAME> SET <COLUMNNAME1>=<NEW VALUE> , <COLUMNNAME2>=<NEW VALUE>;

21 Example Now we want to UPDATE LASTNAME in persons table UPDATE persons
SET LASTNAME=‘ABC’;

22 -Now, what will be effect? -How looks table(persons)
P_ID LASTNAME FIRSTNAME ADDRESS CITY 1 ABC CHINTAN SATELLITE AHMEDABAD 2 SANJAY GANDHINAGAR 3 NITIN TALOD 4 VISHAL GHATLODIA UPDATE records Conditionally UPDATE table_name SET columnname=value, columnname=value2 WHERE columnname=value;

23 Now we want to UPDATE CITY from Ahmedabd to Bombay, whose FIRSTNAME IS CHINTAN
UPDATE Persons SET CITY=‘BOMBAY’ WHERE FIRSTNAME=‘CHINTAN’; What will be result and how looks table?

24 -What will be result and how looks table?
P_ID LASTNAME FIRSTNAME ADDRESS CITY 1 SHAH CHINTAN SATELLITE BOMBAY 2 PATEL SANJAY GANDHINAGAR 3 PANDYA NITIN TALOD 4 VISHAL GHATLODIA AHMEDABAD

25 MODIFY STRUCTURE OF TABLE
ADDING new columns in existing table. Syntax ALTER TABLE <TABLENAME> ADD(<NEW COLUMNNAME><DATATYPE>(<SIZE>));

26 Now we want to ADD PINCODE fields in PERSONS table
Query ALTER TABLE persons ADD(PINCODE NUMBER(6)); OUTPUT: - P_ID LASTNAME FIRSTNAME ADDRESS CITY PINCODE 1 SHAH CHINTAN SATELLITE AHMEDABAD 2 PATEL SANJAY GANDHINAGAR 3 PANDYA NITIN TALOD 4 VISHAL GHATLODIA

27 EXERCISE TIME NOW INSERT/UPDATE PINCODE IN PERSONS TABLE.
Your table looks like this. P_ID LASTNAME FIRSTNAME ADDRESS CITY PINCODE 1 SHAH CHINTAN SATELLITE AHMEDABAD 380015 2 PATEL SANJAY GANDHINAGAR 382051 3 PANDYA NITIN TALOD 382055 4 VISHAL GHATLODIA 380009

28 MODIFY STRUCTURE OF TABLE
MODIFY columns in existing table. Syntax ALTER TABLE <TABLENAME> MODIFY(<COLUMNNAME><NEWDATATYPE> (<NEWSIZE>));

29 MODIFY(PINCODE VARCHAR2(10));
Now we want to MODIFY PINCODE data type from NUMBER TO VARCHAR2 in PERSONS table. Query ALTER TABLE persons MODIFY(PINCODE VARCHAR2(10)); OUTPUT: - TABLE ALTERED.

30 RENAMING TABLES SYNTAX :
RENAME <TableName> TO <NewTableName> EXAMPLE : RENAME PERSONS TO CLIENTS; TABLE RENAMED

31 TRUNCATING TABLES DESTROYING TABLES EXAMPLE : TRUNCATE TABLE PERSONS;
Table Truncatd. DESTROYING TABLES EXAMPLE: DROP TABLE PERSONS; Table Dropped.


Download ppt "SQL."

Similar presentations


Ads by Google