
1 INTRODUCTION TO STRUCTURED QUERY LANGUAGE(SQL) SQL often referred to as Structured Query Language, is a database computer language designed for managing data in relational database management systems (RDBMS), and originally based upon relational algebra and calculus. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control. SQL was one of the first commercial languages for Edgar F. Codd's relational model, as described in his influential 1970 paper, "A Relational Model of Data for Large Shared Data Banks". Despite not adhering to the relational model as described by Codd, it became the most widely used database language. LANGUAGE ELEMENTS The SQL language is sub-divided into several language elements, including: Clauses, which are constituent components of statements and queries. (In some cases, these are optional.) Expressions, which can produce either scalar values or tables consisting of columns and rows of data. Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow. Queries, which retrieve the data based on specific criteria. This is the most important element of SQL. Statements, which may have a persistent effect on schemata and data, or which may control transactions, program flow, connections, sessions, or diagnostics. o SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar. Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability. Data types Each column in an SQL table declares the type(s) that column may contain. ANSI SQL includes the following datatypes. Character strings CHARACTER(n) or CHAR(n) — fixed-width n-character string, padded with spaces as needed CHARACTER VARYING(n) or VARCHAR(n) — variable-width string with a maximum size of n characters NATIONAL CHARACTER(n) or NCHAR(n) — fixed width string supporting an international character set NATIONAL CHARACTER VARYING(n) or NVARCHAR(n) — variable-width NCHAR string DBMS MANUAL DHANEKULA INSTITUTE OF ENGINEERING &TECHNOLOGY 2 Bit strings BIT(n) — an array of n bits BIT VARYING(n) — an array of up to n bits Numbers INTEGER and SMALLINT FLOAT, REAL and DOUBLE PRECISION NUMERIC(precision, scale) or DECIMAL(precision, scale) SQL provides a function to round numerics or dates, called TRUNC (in Informix, DB2, PostgreSQL, Oracle and MySQL) or ROUND (in Informix, Sybase, Oracle, PostgreSQL and Microsoft SQL Server)[19] Date and time DATE — for date values (e.g., 2010-05-30) TIME — for time values (e.g., 14:55:37). The granularity of the time value is usually a tick (100 nanoseconds). TIME WITH TIME ZONE or TIMESTAMP — the same as TIME, but including details about the time zone in question. TIMESTAMP — This is a DATE and a TIME put together in one variable (e.g., 2010- 05-30 14:55:37). TIMESTAMP WITH TIME ZONE or TIMESTAMPTZ — the same as TIMESTAMP, but including details about the time zone in question. Different types of commands in SQL: A).DDL commands: - To create a database objects B).DML commands: - To manipulate data of a database objects C).DQL command: - To retrieve the data from a database. D).DCL/DTL commands: - To control the data of a database… THE ORACLE TABLE – DUAL DUAL is the table owned by SYS.SYS owns the data dictionary and DUAL is a part of the data dictionary.DUAL is a small oracle worktable, which consists of only one row and one column, and contains the value x in that column .Besides arithmetic calculations it also supports date retrieval and it’s formatting DBMS MANUAL DHANEKULA INSTITUTE OF ENGINEERING &TECHNOLOGY 3 ORACLE FUNCTIONS Oracle functions serve the purpose of manipulating data items and returning a result Functions are also capable of accepting user supplied variables or constants and operating on them Such variables or constants are called arguments. Any number of arguments (or no arguments at all) can be passed to a function in the following format: Function_name(argument1,argument2,………..) Oracle Functions can be clubbed together depending upon whether they operate on a single row or a group of rows retrieved from a table.Accordingly, functions an be classified as follows: Group Functions(Aggregate Functions) Functions that act on a set of values are called Group functions.For example, SUM is a function which calculates the total set of numbers.A group function returns a single result row for a group of queried rows. Scalar Functions(Single Row Functions) Functions that act on only one value at a time are called Scalar functions.For example ,Length relates to the String Data type .Functions can be classified corresponding to different data types as: String Functions : For String data type Numeric Functions : For Number data type Conversion Functions : For Conversion data type to another Date Functions : For Date data type Additional Exercise-I Queries to facilitate acquaintance of Built-In Functions : Aggregate functions AGGREGATE FUNCTIONS 1. AVG:Returns an average value of ‘n’, ignoring null values in a column. Syntax: AVG ([<DISTINCT>| <ALL>] <n>) Example: SELECT AVG(CURBAL) “Average balance” FROM ACCT_MSTR; 2.MIN:Returns a minimum value of expr Syntax: MIN([<DISTINCT>| <ALL>] <expr>) Example : SELECT MIN(CURBAL) “Minimum balance” FROM ACCT_MSTR; 3.MAX:Returns a maximum value of expr. Syntax: MAX([<DISTINCT>| <ALL>] <expr>) DBMS MANUAL DHANEKULA INSTITUTE OF ENGINEERING &TECHNOLOGY 4 Example: SELECT MAX(CURBAL) “Maximum balance” FROM ACCT_MSTR; 4.COUNT: Returns the number of rows where expr is not null. Syntax: COUNT ([<DISTINCT>| <ALL>] <expr>) Example : SELECT COUNT(ACCT_NO) “No of accounts” FROM ACCT_MSTR; 5.COUNT(*):Returns the number of rows in the table , including duplicates and those with nulls. Syntax: COUNT(*) Example: SELECT COUNT(*) “No of records” FROM ACCT_MSTR; 6. SUM:Returns the sum of the values of ‘n’. Syntax: SUM([<DISTINCT>| <ALL>] <n>) Example: SELECT SUM(CURBAL) “Total Balance” FROM ACCT_MSTR; DBMS MANUAL DHANEKULA INSTITUTE OF ENGINEERING &TECHNOLOGY 5 Exercise-I Queries to facilitate acquaintance of Built-In Functions, String Functions, Numeric Functions, Date Functions and Conversion Functions. A.NUMERIC FUNCTIONS 1.ABS:Returns the absolute value of ‘n’. Syntax: ABS(n) Example : SELECT ABS(-15) “Absolute” FROM DUAL; 2.POWER:Returns m raised to the n th power .n must be an integer, else an error is returned. Syntax: POWER(m,n) Example: SELECT POWER(3,2) “Raised” FROM DUAL; 3.ROUND:Returns n, rounded to m places to the right of a decimal point. If m is omitted, n is rounded to 0 places.m can be negative to round off digits to the left of the decimal point.m must be an integer. Syntax:ROUND(n[,m]) Example: SELECT ROUND(15.19,1) “ROUND” FROM DUAL; 4.SQRT:Returns square root of n.If n<0, NULL.SQL reurns a real result. Syntax: SQRT(n) Example : SELECT SQRT(25) “Square Root” FROM DUAL; 5.EXP:Returns e raised to the n th power, where e=2.71828183. Syntax: EXP(n) Example SELECT EXP(5) “Exponent” FROM DUAL; 6.GREATEST : Returns the greatest value in a list of expressions. Syntax: DBMS MANUAL DHANEKULA INSTITUTE OF ENGINEERING &TECHNOLOGY 6 GREATEST(expr1,expr2,………….,expr_n) Where the expr1,expr2,………….,expr_n are expressions that are evaluated by the greatest function. Example SELECT GREATEST (5,6,17) “num” FROM DUAL; 7.LEAST : Returns the LEAST value in a list of expressions. Syntax: LEAST(expr1,expr2,………….,expr_n) Where the expr1,expr2,………….,expr_n are expressions that are evaluated by the LEAST function. Example: SELECT LEAST (5,6,17) “num” FROM DUAL; 8.MOD:Returns the remainder of the first number divided by the second number passed a parameter . If the second number is zero , the result is the same as the first number. Syntax: MOD(m,n) Example : SELECT MOD(15,7) “RESULT” FROM DUAL; 9.FLOOR:Returns the largest integer value that is equal to or less than a number. Syntax: FLOOR(n) Example: SELECT FLOOR(24.8) “FLOOR” FROM DUAL; 10. CEIL: Returns the SMALLEST integer value that is equal to or greater than a number. Syntax: CEIL(n) Example: SELECT CEIL(24.8) “CEIL” FROM DUAL; STRING FUNCTIONS 1.LOWER:Returns char, with all letters in lowercase. Syntax: LOWER(char) Example:SELECT LOWER (‘DHANEKULA’) “Lower” FROM DUAL; 2.UPPER:Returns char, with all letters in uppercase. DBMS MANUAL DHANEKULA INSTITUTE OF ENGINEERING &TECHNOLOGY 7 Syntax:UPPER(char) Example:SELECT UPPER (‘dhanekula’) “Upper” FROM DUAL; 3.SUBSTR:Returns a portion of characters, beginning at character m, and going upto character n. If n is omitted, the result returned is upto the last character in the string.The first position of the char is 1. Syntax: SUBSTR(<string>,<start_position>,[<length>]) Where < string> is the source string. <Start_position> is the position for extraction. The first position in the string is always 1. <Length> is the number of characters to extract. Example SELECT SUBSTR(‘SECURE’,3,4) “Substring” FROM DUAL; 4. INSTR:Returns the location of a substring in a string. Syntax: INSTR(<string1>,(<string2>, [<start_position>],[<n th_appearance>]) Where , <string1> is the string to search. <String2> is the substring to search for in string1. <Start_position> is the position in string1 where the search will start. If omitted , it defaults to 1. The first position in the string is 1. nth_appearance is the nth appearance of string2 .If omitted, it defaults to 1. Example: SELECT INSTR(‘SCT on the net’,’t’) “instr” FROM DUAL; 5. LENGTH:Returns the length of a word. Syntax: LENGTH(word) Example: SELECT LENGTH(‘DHANEKULA’) “Length” FROM DUAL; 6. LTRIM:Removes characters from the left of char with initial characters removed upto the first character not in set.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages65 Page
-
File Size-