Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Definition After this lecture, you should be able to:

Similar presentations


Presentation on theme: "Data Definition After this lecture, you should be able to:"— Presentation transcript:

1 Data Definition After this lecture, you should be able to:
Use SQL DDL Statement to define a database structure. Work with mysql or phpMyAdmin to access your MySQL databases. Complete Assignment 2 (Part II - a, b). 1

2 SQL DDL Statements Create Table create table s (sno char(5), ... );
Drop Table drop table s; Alter Table alter table s add phone char(13); Create View create view SV as select .... Drop View drop view SV; Create Index create index X on s(sno); Drop Index drop index X;

3 Suppliers-Parts Database: E-R Diagram
sno (key) pno (key) sname pname SP M S M P city status city qty color weight

4 Supplier-Part Database
Table S Table P sno | sname | status | city pno | pname | color | weight | city s1 | Smith | | London p1 | nut | red | | London s2 | Jones | | Paris p2 | bolt | green | | Paris s3 | Blake | | Paris p3 | screw | blue | | Rome s4 | Clark | | London p4 | screw | red | | London s5 | Adams | | Athens p5 | cam | blue | | Paris p6 | cog | red | | London Table SP sno | pno | qty s1 | p1 | 300 s1 | p2 | 200 s1 | p3 | 400 s1 | p4 | 200 s1 | p5 | 100 s1 | p6 | 100 s2 | p1 | 300 s2 | p2 | 400 s3 | p2 | 200 s4 | p2 | 200 s4 | p4 | 300 s4 | p5 | 400

5 Create Table Statements
sno char(5) not null, sname char(20) not null, status smallint, city char(15), primary key (sno) ); create table p ( pno char(6) not null, pname char(20) not null, color char(6), weight smallint, primary key (pno) create table sp ( qty integer not null, primary key (sno, pno)

6 Data Types integer The magnitude range is -2,147,484, ,147,484,647. real For fixed or floating-point numbers. Allowable values are E+38 to E-38, 0, and E-38 to E+38. char For fixed length character strings up to 255 bytes. varchar For variable-length character strings. The maximum length is 65,532 bytes. bit[n] n indicates the number of bits, from 1 to 64. The default is 1 if n is omitted . date Year values in the range are converted to Year values in the range are converted to datetime Date and time. The format is ‘YYYY-MM-DD HH:MM:SS’.

7 Creating S-P-SP Database
% mysql –h mysql.cs.orst.edu –u pham –p pham Enter password: mysql> \. create_s_p_sp.sql; Query OK, 0 rows affected (0.01 sec) SQL> show tables; TABLE_in_pham p s sp

8 Looking at Table Structures
mysql> desc s; Name Null? Type SNO NOT NULL CHAR(5) SNAME NOT NULL CHAR(20) STATUS SMALLINT(6) CITY CHAR(15) mysql> desc p; Name Null? Type PNO NOT NULL CHAR(6) PNAME NOT NULL CHAR(20) COLOR CHAR(6) WEIGHT SMALLINT(6)

9 Adding Data to a Table insert into s
values('s1', 'Smith', 20, 'London'); values('s2', 'Jones', 10, 'Paris'); values('s3', 'Blake', 30, 'Paris'); values('s4', 'Clark', 20, 'London'); values('s5', 'Adams', 30, 'Athens'); mysql> \. insert_s_p_sp.sql Query OK, 0 rows affected (0.01 sec)

10 Showing Table Contents
SQL> select * from s; SNO SNAME STATUS CITY s1 Smith London s2 Jones Paris s3 Blake Paris s4 Clark London s5 Adams Athens SQL> select * from p; PNO PNAME COLOR WEIGHT CITY p1 nut red London p2 bolt green Paris p3 screw blue Rome p4 screw red London p5 cam blue Paris p6 cog red London

11 Sailors-Boats Database: E-R Diagram (Schema)
sid (key) sname bid (key) bname Reserves Sailor M M Boat date rating age color 4

12 Create Table Statements
create table sailors ( sid integer not null, sname char(20) not null, rating smallint, age real, primary key (sid) ); create table boats ( bid integer not null, bname char(20) not null, color char(10), primary key (bid) create table reserves ( sid integer not null, day date not null, primary key (sid, bid, day)

13 Sailors-Boats Database: Tables
sid sname rating age 22 Dustin 7 45.0 33 Libber 8 55.5 Boats bid bname color 2 Yuppy Blue 3 Lubber Red Reserves sid bid day 22 2 10/10/96 3 10/09/96 33 11/12/96


Download ppt "Data Definition After this lecture, you should be able to:"

Similar presentations


Ads by Google