Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Definition Language."— Presentation transcript:

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

2 Topics for Today Data Types Creating Tables Deleting Tables Adding Columns Deleting Columns Editing Columns Identity Columns Constraints

3 Integer Data Types Data Types TINYINT(width): 1 byte SMALLINT(width): 2 bytes MEDIUMINT(width): 3 bytes INT(width): 4 bytes BIGINT(width): 8 bytes All (width) specifications are optional INT  given 104, displays 104 INT(8)  given 104, displays 00000104

4 Numeric Data Types Data Types FLOAT(n, d): 4 bytes REAL(n, d): 8 bytes All (n, d) specifications are optional. FLOAT  given 1.06, displays 1.06 FLOAT(10, 1)  given 1.06, displays 1.1 FLOAT(10, 6)  given 1.06, displays 1.060000

5 String and Date Data Types Data Types DATE: 3 bytes CHAR(n): Variable bytes VARCHAR(n): Variable bytes All (n) specifications are optional CHAR(1)  Given ‘s’, displays s CHAR(1)  Given ‘sample’, displays ‘s’ CHAR(10)  Given ‘sample’, displays ‘sample’ Prefer VARCHAR to CHAR

6 Special Notes You can use DML and DDL commands in the console, query browser, and within an SQL script file Transactions (BEGIN, ROLLBACK, and COMMIT) do not work with DDL commands. Do not try to memorize all these commands Use templates After a while, you’ll remember them

7 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

8 CREATE TABLE Example Example: CREATE TABLE Musician ( Title 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’);

9 CREATE TABLE Example CREATE TABLE can also take an optional ENGINE parameter at the end (MySQL only): CREATE TABLE Musician ( Title VARCHAR(32) NOT NULL ) Engine=InnoDB; MySQL uses pluggable storage engines Default is MyISAM Another popular one is InnoDB Currently only InnoDB supports foreign keys Many uncommon ones are listed herehere

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

11 Dropping Tables When dropping tables (in MySQL only), it is useful to type in the following foreign key check commands when using the InnoDB storage engine SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS tablename; SET FOREIGN_KEY_CHECKS = 1;

12 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;

13 Adding Columns 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’);

14 Dropping Columns Syntax: ALTER TABLE tablename DROP COLUMN column_name; Examples: ALTER TABLE Musician DROP COLUMN Type; ALTER TABLE Musician DROP COLUMN MusicianID;

15 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

16 Editing Columns 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;

17 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.

18 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,...);

19 Examples Examples: CREATE TABLE Musician( Instrument INT NOT NULL, CONSTRAINT fk_musician PRIMARY KEY(Instrument) ); CREATE TABLE Musician( Instrument INT NOT NULL PRIMARY KEY ); ALTER TABLE Musician ADD CONSTRAINT fk_musician PRIMARY KEY(Instrument);

20 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,...);

21 Foreign Key Constraints Example: -- Assign the proper foreign key to the taglines table. ALTER TABLE Taglines ADD CONSTRAINT fk_taglines FOREIGN KEY(MovieID) REFERENCES Movies(MovieID); -- Now try adding an invalid tagline. INSERT INTO Taglines VALUES(1029837, ‘Some tagline goes here.’);

22 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,...);

23 Unique Constraints 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’);

24 Default Constraints Using CREATE 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;

25 Default Constraints 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 100.00;

26 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;

27 Examples Examples: -- Drop the fk_taglines foreign key in the Taglines table that we added previously. ALTER TABLE Taglines DROP FOREIGN KEY fk_taglines; -- Drop the primary key in the Genres table. ALTER TABLE Genres DROP PRIMARY KEY; -- Drop the uq_username unique key in the login table. ALTER TABLE login DROP KEY uq_username;

28 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

29 Identity Column Example 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 Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Definition Language."

Similar presentations


Ads by Google