inf 523 fundamentals of information technology: databases ... › ~gangolly ›...

51
Inf 523 Fundamentals of Information Technology: Databases (Fall 2011) Jagdish S. Gangolly Informatics CCI SUNY Albany September 16, 2011 Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (F

Upload: others

Post on 25-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Inf 523 Fundamentals of Information Technology:Databases (Fall 2011)

Jagdish S. GangollyInformatics

CCISUNY Albany

September 16, 2011

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Modeling

I Entity-Relationship Diagram (ERD)I Entity

I Weak entitiesI Strong entitiesI Dependent entities

I RelationshipI Degree of a relationshipI Cardinality of a relationshipI Recursive relationship

I AttributesI Identifier

I ERD with crows foot notation

I Uniform Modeling Language (UML)

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Entity-Relationship Diagrams: Entity

I EntityI Entity is a set that consists of a number of instances, each

named by using identifiers. An identifier of an entity may be asingle attribute or a set of attributes (composite identifier).

I Weak entities are entities that have no independent existenceof their own, but exist by borrowing their identities from otherentities (strong entities)

I Strong entities are entities that have an existence of their ownI If a weak entity also has its own identifying attribute(s) in

addition to the identifiers of other entities, it is called anID-Dependent entity

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Entity-Relationship Diagrams: Relationship

I Degree of a relationship is the number of entities participatingin that relationship. If two entities partipate in a relationship,it is called a binary relationship. If three entities participate, itis called a ternary relationship and so on. In general, if nentities participate in a relationship it is called an n-aryrelationship.

I Cardinality of a relationship is the number of entity instancesthat can occur in a relationship

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Modeling

I Maximum and Minimum Cardinality (use of hash signs andovals in ERDs

I To normalize, all N:N relationships are eliminating by replacingthem with an entity with 1:N relationships on either side

I Procedure for normalization with ERDs:I STEP 1: Draw ERDI STEP 2:If there are any relationships with degree greater than

two, convert such relationships into binary relationshipsI STEP 3: Replace all N:N relationships by entities (you can

name them by hyphenating the names of entities on eitherside) which have only 1:N relationships on either side, andinclude among the attributes os such entities the keys of theadjoining entities

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

ERD with Crow’s Foot Notation

I Relationships between strong entities are shown in dashedlines and are called non-identifying relationships since theentities do not borrow foreign keys to establish their identities

I Crows foot indicates ”many”, single hash mark indicates”one” (or ”mandatory”), circle indicates ”zero”

I The symbol closest to the entity indicates maximumcardinality, the other symbol indicates minimum cardinality

I The relationships between weak entities and the entities onwhich they depend for their identities, called identifyingrelationships, are marked by solid lines

I It is possible to change an ID dependent entity into a non-IDdependent entity by coding such weak entity to remove the IDdependence. An example is a vehicle ID number (whichencodes the manufacturer as well as model year). In this case,the relationship is shown in dashed lines

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependency

Functional dependence is a relationship between attributes inwhich given the value of one attribute, the value of anotherattribute is determined.

An Example: Consider the following relation:

EMPLOYEE ( Social Security No, Date of Birth, Date of Hire, . . .)

Since social security number uniquely identifies an employee, if youknow an employee’s social security number his/her date of birthand date of hire are already determined. In other words, socialsecurity number functionally determines the date of birth and dateof hire. This can be written as:

Social Security No → Date of Birth

Social Security No → Date of Hire

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependency

Another Example:

SalesOrder(SalesOrderNumber, CustomerName, Address, Ite-mOrdered, QuantityOrdered)

The functional dependencies in this relation are:

SalesOrderNumber → CustomerNameSalesOrderNumber → AddressSalesOrderNumber → ItemOrderedSalesOrderNumber → QuantityOrderedCustomerName → Address (Assuming a customer can have only one

address)

Remark: Here, a non-key attribute address is functionallydependent on another non-key attribute CustomerName

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependency

Yet Another Example:

Student(StudentName, Studentemail, AdvisorName, Advisoremail,Department, DepartmentAdmin)

The functional dependencies in the above relations are:StudentName → StudentemailStudentName → AdvisorNameStudentName → Advisoremail (assuming a student can have only oneadvisor)StudentName → Department (assuming a student can register in onlyone department)StudentName → DepartmentAdmin (assuming a student can register inonly one department)AdvisorName → Advisoremail

Department → DepartmentAdmin

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependency

I The relationships between attributes expressed in functionaldependencies are exploited in relational database design tominimise duplication of data and eliminating deletion, updateand insertion anomalies.

I Functional dependencies reflect the business rules that exist inorganisations. Therefore, considering them in database designprevents conflicts between the database and businessoperations.

I Functional dependencies are also very useful in theidentification of candidate keys, since to be a key, a set ofattributes must functionally determine all other remainingattributes in that relation.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Reasoning with Functional Dependencies

Armstrong Axioms: These axioms are a complete set of inferencerules for reasoning about functional dependencies:

Reflexivity If {B1, B2, B3, . . . , Bm } ⊆ {A1, A2, A3, . . . , Am },then A1A2 . . . An → B1B2 . . . Bm. These are calledtrivial functional dependencies

Augmentation If A1A2 . . . An → B1B2 . . . Bm, thenA1A2 . . . AnC1C2 . . . Ck → B1B2 . . . BmC1C2 . . . Ck

ie., if you append the same excess baggage on bothends of the arrow, the functional dependency stillholds

Transitivity If A1A2 . . . An → B1B2B3 . . . Bm and B1B2B3 . . . Bm

→ C1C2 . . . Ck , then A1A2 . . . An → C1C2 . . . Ck .

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Relation Keys

I Key: Key of a relation is an attribute or a set of attributesthat uniquely identifies a row . Since each row in a relationpertains to an individual entity, if you know the values of thekey attributes of such entity, then the values of the remainingnon-key attributes for that entity are already determined. Inother words, the non-key attributes in a relation arefunctionally dependent on the key.

An Example:

The social security number can be the key of an Employeerelation given below because no two employees can have thesame social security number. The relation key is underlined.

Employee(SocialSecurityNo., FullName, Address, eMailAddress)

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Relation Keys

I It is possible that in a given situation, FullName of theemployees can also be a key because no two employeescurrently share FullName. However, merely that there is apossibility that two employees can share a FullName suggeststhat it is not a good idea to consider it a key.

I Candidate Keys: A relation can have more than one key. Forexample, in the above Employee relation, if an employee cannot have more than one email address, then eMailAddress canalso be a key. In that case, FullName and eMailAddress areboth called Candidate keys.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Relation Keys

I Primary Keys: The database designer must pick one of thecandidate keys to be the Primary key . Database ManagementSystems such as Oracle or DB2 maintain indexes to the tablesbased on such primary keys. This makes retrieval ofinformation from databases faster.

I Composite Keys: It is not necessary that the keys consist ofjust one attribute. It may be composed of a set of attributes,in which case it is called a Composite key . For example, inthe GradeRoster example below, the key is composed ofStudentIDNumber, CourseNumber. Sometimes the only keyof a relation may consist of all the attributes of the relation.

GradeRoster(StudentIDNumber, CourseNumber, Grade )

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Relation Keys

I Surrogate Keys: Sometimes the Database ManagementSystem assigns the primary key. Such keys are called surrogatekeys. A common example is the sales order that companiesprepare when they receive an order from a customer. DBMSassigns sales orders unique consecutive primary keys usuallycalled salesOrderNumber . See the relation below:

SalesOrder(SalesOrderNumber, CustomerName, Address, Ite-mOrdered, QuantityOrdered)

Assigning such consecutive surrogate keys makes it possible tofind out if all sales orders have been processed.

Surrogate keys are also used when the key natural to arelation is awkward to use. For example, for the propertyrecords the street address is the natural key, but a surrogatekey such as PropertyID may be used.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Relation Keys

I Foreign Keys: Foreign keys are non-key attributes of a relationthat are primary keys of a foreign (ie., different) relation. Forexample, in the Delivery example below, DeliveryNo. is thesurrogate key, and the attributes OrderNo., CustomerNo.,TruckNo., SalesmanNo. are keys of foreign relations Order ,Customer , Truck, and Salesman respectively.

Delivery(DeliveryNo., OrderNo., CustomerNo., TruckNo., Sales-manNo.)

Foreign keys enable us to represent relationships in databases.They also enable us to ensure referential integrity of thedatabase. For example, in the delivery example, referentialintegrity would entail not permitting in the database a deliveryto a customer who does not exist in the database, delivery ofa non-existent order, delivery by a non-existent salesman, ordelivery by a non-existent truck.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Use of Armstrong axioms to identify candidate keys

R(A, B, C , D) (1)

with the set of functional dependencies given by

F = A→ B, A→ E , B → D, C → A, D → C (2)

The candidate keys for the relation R are A and C:A is a Candidate key: (i) A → B, (given); (ii) A → C, (usingtransitivity twice) A → B, B → D, and D → C; (iii) A → D, (bytransitivity): A → B, andB → D; (iv) A → E, (given)C is a Candidate key: (i) C → A; (ii) C → B (by transitivity)C → A, and A → B; (iii) C → D (using transitivity twice) C → A,A → B, and B → D; (iv) C → E (given)

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Design

Anomalies Caused by Poor Database DesignWhen a table contains information on more than one entity certainanomalies, which make the maintenance of the database difficult,can occur. Care must be exercised in the design of the databasesto avoid such anomalies.

I Insertion Anomalies

I Deletion Anomalies

I Update Anomalies

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Insertion Anomalies

You can not enter facts about an entity unless you haveinformation about another entity.

An Example:

You can not insert a new admitted student unless you know whoher advisor is.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Deletion Anomalies

Deletion of facts about one entity can delete facts about anotherentity.

An Example:

If an advisor has only one advisee, deleting that advisee will deletethe advisor entirely from the database.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Update Anomalies

Updating the database for a fact may require scanning the entiretable.

An Example:

If an advisor’s email address changes, you will need to scan theentire Student table and replace the email for each occurence ofthat advisor.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Characteristics of a Good Database

I Insertion of a row in a table does not have any side-effects onother entities (anomaly)

I Modifications require replacing at most one row (tuple) in atable

I Insertion of a row in a table does not refer to a non-existententity (referential integrity)

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Design: Normalization of Databases

Database normalization avoids unnecessary duplication of data,and leads to smaller relations. If the tables in the database are notnormalized, the following anomalies can ocur

I Insertion Anomalyadding new rows forces user to createduplicate data

I Deletion Anomalydeleting rows may cause a loss of data thatwould be needed for other future rows

I Modification Anomalychanging data in a row forces changesto other rows because of duplication

General rule of thumb: A table should not pertain to more thanone entity type

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Anomalies: An Example

Figure: Database Anomalies

c©Prentice Hall, 2011

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Anomalies: An Example

I Insertion cant enter a new employee without having theemployee take a class

I Deletion if we remove employee 140, we lose informationabout the existence of a Tax Acc class

I Modification giving a salary increase to employee 100 forcesus to update multiple records

c©Prentice Hall, 2011

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependency and Relation Key

Functional Dependency If the value of an attribute (or set ofattributes) A determines the value of anotherattribute (or set of attributes) B, then we say that Afunctionally determines B, or that B is functionallydependent on A

Relation key An attribute (or a set of attributes) of a relation issaid to the a relation key if all other remainingattributes of the relation are functionally dependenton such an attribute (or set of attributes)

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependence and Relation Key: Example 1

Figure: Employee

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Functional Dependence and Relation Key: An extendedInvoice example

Figure: A Sales Invoice

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Normalisation: An extended example

Figure: Un-normalised Database Relation

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Normalisation: An extended example

Figure: Un-normalised Database Relation

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Normalisation: An extended example

The 1NF relations are derived from non-normalized relations bysplitting (decomposing) such relations so as to eliminate repeatedgroups. For example, we can split the above invoice relation intotwo relations (Invoice and Invoice Details) as done below:

Figure: First Normal Form relationsJagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Second Normal Form

A relation is said to be in the Second Normal Form if it is in theFirst normal Form and all non-key attributes are functionallydependent on the WHOLE key. Now let us see if the two relationsin the above figure are in 2NF.

I INVOICE RELATION: INVOICE-NUM is the relation keysince all other attributes in the relation INVOICE arefunctionally dependent on it. Since all such non-key attributesare functionally dependent on the whole key (consisting of thesingle attribute INVOICE NUM), the relation INVOICE is in2NF.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Second Normal Form

I INVOICE DETAILS RELATION:Since ITEM-DESCR, PRICE,QUANTITY, and AMOUNT are all functionally dependent onthe set of attributes INV-NUM, ITEM-NUM, this set is therelation key. However, this relation is NOT in the SecondNormal Form since the non-key attributes ITEM DESCR andPRICE are functionally dependent on a part of the relationkey ( ITEM NUM) rather than the whole relation key. We canderive the Second Normal Form relations forINVOICE DETAILS by splitting this relation into two byforming a separate relation with the offending attributes ofITEM DESCR and PRICE.

The resultant 2NF relations for INVOICE DETAILS are given inthe figure on the next slide

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Normalisation: An extended example

Second Normal Form Decomposition of INVOICE-DETAILSRelation

Figure: Second Normal Form Decomposition of INVOICE-DETAILSRelation

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Normalisation: An extended example

A relation is said to be in the Third Normal Form if it is in theSecond Normal Form, and in addition each non-key attribute isfunctionally dependent on the whole key and nothing but the key.

The INVOICE relation above is in the Second Normal Form, but isNOT in the Third Normal Form since the attribute CUST ADDRis functionally dependent on the non-key attribute CUST NAME.To convert it to the Third Normal Form, we again split theINVOICE relation into INVOICE and CUSTOMER relations byremoving the offending attributes from INVOICE and creating theCUSTOMER relation. The final 3NF relations for the INVOICEexample are on the next slide

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Database Normalisation: Third Normal Form Relations

Second Normal Form Decomposition of INVOICE-DETAILSRelation

Figure: Third Normal Form Decomposition of Invoice Relation

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Structured Query Language

I Not a complete programming language

I It is a data sub-language

I Much of the Data Manipulation Language (DML) part of SQLis a declarative language, ie., you instruct the computer whatyou want, and not how to find it

I To build complete systems, you must embed SQL code in animperative language such as Java, C , or C++. Imperativelanguages instruct the computer how to find answers

I SQL standardsI ANSI SQL 1986, 1989, 1992 (SQL-92)I 1999 ANSI SQL-3 (added object-oriented features). 2003,

2006I 2008 SQL:2008 (added support for XML)

I NOT ALL STANDARD SQL SYNTAX WORKS INMS-ACCESS. Access also defaults to SQL-89, but you canchange the options to change the default to SQL-1992 (SQLServer Compatible Syntax).

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Structured Query Language

I QBE (Query By Example), a graphical interface for querying,based on logic (relational calculus)

I SQL (Structured Query Language), a command line and textoriented query language (based on relational algebra)

I A good idea to know both, though as a beginner it is a goodidea to be able to use SQL

I SQL consists of two sub-languagesI Data Definition Language (DDL): used to create database

tables, specify keys, data types for attributes, etc.I Data Manipulation Language (DML): used to populate tables

(insert, delete, update)

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Definition Language

I CREATE TABLE:CREATE TABLE TableName (ColumnName DataType optionalConstraint,ColumnName DataType optionalConstraint,. . . . . . . . ., );

I optional constraints: PRIMARY KEY, NOT NULL, NULL,and UNIQUE which is not supported by MS-Access SQL-89)

I Allowable data types: AutoNumber, Text (max length: 256characters), Memo (up to 2GB data and RTF support),Number (up to 16 bytes of data), Date/Time (including Autocalendar feature), Currency (with precision to four decimalplaces), Yes/No (Boolean data values), OLE object (up to2GB, to store graphs, documents, etc.), Hyperlink (max sizelimit: 1GB), Attachment ( used to store images, spreadsheetfiles, documents, charts and other types of supported files.New feature in Access 2007) See Figure 3-5 in the textbook.

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Definition Language

I For details of all data types supported seehttp://msdn.microsoft.com/en-us/library/ms714540(VS.85).aspxorhttp://www.databasedev.co.uk/access2007 data types.html#auto calendar

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Structured Query Language

An Example:

CREATE TABLE EMPLOYEE (

EmployeeNumber Int NOT NULL IDENTITY (1, 1),

FirstName Char(25) NOT NULL,

LastName Char(25) NOT NULL,

Department Char(35) NOT NULL DEFAULT ’Human Resources’,

Phone Char(12) NULL,

Email VarChar(100) NOT NULL UNIQUE,

CONSTRAINT EMPLOYEE PK PRIMARY KEY (EmployeeNumber),

CONSTRAINT EMP DEPART FK FOREIGN KEY (Department),

REFERENCES DEPARTMENT (DepartmentName),

ON UPDATE CASCADE

);

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Structured Query Language

Another Example:

CREATE TABLE ASSIGNMENT (

ProjectID Int NOT NULL,

EmployeeNumber Int NOT NULL,

HoursWorked Numeric (6, 2) NULL,

CONSTRAINT ASSIGNMENT PK PRIMARY KEY (ProjectID, EmployeeNumber),

CONSTRAINT ASSIGN PROJ FK FOREIGN KEY (ProjectID)

REFERENCES PROJECT (ProjectID)

On UPDATE NO ACTION

ON DELETE CASCADE,

CONSTRAINT ASSIGN EMP FK FOREIGN KEY (EmployeeNumber)

REFERENCES EMPLOYEE (EmployeeNumber)

ON UPDATE NO ACTION

ON DELETE NO ACTION

);

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Structured Query Language

I Business rules are implemented in DDL part of SQL asCONSTRAINTs

I Most constraints deal with the treatment of UPDATEs andDELETEs

I Surrogate keys are implemented in SQLServer throughspecification of IDENTITY (m,n) where m is the startingsequence number assigned to the first row, and n is theincrement.

I Implementation of surrogate keys varies from vendor to vendor

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language (DML)

I SQL provides SELECT, INSERT, and DELETE statements

I SELECT statement is the work horse of DML and is thefundamental statement for database querying

I INSERT and DELETE statements are available for updating thedatabase

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: INSERT

I Syntax:INSERT INTO 〈TableName〉(VALUES 〈Tuple to be inserted〉 );

I Integer and numeric values must NOT be enclosed in singlequotes

I Char, Varchar, and DateTime values must be enclosed insingle quotes

I SQL is fussy about single quotes. Directional quotes willproduce errors

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: INSERT

If some column data is missing:

I Syntax:INSERT INTO 〈TableName〉(〈List of field names to be inserted〉)(VALUES 〈Tuple to be inserted〉 );

I Order of the field (column) names must match the order ofvalues, but the order of the field (column) names need notmatch the order in the table

I values must be provided for all fields (columns) that can nothave NULL values

I NULL values will be inserted for missing field names

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: SELECT

I We will proceed from very simple to complex queries. Ourobjective is to master the syntax

I A Simple Query:SELECT 〈Comma separated Column names〉FROM 〈Table name〉WHERE 〈Condition〉;

I SELECT tells us which column data must be returned by thequery

I FROM specifies the table to be searched for the data

I WHERE specifies the conditions to be met if the data item is tobe returned by the query

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: SELECT

I You can select every column from a table by specifying * tobe the 〈Comma separated Column names〉

I The results of every SQL query is a Table

I However, the result is NOT a relation since it can containduplicate rows

I You can eliminate such duplicate rows by using the keywordDUPLICATE

I An exampleSELECT DISTINCT DepartmentFROM ProjectWHERE MaxHours > 135;

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: SELECT WHERE

I You can connect any number of conditions by using keywordsAND and OR. For example,

WHERE Department = ’Accounting’ OR Phone =360-285-8410’;

I You can You can specify that the column value should belongto a set of values. For example,

WHERE Department IN (’Accounting’, ’Finance, Marketing’);or

WHERE Department NOT IN (’Accounting’, ’Finance,Marketing’);

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: SELECT WHERE

I You can specify ranges by using comparison operators such as=, >,<, >=, <=, <> and the keyword BETWEEN. Forexample,

WHERE EmployeeNumber BETWEEN 2 AND 5;

which is equivalent to

WHERE EmployeeNumber >= 2 AND EmployeeNumber <= 5;

I You can search by pattern by using wildcard characters and%. stands for one unspecified character and % stands forone or more unspecified characters as in the following

WHERE Phone LIKE ’360-237-5 67’; or

WHERE Phone LIKE ’360-237-5%;

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)

Data Manipulation Language: SELECT WHERE

I You can search for NULL values by using the keyword IS NULLas in

WHERE Phone IS NULL

Jagdish S. Gangolly Informatics CCI SUNY Albany Inf 523 Fundamentals of Information Technology: Databases (Fall 2011)