Вы находитесь на странице: 1из 3

Structured Query Language(SQL) What is SQL?

SQL is non Procedural Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can retrieve data from a database SQL can insert new records in a database SQL can delete records from a database SQL can update records in a database SQL is easy to learn SQL Data Definition Language (DDL) The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are: 1. CREATE TABLE - creates a new database table 2. ALTER TABLE - alters (changes) a database table 3. DROP TABLE- deletes a database table 4. CREATE INDEX - creates an index (search key) 5. DROP INDEX - deletes an index SQL Data Manipulation Language (DML) SQL (Structured Query Language) is syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. These query and update commands together form the Data Manipulation Language (DML) part of SQL: 1. SELECT - extracts data from a database table 2. UPDATE - updates data in a database table 3. DELETE - deletes data from a database table 4. INSERT INTO - inserts new data into a database table SQL-Transaction Statements -- control transactions 1. COMMIT Statement -- commit the current transaction 2. ROLLBACK Statement -- roll back the current transaction Table and other Related Terms: 1. 2. 3. 4. 5. A table is uniquely identified by its name and consists of rows that contain the stored information, each row containing exactly one tuple (or record ). A table can have one or more columns. A column is made up of a column name and a data type, and it describes an attribute of the tuples. The structure of a table, also called relation schema, thus is defined by its attributes. The type of information to be stored in a table is defined by the data types of the attributes at table creation time.

What are the different Data Types? char(n): Fixed-length character data (string), n characters long. The maximum size for n is 255 bytes (2000 in Oracle8). Note that a string of type char is always padded on right with blanks to full length of n. (can be memory consuming). Example: char(40) varchar2(n): Variable-length character string. The maximum size for n is 2000 (4000 in Oracle8). Only the bytes used for a string require storage. Example: varchar2(80) number(m, n): Numeric data type for integers and reals. m = overall number of digits, n = number of digits to the right of the decimal point. date: Date data type for storing date and time. The default format for a date is: DD-MMM-YY. Examples: 13-OCT-94, 07-JAN-98 long: Character data up to a length of 2GB. Only one long column is allowed per table. How to create table in SQL? Creating Table the SQL command for creating an empty table has the following form: create table <table> (<column 1> <data type> [not null] [unique] [<column constraint>], . . . . . . . . .<column n> <data type> [not null] [unique] [<column constraint>], [<table constraint(s)>] ); Example: Create table student(Htno varchar2(10) constraint pk primar key, name varchar2(50), Branch char(5)); What is a Constraint? The definition of a table may include the specification of integrity constraints. Basically twotypes of constraints are provided: column constraints are associated with a single column whereas table constraints are typically associated with more than one column. However, any column constraint can also be formulated as a table constraint.

The specification of a (simple) constraint has the following form: [constraint <name>] primary key | unique | not null How to insert data into a Table? insert into <tablename> values(value1, value2, ...) Example: insert into student values (01501A1210, Ravi Kumar, CSE); Insert Data in Specified Columns This "Student" table: Htno 01501A1210 01501A1211

Name Ravi Ramesh

Branch CSIT CSIT

This SQL statement: INSERT INTO Persons (Htno, Name) VALUES ('01501A1213', After executing the above statement the result is as follows: Htno Name 01501A1210 Ravi 01501A1211 Ramesh 01501A1213 Prasad SELECT Statement: SELECT <columnname>(s) FROM <tablename>;

'Prasad'); Branch CSIT CSIT

To select the content of columns named "LastName" and "FirstName", from the database table called "Persons", use a SELECT statement like this: SELECT LastName, FirstName FROM Persons; How to Select All Columns? To select all columns from the "Persons" table, use a * symbol instead of column names, like this: Example: select * from employee; SELECT DISTINCT Statement The DISTINCT keyword is used to return only distinct (different) values. The SELECT statement returns information from table columns. But what if we only want to select distinct elements? With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement: Syntax: SELECT DISTINCT <columnname> (s) FROM <tablename> WHERE Clause: The WHERE clause used to specify selection criteria, To conditionally select data from a table, a WHERE clause can be added to the SELECT statement. Syntax: SELECT column FROM table WHERE column operator value Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern Example: Select * from Student where Htno=01501A1210; The LIKE Condition The LIKE condition is used to specify a search for a pattern in a column. Syntax: SELECT column FROM table WHERE column LIKE pattern The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern. The _ is used a replacement for single character. Example: select Htno, name from student where name like S% (This query returns all the records in which name is starting with character S) The Update Statement The UPDATE statement is used to modify the data in a table. Syntax: UPDATE <tablename> SET <columnname> = new_value WHERE <columnname> = some_value; Example: Update one Column in a Row We want to add a name to the person with a Htno of 01501A1210; Update student set name=Ramesh where Htno= 01501A1210;

DELETE Statement: The DELETE statement is used to delete rows in a table. Syntax: DELETE FROM <tablename> WHERE <columnname> = <somevalue> Sort the Rows ORDER BY clause is used to sort the rows. Emp_Name Ravi Ramesh Kishore Prasad Associate_ID 3010 8010 1241 3456

Example To display the Emp_Name in alphabetical order: SELECT Emp_Name, Associate_ID FROM Associate ORDER BY Emp_name; AND & OR AND and OR joins two or more conditions in a WHERE clause. The AND operator displays a row if ALL conditions listed are true. The OR operator displays a row if ANY of the conditions listed are true. SELECT * FROM Associate WHERE Emp_name='Kishore' AND Associate_ID='1241'; Exercise: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

What can SQL do? Mention any three DDL Statements? Mention any three DML Statements? What is the syntax of creating a table? What is the syntax of insert statement in SQL? What is the difference between char(n) and varchar2(n)? How to insert data into the specified columns? What does ORDER BY will do? How to use where condition? Which purpose does LIKE used? How to use AND in where clause? How to use OR in where clause?

Вам также может понравиться