sql basics

40
Structured Query Language (SQL) Basics Genesis Omo 1

Upload: genesis-omo

Post on 11-Jun-2015

186 views

Category:

Documents


4 download

TRANSCRIPT

  • 1. Genesis Omo1

2. an acronym for Structured QueryLanguage forms the backbone of most moderndatabase systems the language of databases a standard programming language forquerying and modifying data andmanaging databases. a standard computer language foraccessing and manipulating databases.2 3. SQLallows you to access a database SQL is an ANSI standard computerlanguage SQL can execute queries against adatabase 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 3 4. The first version of SQL, initially called SEQUEL, wasdeveloped at IBM by Chamberlin and Boyce in the early1970s. later formally standardized by the American NationalStandards Institute (ANSI) in 1986. The first non-commercial non-SQL RDBMS, INGRES,was developed in 1974 at the U.C. Berkely. Ingresimplemented a query language known as QUEL, whichwas later supplanted in the marketplace by SQL. In the late 1970s, Relational Software, Inc. (now OracleCorp.) saw the potential of the concepts described byCodd, Chamberlin, and Boyce and developed their ownSQL-based RDBMS. 4 5. Statements which may have a persistent effect onschemas and data, or which may control transactions,program flow, connections, sessions, or diagnostics. Queries which retrieve data based on specific criteria. Expressions which can produce either scalar values ortables consisting of columns and rows of data. Predicates which specify conditions that can beevaluated to SQL three-valued logic (3VL).5 6. Clauses which are (in some cases optional) constituentcomponents of statements and queries. Whitespace is generally ignored in SQL statements andqueries, making it easier to format SQL code forreadability. SQL statements also include the semicolon (";")statement terminator. Though not required on everyplatform, it is defined as a standard part of the SQLgrammar. 6 7. A database most often contains one ormore tables. Each table is identified by a name (e.g."Customers" or "Orders").Tables contain records (rows) with data. 7 8. 8 9. Table: Enrol Class_Code Stu_Num Table: Enroll_Grade Student_Info Table: Class Stu_Num Class_Code Stu_LName Crs_Cde Stu_Fname Class_Section Class_Room Stu_MName Pro_Num Stu_Bdate Stu_Hrs Table: Course Stu_Class Crs_Code Dept_Code Stu_GPA Crs_Description Stu_Transfer Crs_Credit Dept_Code Stu_Phone9 10. With SQL, we can query a database and have a result set returned. SELECT LastName FROM Persons10 11. SQL(Structured Query Language) is asyntax for executing queries. SQL language also includes a syntax toupdate, insert, and delete records. 11 12. SELECT - extracts data from a database table UPDATE - updates data in a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into adatabase table 12 13. DataDefinition Language (DDL) part of SQLpermits database tables to be created or deleted. Define indexes (keys), specify links betweentables, and impose constraints betweendatabase tables.13 14. CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index14 15. SELECTstatement is used to select datafrom a table. The tabular result is stored in a result table(called the resultset). SyntaxSELECT column_name(s)FROM table_name 15 16. Toselect the content of columns named "LastName" and "FirstName", from the database table called "Persons", use a SELECT statement like this: SELECT LastName,FirstName FROM Persons16 17. 17 18. Toselect all columns from the "Persons" table, use a * symbol instead of column names, like this: SELECT * FROM Persons18 19. 19 20. TheDISTINCT keyword is used to returnonly distinct (different) values. The SELECT statement returnsinformation from table columns. But whatif we only want to select distinctelements? With SQL, all we need to do is to add aDISTINCT keyword to the SELECTstatement:Syntax :SELECT DISTINCT column_name(s)FROM table_name 20 21. Toselect ALL values from the column named "Company" we use a SELECT statement like this: SELECT Company FROM Orders21 22. Semicolon is the standard way to separateeach SQL statement in database systemsthat allow more than one SQL statement to be executed in the same callto the server.22 23. DISTINCT keyword is used to return onlydistinct (different) values. SELECT statement returns informationfrom table columns. But what if we onlywant to select distinct elements? Syntax SELECT DISTINCT column_name(s) FROM table_name23 24. Toselect ALL values from the column named "Company" we use a SELECT statement like this: SELECT Company FROM Orders24 25. 25 26. SELECT DISTINCT Company FROM Orders 26 27. 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 27 28. 28 29. Toselect only the persons living in the city "Sandnes", we add a WHERE clause to the SELECT statement: 29 30. 30 31. Note that we have used single quotesaround the conditional values in theexamples. SQL uses single quotes around text values(most database systems will also acceptdouble quotes). Numeric values should not be enclosed in quotes.31 32. Thisis correct: SELECT * FROM Persons WHEREFirstName=Tove This is wrong:SELECT * FROM Persons WHEREFirstName=Tove 32 33. This is correct:SELECT * FROM Persons WHEREYear>1965 Thisis wrong:SELECT * FROM Persons WHEREYear>196533 34. TheLIKE condition is used to specify a search for a pattern in a column. SyntaxSELECT column FROM tableWHERE column LIKE patternA "%" sign can be used to define wildcards(missing letters in the pattern) both beforeand after the pattern. 34 35. Thefollowing SQL statement will returnpersons with first names that start withan O:SELECT * FROM PersonsWHERE FirstName LIKE O% SELECT * FROM PersonsWHERE FirstName LIKE %a SELECT * FROM PersonsWHERE FirstName LIKE %la% 35 36. INSERTINTO statement is used to insert new rows into a table. Syntax INSERT INTO table_name VALUES (value1, value2,....)INSERT INTO table_name (column1,column2,...)VALUES (value1, value2,....) 36 37. This "Persons" table:37 38. INSERT INTO Persons VALUES (Hetland, Camilla,Hagabakka 24, Sandnes)38 39. Insert Data in Specified ColumnsThis "Persons" table: 39 40. Tables: Customer Invoice Line ProductsIdentify the different fields pertable Inv_Number: inv,line Line_Price: line Cus_Code: cus,inv Inv_Date: inv Prod_Code: prod,line Cus_Lname: cus Prod_Descript: prod Cus_Fname: cus Prod_Price: prod Cus_Initial: cus Prod_On_Hand: prod Cus_Areacode: cus Cus_Phone: cus Vend_Code:prod Line_Units: line, prod40