Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit-3 Interactive SQL.

Similar presentations


Presentation on theme: "Unit-3 Interactive SQL."— Presentation transcript:

1 Unit-3 Interactive SQL

2 Table: A table is a set of data elements (values) that is organized using a model of vertical columns and horizontal raws Entity information is stored in a table. Table is a two dimensional matrix that consists of rows and column. The table must have a unique name via which it can be referred to after its creation. Entity: A group of similar information or data which is of interest to an organization is called an Entity For Example: A client is a considered as an entity, information about the client entity can be stored in a client_mster table. Attribute: Each entity can have a number of characteristics, the characteristics of an entity are called as an attribute, and the value of these characteristics are called as an attribute value For Example: A client have characteristics like name, address, telephone number, fax number, balance, etc

3 Columns/Field: When entity information is stored in a table, the attributes associated with the entity are stored in a table columns/ table fields. Tuple /Record /Row: an organization will deal with many clients and the same information must be recorded for each client. Multiple fields placed in a horizontal plane, is called a Record or Row or Tuple. To Create a columns / field, to store, maintain and manipulate data, the engine requires a minimum of three parameters to be passed by the creator These parameters are column name, column size and column data type. These parameters are passed to the engine, via its natural language, SQL. There are several other parameters that a creator can pass to the engine at column creation time. Some of these parameters will place constraints on the data that a user can load into the column Constrains: Constraints enforce business rules in the database, in other words, they limit the acceptable data values for a table.

4 The Data Types: The various data types, recognized by the oracle engine and permitted to be used for creating an oracle table column are defined below: CHAR (size) This data type is used to store character string values of fixed length. The size in brackets determines the number of characters the cell can hold. The maximum number of characters this data type can hold is 255 characters ORACLE compares CHAR values using blank-padded comparison semantics. E.g. if a value that is inserted in a cell of CHAR data type is shorter that the size it is defined for then it will be padded with spaces on the right until it reaches the size characters in length VARCHAR (size) / VARCHAR2 (size) This data type is used to store variable length alphanumeric data. The maximum this data type can hold is 2000 characters Note:- One difference between this data type and CHAR data type is VARCHAR values using non-padded comparison semantics. Inserted values will not be padded with spaces. M

5 NUMBER (P, S). The NUMBER data type is used to store numbers
NUMBER (P, S) The NUMBER data type is used to store numbers. The precision (P) determine the maximum length of the data, whereas the scale (S) determine the number of places to the right of the decimal. If scale is omitted then the default is zero, if the precision is omitted values are stored with their original precision up to maximum. DATE This data type is used to represent date and time. The standard format is DD-MON-YY as in 7-DEC-09 To enter dates other than the standard format use the appropriate functions. Date Time stores date in the 24-hr format. By default, the time in a date field is 12:00:00 am, if no time portion is specified. The default date for a date filed is the first day of the current month LONG This data type is used to store variable length character strings containing up to 2GB LONG values cannot be indexed, and the normal character functions such as SUBSTR cannot be applied to LONG values.

6 RAW/ LONG RAW The RAW / LONG RAW data type can be used store binary data, such as digitized picture or image Raw data type ca have maximum length of 255 bytes. LONG RAW data type can contain up to 2GB Values stored in columns having LONG RAW data type cannot be indexed.

7 To Create Table:- syntax CREATE TABLE tablename
(Columnname datatype (size), columnname datatype(size)); Insertion of A Data Into Table:- syntax INSERT INTO tablename (columnname, columnname) VALUES (expression, expression); When inserting a single row of data into the table, the insert operation: - create a new row in the database table - Loads the values passed into all the columns specified.

8 Character expression must be enclosed in single quotes.(‘)
Note:- Character expression must be enclosed in single quotes.(‘) if there are exactly the same number of values as there are columns and the values are given in accordance with the way the columns were created. There is no need to indicate the column names in the SQL sentence. If there are less values being described than columns in the table then it is mandatory to indicate both the table column name and its corresponding value in the insert into SQL sentence. Viewing Data In The Tables:- Once data has been inserted into table, the next most logical operation would be to view what has been entered. The ‘SELECT’ SQL verb is used to achieve this.

9 In order to view global table data the syntax is:
1. SELECT (columnname1 …….. Columnname n) FROM tablename; 2. SELECT * FROM tablename; Here asterisk (*) mean all column in the table. Filtering Table Data While viewing data from a table it is rare that all the data from the table will be required each time. Hence, SQL must give us a method of filtering out data that is not required.

10 The ways of filtering table will be
- Selected columns and all rows - Selected rows and all columns - Selected columns and selected columns. Selected columns and al rows syntax: SELECT columnname, columnname FROM tablename; e.g. SELECT clientno, name FROM client_master; Selected rows and all columns If information of a particular client must be retrieved from the table, its retrieval must be based on a specific condition. Oracle provides the option of using a ‘where clause’ in a SQL sentence to apply a filter on the rows the select statement will retrieve.

11 When a ‘where clause’ is added to the SQL sentence, the oracle server compares each record from the table with the condition specified in the where clause. Oracle displays only those records that satisfy the specified condition. Syntax:- SELECT * FROM tablename WHERE search condition; Example:- SELECT * FROM client_master WHERE bal>0; Selected Columns And Selected Rows:- SELECT columnname, columnname FROM tablename

12 Elimination Of Duplicate From the Select statement:
A table could hold duplicate rows. In such a case, to see only unique rows the syntax is: Syntax:- SELECT DISTINCT columnname, columnname FROM tablename; The SELECT DISTINCT SQL syntax scans through the values of the column/s specified and display unique values from amongst them. Sorting data in a table:- Oracle allows data from table to be viewed in a sorted order. The rows retrieved from the table will be sorted I either ascending or descending order depending on the condition specified in the select sentence. SELECT * FROM ORDER BY columnname, columnname (sort order);

13 For viewing the data in descending sorted order the word ‘desc’ must be mentioned after the columnname and the before the semi colon in the order by clause. In case there is no mention of the sort order, the oracle engine sorts in the ascending order by default. Delete Operations:- The verb DELETE in SQL is used to remove rows from table. We can remove either all the rows from a table or a selected set of rows from table. To remove all rows syntax is: DELETE FROM tablename; To remove a specified row/s syntax is: DELETE FROM tablename WHERE search condition;

14 Updating The Contents Of A Table:-
The UPDATE command is used to change or modify data values in a table. We can update all the rows from a table or a select set of rows from a table. Updating of all rows syntax is:- UPDATE tablename SET columnname = expression, columnname = expression; Updating records conditionally syntax is:- SET columnname = expression, columnname = expression……. WHERE columnname = expression

15 Modify The Structure Of Table:-
For Adding New Columns Syntax is: ALTER TABLE tablename ADD (new columnname datatype(size) ………); For Modifying Columns Syntax is: MODIFY (columnname newdatatype (newsize)……..); Restriction on the ALTER TABLE: Using the ALTER TABLE clause the following tasks cannot be performed: - Change the name of the table - Change the name of the column -Drop a column - Decrease the size of a column if table data exists.

16 CREATING TABLE FROM ANOTHER TABLE
RENAMING TABLES To rename a table, the syntax is: RENAME old tablename TO new tablename DESTROYING TABLES TO destroy a table, the syntax is: DROP TABLE tablename; CREATING TABLE FROM ANOTHER TABLE Syntax: CREATE TABLE tablename (columnname, columnname) AS SELECT columnname, columnname FROM tablename;

17 Here source table is the table identified in the ‘SELECT’ section and the target table is one identified in the ‘CREATE’ section of the SQL sentence. If the source table is populated with records these will be uploaded in target table. The structure of the source table and target table is same. Finding Out The table/s created by a user: To determine which tables the user has access to the syntax is: SELECT * FROM tab; Finding Out the column detail of a table created: To find information about the columns defined in the table use the following syntax: DESCRIBE tablename;

18 Operator Arithmetic Operator Oracle allows arithmetic operators to be used while viewing records from a table or while performing data manipulations such as Insert, Update and Delete. These are + : Addition - : Subtraction / : Division * : Multiplication ** : Exponentiation () : Enclosed Operation For Example, Select product_no, description, sell_price*0.05 From product_master;

19 Renaming Columns Used With Expression Lists:
By default, the oracle engine will use column names of the table product_master as column headers when displaying column output on the VDU screen. Since there are no columns with the names sell_price*0.05 in the table product_master, the engine will perform the required operations and use each formula as the default column header when displaying outputs. Renaming the default output column nameswith an alias, when required. Syntax: SELECT columnname result_columnname, columnname result_columnname FROM tablename; For Example: Select product_no, description, sell_price*0.05 Increase From product_master;

20 Logical Operators: Logical operators that can be used in SQL sentences are:
The AND Operator: The oracle engine will process all rows in a table and display the result only when all of the conditions specified using the AND operator are ly when product satisfied. For Example, select emp_no, name from emp_master where salary>=10000 and salary<=50000 The OR Operator: The oracle engiine will process all rows in a table and display the result only when any of the conditions specified using the Or operators are satified. select client_no, name from client_master where pincode= OR pincode = ;

21 The NOT Operators The oracle engine will process all rows in a table and display the result only when none of the conditions specified using the Not operators re satisfied. For Example, SELECT client_no, name FROM client_master WHERE NOT (city = ‘mumbai’ or city = ‘delhi’);

22 Range Searching: In order to select data that is within a range of values, the BETWEEN operator is used. The BETWEEN operator allows the selection of rows that contain values within a specified lower and upper limit. The lower value must be coded first. The two values in between the range must be linked with the keyword AND. A BETWEEN operator can be used with both character and numeric data types but one cannot mix the data types e.g. the lower range of values from a character column and the other from a numeric column. For Example, SELECT emp_no, name from emp_master WHERE salary BETWEEN AND 50000; SELECT product_no, description FROM product_master WHERE profit NOT BETWEEN 10 AND 15;

23 Pattern Matching: The use of the LIKE predicate:
The comparison operators discussed so far have compared one value, exactly to one other value. Such precision may not always be desired necessary. For this purpose oracle provides the LIKE predicate. The LIKE predicate allows comparison of one string value with another string value, which is not identical. This achieved by using wildcard characters. Two characters that are available: For character data types: % allows to match any string of any length (include zero length) _ allows matching on a single character Tables: CUST_MSTR Columns: FNAME,LNAME,DOB_INC Technique: Operators: IN, Clauses: WHERE, Others: ALIAS Tables: CUST_MSTR Columns: FNAME,LNAME,DOB_INC Technique: Operators: BETWEEN, Clauses: WHERE, Others: ALIAS FNAME LNAME Birthday OCCUP

24 Example: List the customer whose being with the letters ‘Ch’
Synopsis: Solution: SELECT FNAME, LNAME, DOB_INC “BIRTHDATE”, OCCUP FROM CUST_MSTR WHERE FNAME LIKE ‘CH%’; Output: Chriselle Bayross OCT Service Chhaya Bankar OCT Service Explanation: In the above examples, all those records where the values held in the FNAME beings with Ch are displayed. The % indicates that any number of characters can follow the letters Ch. The IN and NOT IN predicates: The arithmetic operator (=) compares a single value to another single value. In case a value needs to be compared to a list of values then the IN predicate is used. The IN predicates helps reduce the need to use multiple OR conditions.

25 ----------------------------------------------------------
Example: List the customer details of the customers named Hansel, Mamta, Namita and Aruna. Synopsis: Solution: SELECT FNAME, LNAME, DOB_INC “birthday”, OCCUP FROM CUST_MSTR WHERE FNAME IN (‘Hansel’,.Mamta’,Namita’,’Aruna’); Output: FNAME LNAME Birthday OCCUP Mamta Muzumdar AUG Service Hansel Colaco JAN Service Namita Kanade JUN Self Employed Explanation: The above example, displays all those records where the FNAME filed holds any one of the four specified Values.


Download ppt "Unit-3 Interactive SQL."

Similar presentations


Ads by Google