sql/sybase programming. a database is... a collection of related tables containing data and...
TRANSCRIPT
SQL/SyBase ProgrammingSQL/SyBase Programming
A Database is...A Database is...
A collection of related tables A collection of related tables containing data and definitions of containing data and definitions of database objects.database objects.
The “table” is a paradigm which The “table” is a paradigm which describes the data stored within the describes the data stored within the database.database.
The Relational ModelThe Relational Model• Codd’s 12 RulesCodd’s 12 Rules
– Information rule - everything is a tableInformation rule - everything is a table
– Data independenceData independence
– Comprehensive data sublanguageComprehensive data sublanguage
– Support relational operations (select, Support relational operations (select, project, join)project, join)
– View updating ruleView updating rule
– Systematic null value supportSystematic null value support
– Integrity independenceIntegrity independence
TablesTables
• Tables contain:Tables contain:– Rows = RecordsRows = Records
– Columns = FieldsColumns = Fields
– Primary Key = Unique IdentifierPrimary Key = Unique Identifier
– Data Elements = ValueData Elements = Value
Occupation TableOccupation Table
KEY FIELD
ID Name Age Occupation
RECORD 101 Will Williams 24 Chef
102 Dave Davidson 19 Programmer
103 Jan Janis 35 Pilot
104 Bill Jackson 42 Engineer
105 Don DeMarco 30
106 Becky Boudreaux 29
Two Kinds of TablesTwo Kinds of Tables
• User TablesUser Tables– contain user loaded datacontain user loaded data
– primary query tablesprimary query tables
– created, modified, maintained by usercreated, modified, maintained by user
• System TablesSystem Tables– contain database and table descriptionscontain database and table descriptions
– maintained by database systemmaintained by database system
– can be queried like any other tablecan be queried like any other table
Structured Query LanguageStructured Query Language(SQL)(SQL)• SQL specifies syntax features for retrieval SQL specifies syntax features for retrieval
update and definition of the database.update and definition of the database.
– SELECT SELECT - query data in the database- query data in the database– INSERTINSERT - Add a single row - Add a single row– UPDATEUPDATE - Alter attribute values in rows - Alter attribute values in rows
– DELETEDELETE - Delete rows of data from a - Delete rows of data from a tabletable
Select StatementSelect StatementIts syntax is : Its syntax is :
SELECT SELECT (select-list - attributes or derived (select-list - attributes or derived data)data)
FROM FROM (table name or names) (table name or names)
WHEREWHERE (sets up conditions) (sets up conditions)
SELECT and FROM are requiredSELECT and FROM are required
SELECT Name FROM OCCUPATIONSSELECT Name FROM OCCUPATIONS
WHERE ID = 101;WHERE ID = 101;
Select ExamplesSelect Examples
PNAMENUTBOLTCARAVAN
PARTNOP1P2P3
PRICE$0.20$1.00$5000.00
QOH2040 3
SELECT * FROM PART;
Selects all column values for all rows
SELECT PARTNO, PNAME FROM PART
WHERE PRICE >= 0.2;
Selects rows where price >= .2
SELECT PARTNO, PNAME FROM PART
WHERE PNAME LIKE (’CAR%');
Selects rows where pname has a value starting with CAR
PART
Insert StatementInsert Statement
This command allows data to be inserted, This command allows data to be inserted, one one row at a timerow at a time, into an existing table, into an existing table
Syntax: Insert into <tablename> (list of attributes) Syntax: Insert into <tablename> (list of attributes) values (list of values)values (list of values)
Note: The list of attributes can be ignored providing Note: The list of attributes can be ignored providing the order of attribute values, and completeness of the order of attribute values, and completeness of attribute instances, is as per the table list.attribute instances, is as per the table list.
example: example: insert into emp(name, sal, byear)insert into emp(name, sal, byear) values(‘Jones, Bill’, 45000,1967);values(‘Jones, Bill’, 45000,1967);or(see note) or(see note) insert into emp values(‘Jones, Bill’, insert into emp values(‘Jones, Bill’,
45000,1967);45000,1967);
Update StatementUpdate StatementUPDATE tablename UPDATE tablename
SET colname = expression { , colname = expression}SET colname = expression { , colname = expression}
[ WHERE search_condition ][ WHERE search_condition ]
• Replaces values of the specified columns with expression Replaces values of the specified columns with expression values for all rows satisfying the search-condition.values for all rows satisfying the search-condition.
• Expressions in the set clause may be constants or column Expressions in the set clause may be constants or column values from the UPDATE tablename or FROM tablenamevalues from the UPDATE tablename or FROM tablename
Example: UPDATE PARTExample: UPDATE PART
SET price = price * 1.1SET price = price * 1.1
WHERE price < 20; WHERE price < 20;
Delete StatementDelete Statement
DELETE FROM tablenameDELETE FROM tablename
[ WHERE search-condition ][ WHERE search-condition ]
Delete one or many rows in a table.Delete one or many rows in a table. In In general search-condition and qualification general search-condition and qualification may not involve a sub select on tablename.may not involve a sub select on tablename.
DELETE FROM PART WHERE qoh < 4.00DELETE FROM PART WHERE qoh < 4.00;;
Joining TablesJoining TablesEMP DEP
SELECT e.empno AS Number, e.ename AS Name, d.dname AS Department
FROM emp e, dep d
WHERE e.deptno = d.deptno;
EMPNOE1E2E3
ENAMEREDBLUEBROWN
MGRNOE1E1E1
DEPTNOD1D1D2
DEPTNOD1D2D3
DNAMETAXPAYLEAVE
NumberE1E2E3
NameREDBLUEBROWN
DepartmentTAXTAXPAY
A Search and Join A Search and Join ConditionCondition
For each prime minister born in or after 1900, give his name, birth year and party.
SELECT P.PM_NAME, BIRTH_YR, PARTYFROM PRIME_MINISTER P, MINISTRY MWHERE P.PM_NAME = M.PM_NAME AND BIRTH_YR >= 1900;
PM_NAME BIRTH_YR PARTYHolt H E 1908 LiberalHolt H E 1908 LiberalMcEwen J 1900 CountryGorton J G 1911 LiberalGorton J G 1911 LiberalGorton J G 1911 Liberal