15
SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database. SQL is like plain English: easy to understand and to write. SQL statements into various categories, which are: •Data Definition Language •Data Manipulation Language •Data Control Language. •Transaction Control Language •Embedded SQL statements

SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Embed Size (px)

Citation preview

Page 1: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

SQL Fundamentals

 SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database. SQL is like plain English: easy to understand and to write. SQL statements into various categories, which are: •Data Definition Language•Data Manipulation Language•Data Control Language.•Transaction Control Language•Embedded SQL statements

Page 2: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Data Definition Language (DDL) statements:

 DDL statements are used to define, alter, or drop database objects. The following table gives an overview about usage of DDL statements in ORACLE (a commercially used database):

Page 3: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Data Manipulation Language (DML) statements

Once the tables have been created, the DML statements enable users to query or manipulate data in existing schemas objects. DML statements are normally the most frequently used commands. The following table gives an overview about the usage of DML statements in ORACLE.

Page 4: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database
Page 5: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Data Control Language (DCL) A privilege can either be granted to a User with the help of GRANT statement. The privileges assigned can be SELECT, ALTER, DELETE, EXECUTE, INSERT, INDEX etc. In addition to granting of privileges, you can also revoke it by using REVOKE command.  

Page 6: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Transaction Control Statements (TCL) statements: 

TCL statements manage the change made by DML statements, and group DML statements into transactions. The following table gives an overview about the usage of TCL statements in ORACLE.

Page 7: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Embedded SQL statements

The SQL statements used to incorporate DDL,DML and transaction control statements within the body of a procedural language program, are known as Embedded SQL statements. The following table gives an overview about the usage of Embedded SQL statements in ORACLE.

Page 8: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Oracle Data types

The information in a database is maintained in the form of tables and each table consists of rows and columns, which store data, and therefore this data must have some data type i.e the type of data, which is stored in a table.The different data types available in Oracle are:

Page 9: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database
Page 10: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

The most commonly used data types are:         char        varchar or varchar2        number        date        long        long raw/ raw

Page 11: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Let us now briefly describe these data types: Char(n) This data type is used to store character strings of fixed size. The size of the character string is determined by the numeric value of n. This data type can hold maximum of 255 characters. When Oracle stores data in a CHAR data type, it will pad the value stored in the column up to the length of the column as declared by the table with blanks.  For example: If data type of address field is mentioned as CHAR(40) and address information of a particular record complete in 20 characters, then remaining 20 characters space is padded with blank characters.  

Page 12: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Varchar(n) / Varchar2(n)  This data type is used to store variable length alphanumeric data. It can store maximum of 2000 characters. In case of varchar data type, Oracle will not store padded blank spaces if the value stored in a column defined is less than length of the column as declared by the table with data type VARCHAR2. For example: If data type of address field is mentioned as VARCHAR(40) and address information of a particular record complete in 20 characters, then remaining 20 characters space is not padded with blank characters and memory space of 20 characters is used for some other purposes and not wasted as padded with blank characters.

Page 13: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Number (p,s) This data type is used to store numbers fixed or floating point .The precision (p) determines the length of the data while (s), the scale, determines the number of places after the decimal. The NUMBER data type that is used to store number data can be specified either to store integers or decimals with the addition of a parenthetical precision indicator.  For example, if you had a column defined to be data type NUMBER(10,3), the number 546.3832 would be stored as 546.383, because after the decimal point we can store 3 digits. It can store a number of 10 digits including a decimal point for example a maximum number of 999999.999 can be stored with data type of NUMBER(10,3). 

Page 14: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Date This data type, stores date values in a special format internal to Oracle It offers a great deal of flexibility to users who want to perform date manipulation operations There are also numerous functions that handle date operations more complex than simple arithmetic. Another nice feature of Oracle’s method for date storage is that it is inherently millennium compliant. The default format in which date is stored is DD-MON-YY. If we want to store date in other format then we have to use the appropriate functions.

Page 15: SQL Fundamentals SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database

Long The developer can declare columns to be of LONG data type, which can stores upto 2 gigabytes of alphanumeric text data. The values of long data type cannot be indexed and normal characters functions such as SUBSTR cannot be applied to long values. Long Raw / Raw  It is useful to store graphics and sound files when used in conjunction with LONG to form the LONG RAW data type, which can accommodate up to 2 gigabytes of data.  Note: A table cannot contain more than one Long column. They cannot be indexed and no integrity constraints can be applied on them (except for NULL and NOT NULL constrain).