SQLHistoryDefinitions and Data TypesCommon Data TypesCreating TablesCreating a TableOther FunctionsInsertionUpdatingDeletingDropOrderingSlide 13AggregatesGroupingGrouping ContSlide 17Nested QueriesSQLSQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against a database 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 databaseHistoryWas designed and implemented by IBM Research (1986)A joint with American National standards Institute (ANSI) and International Standards Organization (ISO) led to the standard version of SQL-86A revised and expanded in 1992 called SQL-92.Most recent is now SQL-99Definitions and Data TypesUses terms table, rows, columns for the formal relational model terms relations, tuple, and attribute.An SQL schema is identified by a schema name, including authorization identifier to indicate user who owns it and descriptions for each element.Schema elements include tables, constraints, views, domains, and other constructsCatalog – a named collection of schemas in an SQL environmentCommon Data Typeschar (size) – Fixed length character string. Size is specified in parenthesis. Max 255 bytes.varchar (size) – Variable-length character string. Max size is specified in parenthesis.number (size) – Number value with a max number of column digits specified in parenthesis. date – Date valuenumber (size, d) – Number value with a max number of digits of “size” total, with a max number of “d” digits to the right of the decimal.Creating TablesCreate Table – used to specify a new relation by giving it a name, and attributes with initial constraints.–Example: CREATE TABLE company.employee …Company is the schema nameEmployee is the relation nameCreating a TableCreate table myemployees(firstname varchar(30), lastname varchar(30), title varchar (30), age number(2), salary number(8, 2));Creates a table called myemployeesFirst name, last name, and title allows 30 characters each.Age is allowed 2 digitsSalary is allowed 8 digits with 2 decimalsOther FunctionsSelect – allows you to select a certain and retrieve data specified.–Example: Select “column1” From “TableName”Where “condition”;Select column 1 from the TableName with the following condition.InsertionInsert into “tablename”(first_column, … last_column)values (first_value,…last_value);Insert into employee(first, last, age, address, city, state)values (‘James’, ‘Tran’, 23, ‘1111 1st street’, ‘San Jose’, ‘California’);Inserts into specified table nameSpecify all columns inserting to separated by a comma.Values inserted are specified afterwardsStrings enclosed in single quotes, numbers are not.Updatingupdate “tablename” set “columnname” = “newvalue”[,”nextcolumn” = “newvalue2”…]where “columnname”OPERATOR “value”[and | or “column”OPERATOR “value”];**[ ] = optionalUpdate the specified table nameSet the column specified with new valueAdd in conditionals if neededOptional values and input add [ ]Deletingdelete from “tablename”where “columnname”OPERATOR “value”[and | or “column”OPERATOR “value”];[ ] = optionalDelete a certain table, column, row, etc.Operator meaning >,<,=, etc…DropDropping a table removes all rows and definitions.Example: Drop table “TableName”OrderingSQL allows you to order elements in a table.Example: orders by alphabeticalselect distinct customer-namefrom borrower, loanwhere borrower.loan-number = loan.loan-number and branch-name = ‘Perryridge’order by customer-nameEvaluation of GroupBy with HavingAggregatesMore functions that allow you to operate on sets.–COUNT, SUM, AVG, MAX, MINProduces numbers, not tables.Not part of relational algebra–Example: Select MAX (Age)From Employee EGroupingSometimes we want to apply aggregate functions to subgroups of tuples in a relation.Such as finding the average salary of employees in each department or the number of employees that are working on each project.SQL has a GROUP BY clause that allows for specific grouping of attributesGrouping ContExample:–SELECT Dno, COUNT(*), AVG (Salary)FROM EMPLOYEEGROUP BY Dno;–The EMPLOYEE tuples are partitioned into groups, each group having the same value for the grouping attribute Dno.–Then the COUNT and AVG functions are applied to each group.AA1A2A4A5AA1A2A3A4AA1A1A2A2A2A3A4A5Two tablesa)b)R(A) UNION ALL S(A)c)d)AA2A3AA2A3R(A) EXCEPT ALL S(A)R(A) INTERSECT S(A)SQL Multiset OperationsSRTTTNested QueriesList all courses that were not taught in S2000SELECT C.CrsNameFROM Course CWHERE C.CrsCode NOT IN (SELECT T.CrsCode --subquery FROM Teaching T WHERE T.Sem = ‘S2000’)Evaluation strategy: subquery evaluated once toproduces set of courses taught in S2000. Each row(as C) tested against this
View Full Document