Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Relational Databases and SQL

Similar presentations


Presentation on theme: "Using Relational Databases and SQL"— Presentation transcript:

1 Using Relational Databases and SQL
Lecture 10: Data Definition Language Steven Emory Department of Computer Science California State University, Los Angeles

2 Miscellany For today’s topic, use the Lyrics database

3 Topics for Today Data Types (Appendix)
Creating Tables (Pages 188 – 190)‏ Deleting Tables (Page 190)‏ Adding Columns (Pages 190 – 191)‏ Deleting Columns (Page 192)‏ Editing Columns (Pages 191 – 192)‏ Identity Columns (Pages 193 – 195)‏ Constraints (Pages 196 – 207)‏

4 Data Types TINYINT(width): 1 byte SMALLINT(width): 2 bytes
MEDIUMINT(width): 3 bytes INT(width): 4 bytes BIGINT(width): 8 bytes FLOAT(n, d): 4 bytes REAL(n, d): 8 bytes DATE: 3 bytes CHAR(n) and VARCHAR(n): Variable bytes

5 Creating Tables Syntax:
CREATE TABLE ( fieldname datatype NULL | NOT NULL, fieldname datatype NULL | NOT NULL, fieldname datatype NULL | NOT NULL ); Each row specifies a column definition Use NULL or NOT NULL to specify whether or not a column can contain NULL values

6 CREATE TABLE Example Example:
CREATE TABLE Musician ( Musician VARCHAR(32) NOT NULL ); INSERT INTO Musician VALUES(‘Guitarist’); INSERT INTO Musician VALUES(‘Pianist’); INSERT INTO Musician VALUES(‘Drummer’); INSERT INTO Musician VALUES(‘Vocalist’); INSERT INTO Musician VALUES(‘Bassist’);

7 Dropping Tables Syntax: Example: DROP TABLE [IF EXISTS] tablename;
DROP TABLE Artists; NOTE: BEGIN and ROLLBACK do not work with DROP TABLE

8 Adding Columns Syntaxes:
ALTER TABLE tablename ADD COLUMN column_definition; ALTER TABLE tablename ADD COLUMN column_definition FIRST; ALTER TABLE tablename ADD COLUMN column_definition AFTER column_name;

9 Examples Examples: ALTER TABLE Musician ADD COLUMN MusicianID INT NOT NULL FIRST; ALTER TABLE Musician ADD COLUMN Type VARCHAR(16) NULL; INSERT INTO Musician VALUES(‘1’, ‘Guitarist’, ‘Electric’);

10 Dropping Columns Syntax: Example:
ALTER TABLE tablename DROP COLUMN column_name; Example: ALTER TABLE Musician DROP COLUMN Type;

11 Editing Columns MODIFY let’s you change a column’s data definition (without renaming it) CHANGE does the same thing as MODIFY, but you can also specify a new column name Syntax: ALTER TABLE tablename MODIFY fieldname column_definition; ALTER TABLE tablename CHANGE oldname newname column_definition

12 Examples Examples: -- Let’s rename the Musician column to Instrument. ALTER TABLE Musician CHANGE Musician Instrument VARCHAR(32) NOT NULL; -- Let’s modify the Instrument column string length to make it really short! ALTER TABLE Musician MODIFY Instrument VARCHAR(4) NOT NULL;

13 Constraints A constraint is any kind of restriction placed on the data inserted into a table Primary Key Constraints: Enforces uniqueness of data. Foreign Key Constraints: A value must refer to an existing piece of data. Default Constraints: Data not specifically inserted will take on default values. Unique Constraints: Forces data (other than the primary key) to be unique.

14 Primary Key Constraints
CREATE TABLE Syntax: Can add the keyword PRIMARY key at end of column definition For more than one column you can use the CONSTRAINT keyword. ALTER TABLE Syntax: ALTER TABLE tablename ADD CONSTRAINT constraint_name PRIMARY KEY (field1, field2, ...);

15 Examples Examples: CREATE TABLE tablename( field1 INT NOT NULL, field2 INT NOT NULL, CONSTRAINT constraint_name PRIMARY KEY(field1) ); ALTER TABLE Titles ADD CONSTRAINT fk_titles PRIMARY KEY(TitleID);

16 Foreign Key Constraints
Sometimes called integrity constraints CREATE TABLE Syntax: CONSTRAINT constraint_name FOREIGN KEY(field1, field2, ...) REFERENCES parent_table(field1, field2, ...)‏ ALTER TABLE Syntax: ALTER TABLE tablename ADD CONSTRAINT constraint_name FOREIGN KEY (field1, field2, ...) REFERENCES parent_table(field1, field2, ...);

17 Examples Example: -- To run the following example, make sure to make the Genre attribute the primary key in the Genre table or else this won’t work! A foreign key must reference a primary key! ALTER TABLE Titles ADD CONSTRAINT fk_titles_genre FOREIGN KEY (Genre) REFERENCES Genre (Genre);

18 Unique Constraints Use to make one or more columns (other than the primary key) contain only unique values CREATE TABLE Syntax: Just add the UNIQUE keyword at the end. ALTER TABLE Syntax: ALTER TABLE tablename ADD CONSTRAINT constraint_name UNIQUE (fieldname1, fieldname2, ...);

19 Examples Examples: CREATE TABLE login( user_id INT NOT NULL PRIMARY KEY, username VARCHAR(32) NOT NULL UNIQUE, password VARCHAR(32) NOT NULL ); ALTER TABLE login ADD CONSTRAINT uq_username UNIQUE(username); INSERT INTO login(1, ‘semory’, ‘mypassword’); INSERT INTO login(2, ‘semory’, ‘anotherpass’);

20 Default Constraints Using CREATE TABLE: Using ALTER TABLE:
After specifying the datatype in the column definition, use the following: DEFAULT value Using ALTER TABLE: ALTER TABLE tablename ALTER fieldname SET DEFAULT value;

21 Examples Examples: DROP TABLE login;
CREATE TABLE login( user_id INT NOT NULL PRIMARY KEY, username VARCHAR(32) NOT NULL UNIQUE, password VARCHAR(32) DEFAULT ‘0xk2739’ NOT NULL ); ALTER TABLE SalesPeople ALTER Base SET DEFAULT ;

22 Dropping Constraints Do not read the book on this! The book’s code will not work! Use the following syntax: Syntax: -- To drop a primary key... ALTER TABLE tablename DROP PRIMARY KEY; -- To drop a foreign key... ALTER TABLE tablename DROP FOREIGN KEY keyname; -- To drop any other key... ALTER TABLE tablename DROP KEY keyname;

23 Examples Examples: -- Drop the fk_titles_genre foreign key in the Titles table. ALTER TABLE Titles DROP FOREIGN KEY fk_titles_genre; -- Drop the pk_genre primary key in the Genre table. ALTER TABLE Genre DROP PRIMARY KEY pk_genre; -- Drop the uq_username unique key in the login table. ALTER TABLE login DROP KEY uq_username;

24 Identity Columns Purpose is to auto-generate primary key values
MySQL uses the non-standard keyword AUTO_INCREMENT and you can only define it on the primary key SQL standard uses GENERATE keyword Syntax: fieldname datatype NOT NULL AUTO_INCREMENT

25 Identity Column Example
DELETE FROM Musician; ALTER TABLE Musician MODIFY Instrument VARCHAR(32) NOT NULL AUTO_INCREMENT; INSERT INTO Musician VALUES(0, ‘Guitarist’); INSERT INTO Musician VALUES(0, ‘Pianist’); INSERT INTO Musician VALUES(0, ‘Vocalist’); INSERT INTO Musician VALUES(0, ‘Bassist’); INSERT INTO Musician VALUES(0, ‘Drummer’);


Download ppt "Using Relational Databases and SQL"

Similar presentations


Ads by Google