SQL/SyBase Programming. A Database is... A collection of related tables containing data and...
-
Upload
quentin-mcdaniel -
Category
Documents
-
view
214 -
download
0
Transcript of SQL/SyBase Programming. A Database is... A collection of related tables containing data and...
![Page 1: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/1.jpg)
SQL/SyBase ProgrammingSQL/SyBase Programming
![Page 2: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/2.jpg)
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.
![Page 3: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/3.jpg)
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
![Page 4: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/4.jpg)
TablesTables
• Tables contain:Tables contain:– Rows = RecordsRows = Records
– Columns = FieldsColumns = Fields
– Primary Key = Unique IdentifierPrimary Key = Unique Identifier
– Data Elements = ValueData Elements = Value
![Page 5: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/5.jpg)
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
![Page 6: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/6.jpg)
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
![Page 7: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/7.jpg)
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
![Page 8: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/8.jpg)
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;
![Page 9: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/9.jpg)
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
![Page 10: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/10.jpg)
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);
![Page 11: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/11.jpg)
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;
![Page 12: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/12.jpg)
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;;
![Page 13: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/13.jpg)
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
![Page 14: SQL/SyBase Programming. A Database is... A collection of related tables containing data and definitions of database objects. The “table” is a paradigm.](https://reader036.fdocuments.us/reader036/viewer/2022082709/56649d995503460f94a842d7/html5/thumbnails/14.jpg)
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