Introduction to SQL Example: CREATE TABLE Students (sid CHAR ...

71 downloads 901 Views 211KB Size Report
CS 5614: Basic Data Definition and Modification in SQL. 67. Introduction to SQL ...... V ORACLE: PL/SQL (procedural extensions to SQL). V Open Database ...
CS 5614: Basic Data Definition and Modification in SQL

67

Introduction to SQL

† † †

†

Structured Query Language (‘Sequel’)

† Serves as DDL as well as DML

Declarative † Say what you want without specifying how to do it † One of the main reasons for commercial success of DBMSs Many standards and implementations † ANSI SQL † SQL-92/SQL-2 (Null operations, Outerjoins etc.) † SQL3 (Recursion, Triggers, Objects) † Vendor specific implementations “Bag Semantics” instead of “Set Semantics” † Used in commercial RDBMSs

CS 5614: Basic Data Definition and Modification in SQL

Example:

†

Create a Relation/Table in SQL

CREATE TABLE Students (sid CHAR(9), name VARCHAR(20), login CHAR(8), age INTEGER, gpa REAL);

†

Support for Basic Data Types

† CHAR(n) † VARCHAR(n) † BIT(n) † BIT VARYING(n) † INT/INTEGER † FLOAT † REAL, DOUBLE PRECISION † DECIMAL(p,d) † DATE, TIME etc.

68

CS 5614: Basic Data Definition and Modification in SQL

69

More Examples

†

And one for Courses

CREATE TABLE Courses (courseid CHAR(6), department CHAR(20));

†

And one for their relationship!

CREATE TABLE takes

† Why?

†

(sid CHAR(9), courseid CHAR(6));

Can also provide default values

CREATE TABLE Students (sid CHAR(9), .... age INTEGER DEFAULT 21, gpa REAL);

CS 5614: Basic Data Definition and Modification in SQL

Examples Contd.

† † † †

DATE and TIME † Implementations vary widely † Typically treated as strings of a special form † Allows comparisons of an ordinal nature ( etc.) DATE Example † ‘1999-03-03’ (No Y2K problems) TIME Examples † ‘15:30:29’ † ‘15:30:29.3875’ Deleting a Relation/Table in SQL

DROP TABLE Students;

70

CS 5614: Basic Data Definition and Modification in SQL

71

Modifying Relation Schemas

†

‘Drop’ an attribute (column)

ALTER TABLE Students DROP login;

†

‘Add’ an attribute (column)

ALTER TABLE Students ADD phone CHAR(7);

† What happens to the new entry for the old records? † Default is ‘NULL’ or say ALTER TABLE Students ADD phone CHAR(7) DEFAULT ‘unknown’;

†

Always begin with ‘ ALTER TABLE ’

†

Can use DEFAULT even with regular definition (as in Slide 69)

CS 5614: Basic Data Definition and Modification in SQL

How do you enter/modify data?

†

INSERT command INSERT INTO Students VALUES (‘53688’,’Mark’,’mark2345’,23,3.9)

† Cumbersome (use bulk loading; described later)

†

DELETE command DELETE FROM Students S WHERE S.name = ‘Smith’

†

UPDATE command UPDATE Students S SET S.age=S.age+1, S.gpa=S.gpa-1 WHERE S.sid = ‘53688’

72

CS 5614: Basic Data Definition and Modification in SQL

73

Domains

†

Domains: Similar to Structs and other user-defined types

CREATE DOMAIN Email AS CHAR(8) DEFAULT ‘unknown’; .... login Email // instead of login CHAR(8) DEFAULT ‘unknown’

†

Advantages: can be reused

junkaddress Email, fromaddress Email, toaddress Email, ....

†

Can DROP DOMAINS too!

DROP DOMAIN Email;

† Affects only future declarations

CS 5614: Basic Data Definition and Modification in SQL

Keys

†

To Specify Keys

† Use PRIMARY KEY or UNIQUE † Declare alongside attribute † For multiattribute keys, declare as a separate line CREATE TABLE takes ( sid CHAR(9), courseid CHAR(6), PRIMARY KEY (sid,courseid) );

†

Whats the difference between PRIMARY KEY and UNIQUE?

† Typically only one PRIMARY KEY but any number of UNIQUE keys † Implementor allowed to attach special significance

74

CS 5614: Basic Data Definition and Modification in SQL

75

Creating Indices/Indexes

† †

Why?

† Speeds up query processing time For Students

CREATE INDEX indexone ON Students(sid); CREATE INDEX indextwo ON Students(login);

†

† †

How to decide attributes to place indices on? † One is (typically) created by default on PRIMARY KEY † Creation of indices on UNIQUE attributes is implementation-dependent † In general, physical database design/tuning is very difficult! † Use Tools: Microsoft SQLServer has an index selection Wizard Why not place indices on all attributes? † Too cumbersome for insertions/deletions/updates Like all things in computer science, there is a tradeoff! :-)

CS 5614: Basic Data Definition and Modification in SQL

Other Properties

†

‘NOT NULL’ instead of DEFAULT CREATE TABLE Students (sid CHAR(9), name VARCHAR(20), login CHAR(8), age INTEGER, gpa REAL);

† †

Can insert a tuple without a value for gpa

† NULL will be inserted If we had specified

gpa REAL NOT NULL);

† insert cannot be made!

76

   

"!$#&%('*),+.-&/10(2$35462748%(0(09':!;/&-?2@2A-&%CBD2@>?2$;!FEG/&2$>:?2$0CIY!;%C+L&IJ0L)"+.-&2$0CZ\[]#&2 ^&>N'!_%('9>N2$0(IP!;%C+L&IJ0.IL0(KJ2$`&>NIL3GILQIL0CKL2$`&>NIL%CR_IJ&-AO1>?+.R$2$-1/&>?IJ0 46I]R$>?2@IY!;%C&Kc&2N4d>?2@0(IY!$%(+J&']ab>?+J)eKL%gfD2$+L12$'?Zh[]#&2Q'?2$R@+L&-%(']i*IY!;IJ0(+JKL3548#&%(RY#%(']0(+JKL%CR$IL0jIL&-&2$R@0(IL>NIY!$%XfD2h%(&IY!$/&>?2,k:'!;/1-&2$;!;']abIL)"%(0C%(IJ>l48%X!$#F!;#&2Qm n]oQp9o8qrO&>?+JKL>NIL)"),%C&K,0CIL&KJ/&ILKJ2h48%C0(0^1&!;#1%('\IJ0(0!;+.+c&IP!;/&>NIL0(sNZtabIJRN!;3!;#&2@>?2uIJ>?2uR$0C+L'N2uR$+L>N>?2$'NOv+L&-&2@&R$2$'w`D2V!V4u2$2$-1IY!;IJ`&IL'N2Q'N2,IJ0(0|!;#12-&IY!$I}^J!$'A%C !$+ )"IL%(~)"2$)"+L>?IJ'!$3uI-1%('!$%(&KJ/&%('N#&%(1Kab2$IY!$/&>?2S+Launhiu‚]ƒ„&'c%C'Q!$#&IY!"!;#&2VNIY!;2+J '?2@R$+L&-1IL>?2@&R$2$'Q%(yEG/&2$>N+.R$2$'?'N%(&KJs?3ˆ!$#&2$>?2"IL>N2 2N‰R$2$0C0(2$;!7IL1IL0(+JKL'w!$+c`v+Y!;#SR$/&0g!;/&>N2$'?Z]‚]+P!;#m n]o8p9oQqŠIJ&-ntiu‚]ƒ„1']IL>?2Q-&2@R$0(IJ>?IY!$%XfD2$ZŒ‹#&IY!]4u2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+La5!;#128/1&%(+J}>?2@0(IY!$%(+J&± ²

³k'?%C),O10(2$3j>?%(KJ# !$µ s

¶·?¸ ¹?º ¹?»¹¼½~¾À¿›Á ·?¸¹º¹»¹?¼|½ˆÂ ¶·?¸ ¹?º ¹?»¹¼½~¾À¿›Ã ·?¸¹º¹»¹?¼|½ˆÂ Ä*+Y!;%CR$2S!$#&IY!!$#&2SfÀIL>N%(IL`10(2$'

¸ ¹º¹?» ¹?¼ IJ>?2)"2$>?2@0XN'?‘Q/&'?2@-ab+J>O&IY!N!;2$>N  )"IY!$R¡#1%(&KJ˜1462uR$+J/&0(-#&IfD2W48>?%g!?!$2$F!;#&2QIL`v+fD2W!46+cIL'N± ¶·?Å ¹?Æ ¹;ǹȽ~¾À¿›Á ·?Źƹ ǹ?È|½ˆÂ ¶·?É ¹?Ê ¹?˹̽~¾À¿›Ã ·?ɹʹ˹?Ì|½ˆÂ ·?ÃGÍÀÎGÍÀÏG¶ÑÐ ÒÓlÔ\Õ Ö¨ÔØ×_Ù ÚÜۈÝLÔYÚÜÙNÞ?ß9àáÔYÖ9âYãÛßߨÚäÙâˆ×_åVæ]×ەßç×_Ú Þ?èéå ÛVêWÞ?Ô]ë]ê ÚÜìLÛÖÛVÙVÞ_êÔ@èVã í]ÛÙNÞ\ßîÞbæ.ïÜەàîÔ¡ÖjðQÔ$ê ãïäەñ¡ò

óLó

ôGÁõ ö©Á½ ÷GøùÀõ;ø ·?ÃGÍÀÎGÍÀÏG¶ÑÐ ôGÁõ ö©Ã½ ú Zuû¦ü_¬Gý ¬À¥lþ.¬"§Dªw«¬G­¨®&¯@¦¨§v¥l° ±\[]#&2*-&%(Bv2$>N2$&R$2t² 2$0C2$)"2$ !$'l!$#&IY!AIL>N2A%(=²`&/J!A&+Y!

³~+Ja1!46+A>N2$0(IP!;%C+L&'\²ÿIJ&-—³›%('Œ!;#&2*'?2N!u+La

%C=³ Zh’u'h/&'?/1IL0(35462QIJ'?'?/1),27!;#&IP!t!$#&2A'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