310 likes | 322 Views
Learn the basics of SQL query language, including creating tables, inserting data, filtering and sorting data, updating and modifying table structure.
E N D
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)
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.
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.)
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;
Now Display all the table • Query : Select * from tab; Display list of tables. Displaying Table Structure • Query : DESC PRSONS;
INSERT DATA INTO TABLE • SYNTAX : INSERT INTO table_nameVALUES (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
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
VIEWING DATA IN THE TABLES • SELECT * FROM PERSONS;
Filtering Table Data • Selected Columns And All Rows • Syntax : SELECT <columnname1>,<columnname2> FROM Table Name ; • Example : SELECT P_ID , LASTNAME FROM PERSONS;
Filtering Table Data • Selected Rows and All Columns • Syntax : SELECT * FROM <Table Name> WHERE <Condition> ; • Example : SELECT * FROM PERSONS WHERE NAME=‘NITIN’;
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>;
We use the following SELECT Statement.SELECT DISTINCT city FROM persons;
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.
SQL ORDER BY SyntaxSELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC
We use the following SELECT statement:SELECT * FROM personsORDER BY LASTNAME;
ORDER BY DESC exampleSELECT * FROM persons ORDER BY LASTNAME DESC;
DELETE Statement • Removal of all ROWS -Syntax DELETE FROM PERSONS; -All rows deleted
Question: - • If I want to retrieve data of persons table. • What will happen? • What will be an answer?
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>;
Example • Now we want to UPDATE LASTNAME in persons table UPDATE persons SET LASTNAME=‘ABC’;
-Now, what will be effect?-How looks table(persons) UPDATE records Conditionally UPDATE table_nameSET columnname=value, columnname=value2WHERE columnname=value;
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?
MODIFY STRUCTURE OF TABLE • ADDING new columns in existing table. • Syntax ALTER TABLE <TABLENAME> ADD(<NEW COLUMNNAME><DATATYPE>(<SIZE>));
Now we want to ADD PINCODE fields in PERSONS table • Query ALTER TABLE persons ADD(PINCODE NUMBER(6)); OUTPUT: -
EXERCISE TIME • NOW INSERT/UPDATE PINCODE IN PERSONS TABLE. • Your table looks like this.
MODIFY STRUCTURE OF TABLE • MODIFY columns in existing table. • Syntax ALTER TABLE <TABLENAME> MODIFY(<COLUMNNAME><NEWDATATYPE> (<NEWSIZE>));
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.
RENAMING TABLES • SYNTAX : RENAME <TableName> TO <NewTableName> • EXAMPLE : RENAME PERSONS TO CLIENTS; TABLE RENAMED
TRUNCATING TABLES • EXAMPLE : TRUNCATE TABLE PERSONS; Table Truncatd. DESTROYING TABLES • EXAMPLE: • DROP TABLE PERSONS; • Table Dropped.