Proc SQL: An Overview [PDF]

Create Manager Table. Department Manager Table Includes. Unique Index (Primary Key). Dept Number (Foreign Key −− dep

15 downloads 20 Views 167KB Size

Recommend Stories


Demystifying PROC SQL
Be who you needed when you were younger. Anonymous

SQL Developer Migrations Overview
Sorrow prepares you for joy. It violently sweeps everything out of your house, so that new joy can find

PdF Computer Science: An Overview
There are only two mistakes one can make along the road to truth; not going all the way, and not starting.

[PDF] Computer Science: An Overview
Suffering is a gift. In it is hidden mercy. Rumi

Undocumented and Hard-to-Find PROC SQL Features
The butterfly counts not months but moments, and has time enough. Rabindranath Tagore

Powerful and “Sometimes” Hard-to-find PROC SQL® Features
Respond to every call that excites your spirit. Rumi

PDF Download Learning SQL
When you talk, you are only repeating what you already know. But if you listen, you may learn something

Oracle sql bible pdf
Don't watch the clock, do what it does. Keep Going. Sam Levenson

PDF Download Learning SQL
You miss 100% of the shots you don’t take. Wayne Gretzky

SQL DENGAN POSTGRES [PDF]
5.3 Query = Himpunan. Hasil query sebenarnya merupakan suatu himpunan sebagaimana yang sering kita temui dalam pelajaran matematika. (ingat diagram Venn). Buatlah dua buah tabel berikut: CREATE TABLE tabel1 (id INT);. INSERT INTO tabel1 SELECT 1;. IN

Idea Transcript


Proc SQL: An Overview Carlo R. Cosenza [email protected]

Department of Statistics California State University, East Bay Hayward, CA, USA

Proc SQL: An Overview – p. 1/20

Goals Demonstrate the practical use of Proc SQL. • • • • •

Setup Database Schema Add Constraints Add Data Validate Data Extract Some Information

Proc SQL: An Overview – p. 2/20

Database Schema In a relational database, the schema defines the tables, the fields in each table, and the relationships between fields and tables. • Department Table • Employee Table • Composite Manager Table

Proc SQL: An Overview – p. 3/20

Creating Tables Listing 1: Creating the Employee Table / * C r e a t e Employee T a b l e Employee R e c o r d I n c l u d e s Unique ID ( P r i m a r y Key ) L a s t Name F i r s t Name Dept Number */ PROC SQL ; CREATE TABLE EMP_TABLE (EMP_ ID NUM LABEL='Employee ID' UNIQUE , LAST_NAME CHAR( 3 0 ) LABEL='Last Name' NOT NULL, FIRST _NAME CHAR( 2 0 ) LABEL='First Name' , DEPT_ ID NUM LABEL='Department ID' ) ; QUIT ;

Proc SQL: An Overview – p. 4/20

Creating Tables (cont) Listing 2: Creating the Department Table / * Create Department Table Department Record I n c l u d e s Unique ID ( P r i m a r y Key ) D e p a r t m e n t Name */ PROC SQL ; CREATE TABLE DEPT_TABLE ( DEPT_ID NUM LABEL='Department ID' UNIQUE , DEPT_NAME CHAR( 2 0 ) LABEL='Department Name' ) ; QUIT ;

Proc SQL: An Overview – p. 5/20

Creating Tables (cont) Listing 3: Creating Management Table / * C r e a t e Manager T a b l e D e p a r t m e n t Manager T a b l e I n c l u d e s Unique I n d e x ( P r i m a r y Key ) Dept Number ( F o r e i g n Key −− d e p t _ t a b l e ) Emp Number ( F o r e i g n Key −− emp_ t a b l e ) */ PROC SQL ; CREATE TABLE MGR_TABLE (EMP_ID NUM LABEL='Manager ID' NOT NULL, DEPT_ ID NUM LABEL='Department ID' NOT NULL ) ; QUIT ; PROC SQL ; CREATE INDEX MGR_INDEX ON MGR_TABLE (EMP_ID , DEPT_ ID ) ; QUIT ;

Proc SQL: An Overview – p. 6/20

Take a Peek Listing 4: Employee Table / * t a k e a p e e k a t t h e e m p lo y e e t a b l e notice : constraints . keep t h e unique c o n s t r a i n t s i n t h e back of your head . */ p r o c c o n t e n t s d a t a =emp_ t a b l e ; run ;

Proc SQL: An Overview – p. 7/20

Define Primary Keys I Listing 5: ID Uniquely Identifies Employee /* D e f i n e a P r i m a r y Key t o i n d e x Employee T a b l e Note : t h e P r i m a r y Key c o n s t r a i n t o v e r r i d e s t h e Unique C o n s t r a i n t for a given f i e l d . */ PROC SQL ; ALTER TABLE EMP_TABLE ADD CONSTRAINT PRIM_EMP_KEY PRIMARY KEY (EMP_ ID ) ; QUIT ;

Proc SQL: An Overview – p. 8/20

Define Primary Keys II Listing 6: ID Uniquely Identifies Employee / * E s t a b l i s h DEPT_ID a s P r i m a r y Key * / / * N o t i c e t h a t P r i m a r y Key o v e r r i d e s Unique i n C o n s t r a i n t s * / PROC SQL ; ALTER TABLE DEPT_TABLE ADD CONSTRAINT PRIM_DEPT_KEY PRIMARY KEY ( DEPT_ID ) ; QUIT ;

Proc SQL: An Overview – p. 9/20

Define Foreign Keys I Listing 7: All Managers are Employees / * D e f i n e F o r e i g n Keys Note : Need t o remove c o n s t r a i n t s b e f o r e d e l e t i n g t a b l e s . Keep an e y e on c a s c a d e . * / PROC SQL ; ALTER TABLE MGR_TABLE ADD CONSTRAINT FOREIGN_EMP_KEY FOREIGN KEY (EMP_ ID ) REFERENCES EMP_TABLE ON DELETE RESTRICT ON UPDATE CASCADE ; QUIT ;

Proc SQL: An Overview – p. 10/20

Define Foreign Keys II Listing 8: Managers Manage Departments / * D e f i n e F o r e i g n Keys Note : Need t o remove c o n s t r a i n t s b e f o r e d e l e t i n g t a b l e s . Keep an e y e on c a s c a d e . * / PROC SQL ; ALTER TABLE MGR_TABLE ADD CONSTRAINT FOREIGN_DEPT_KEY FOREIGN KEY ( DEPT_ID ) REFERENCES DEPT_TABLE ON DELETE RESTRICT ON UPDATE CASCADE ; QUIT ;

Proc SQL: An Overview – p. 11/20

Define Foreign Keys III Listing 9: Every Employee Belongs to a Dept /* E v e r y Employee B e l o n g s t o a Dept Note : t h e P r i m a r y Key c o n s t r a i n t o v e r r i d e s t h e Unique C o n s t r a i n t for a given f i e l d . */ PROC SQL ; ALTER TABLE EMP_TABLE ADD CONSTRAINT FOREIGN_EMP_DEPT_KEY FOREIGN KEY ( DEPT_ ID ) REFERENCES DEPT_TABLE ON DELETE RESTRICT ON UPDATE CASCADE ; QUIT ;

Proc SQL: An Overview – p. 12/20

Take Another Peek Listing 10: Describe Manager Table / * use the d e s c r i b e f u n c t i o n . note : i t goes s t r a i g h t to the log . * / proc s ql ; DESCRIBE TABLE mgr_ t a b l e ; quit ;

Proc SQL: An Overview – p. 13/20

Add Some Data I Listing 11: Add Departments / * Add some D e p a r t m e n t s * / PROC SQL ; INSERT INTO DEPT_TABLE ( DEPT_ID , DEPT_NAME) VALUES( 1 0 1 , 'Engineering' ) VALUES( 1 0 2 , 'Maintenance' ) VALUES( 1 0 3 , 'Sales' ) VALUES( 1 0 4 , 'Microbiology' ) VALUES( 1 0 5 , 'Quality Assurance' ) ; SELECT * FROM DEPT ORDER BY DEPT_ID ; QUIT ;

Proc SQL: An Overview – p. 14/20

Add Some Data II Listing 12: Add Employees / * Add some D e p a r t m e n t s * / PROC SQL ; INSERT INTO EMP_TABLE (EMP_ID , LAST_NAME, FIRST _NAME, DEPT_ID ) VALUES( 2 0 0 1 , 'Cosenza' , 'Carlo' , 1 0 1 ) VALUES( 2 0 0 2 , 'Johnson' , 'Bob' , 1 0 2 ) VALUES( 2 0 0 3 , 'Smith' , 'Jack' , 1 0 3 ) VALUES( 2 0 0 4 , 'Lee' , 'Mary' , 1 0 4 ) VALUES( 2 0 0 4 , 'Patel' , 'Vijay' , 1 0 5 ) ; SELECT * FROM EMP ORDER BY EMP_ ID ; QUIT ;

Proc SQL: An Overview – p. 15/20

Add Some Data III Listing 13: Everybody is a Manager / * Add some D e p a r t m e n t s * / PROC SQL ; INSERT INTO MGR_TABLE (EMP_ID , DEPT_ID ) VALUES( 2 0 0 1 , 1 0 1 ) VALUES( 2 0 0 2 , 1 0 2 ) VALUES( 2 0 0 3 , 1 0 3 ) VALUES( 2 0 0 4 , 1 0 4 ) VALUES( 2 0 0 4 , 1 0 5 ) ; SELECT * FROM MGR_TABLE ORDER BY EMP_ ID ; QUIT ;

Proc SQL: An Overview – p. 16/20

Take One Last Peek Listing 14: Describe Manager Table / * use the d e s c r i b e f u n c t i o n . note : i t goes s t r a i g h t to the log . * / proc s ql ; DESCRIBE TABLE mgr_ t a b l e ; quit ;

Proc SQL: An Overview – p. 17/20

Deleting Tables Listing 15: Something is Wrong /* D e m o n s t r a t e t h a t you n e e d t o remove c o n s t r a i n t s before deleting tables */ PROC SQL ; DROP TABLE EMP_TABLE ; QUIT ; / * able to delete multiple t a bl e s * / / * n o t i c e i n c o n s i s t e n t u s e o f comma * / PROC SQL ; DROP TABLE DEPT_TABLE , MGR_TABLE ; QUIT ;

Proc SQL: An Overview – p. 18/20

Deleting Tables (cont) Listing 16: Need to Remove Foreign Key Constraints / * c o n s t r a i n t s must be removed b e f o r e you c a n d e l e t e t h e t a b l e s . * / PROC SQL ; ALTER TABLE MGR_TABLE DROP CONSTRAINT FOREIGN_DEPT_KEY ; QUIT ; PROC SQL ; ALTER TABLE MGR_TABLE DROP CONSTRAINT FOREIGN_EMP_KEY ; QUIT ; PROC SQL ; ALTER TABLE EMP_TABLE DROP CONSTRAINT FOREIGN_EMP_DEPT_KEY ; QUIT ;

Proc SQL: An Overview – p. 19/20

Citations • Cody, Ron. 2007. Learning SAS® by Example: A

Programmer’s Guide. Cary, NC: SAS Institute Inc. • Lafler, Kirk Paul. 2004. Proc SQL: Beyond the Basics

Using SAS® . Cary, NC: SAS Institute Inc.

Proc SQL: An Overview – p. 20/20

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.