DOC PREVIEW
SJSU CS 157A - SQL

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SQLHistoryDefinitions and Data TypesCommon Data TypesCreating TablesCreating a TableOther FunctionsInsertionUpdatingDeletingDropOrderingSlide 13AggregatesGroupingGrouping ContSlide 17Nested QueriesSQLSQL 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 databaseHistoryWas 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-86A revised and expanded in 1992 called SQL-92.Most recent is now SQL-99Definitions and Data TypesUses 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 constructsCatalog – a named collection of schemas in an SQL environmentCommon Data Typeschar (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 valuenumber (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 TablesCreate 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 nameEmployee 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 myemployeesFirst name, last name, and title allows 30 characters each.Age is allowed 2 digitsSalary is allowed 8 digits with 2 decimalsOther FunctionsSelect – 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 nameSpecify all columns inserting to separated by a comma.Values inserted are specified afterwardsStrings enclosed in single quotes, numbers are not.Updatingupdate “tablename” set “columnname” = “newvalue”[,”nextcolumn” = “newvalue2”…]where “columnname”OPERATOR “value”[and | or “column”OPERATOR “value”];**[ ] = optionalUpdate the specified table nameSet the column specified with new valueAdd in conditionals if neededOptional values and input add [ ]Deletingdelete from “tablename”where “columnname”OPERATOR “value”[and | or “column”OPERATOR “value”];[ ] = optionalDelete a certain table, column, row, etc.Operator meaning >,<,=, etc…DropDropping a table removes all rows and definitions.Example: Drop table “TableName”OrderingSQL 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 HavingAggregatesMore functions that allow you to operate on sets.–COUNT, SUM, AVG, MAX, MINProduces numbers, not tables.Not part of relational algebra–Example: Select MAX (Age)From Employee EGroupingSometimes 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 ContExample:–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 S2000SELECT C.CrsNameFROM Course CWHERE 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

SJSU CS 157A - SQL

Documents in this Course
Lecture

Lecture

44 pages

Chapter 1

Chapter 1

56 pages

E-R Model

E-R Model

16 pages

Lecture

Lecture

48 pages

SQL

SQL

15 pages

SQL

SQL

26 pages

Lossless

Lossless

26 pages

SQL

SQL

16 pages

Final 3

Final 3

90 pages

Lecture 3

Lecture 3

22 pages

SQL

SQL

25 pages

Load more
Download SQL
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view SQL and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view SQL 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?