Download presentation
Presentation is loading. Please wait.
1
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language
2
Topics for Today 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)
3
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
4
Data Types TINYINT(w): 1 byte SMALLINT(w): 2 bytes MEDIUMINT(w): 3 bytes INT(w): 4 bytes BIGINT(w): 8 bytes FLOAT(n, d): 4 bytes REAL(n, d): 8 bytes DATE: 3 bytes CHAR(n) and VARCHAR(n): Variable bytes
5
CREATE TABLE Example Example CREATE TABLE tablename1 ( field1 int not null, field2 int not null );
6
Dropping Tables Syntax: DROP TABLE [IF EXISTS] tablename; Example: DROP TABLE Artists; NOTE: ROLLBACK doesn't work with DROP TABLE
7
Adding Columns Syntax: ALTER TABLE tablename ADD fieldname datatype NULL | NOT NULL [FIRST | AFTER] column_name In other words: ALTER TABLE tablename ADD column_definition; Example: ALTER TABLE tablename1 ADD inbetween INT NULL AFTER field1;
8
Dropping Columns Syntax: ALTER TABLE tablename DROP COLUMN column_name;
9
Editing Columns Syntax: ALTER TABLE tablename MODIFY fieldname column_definition; ALTER TABLE tablename CHANGE old_fieldname new_fieldname column_definition
10
Editing Columns Examples: ALTER TABLE tablename1 CHANGE field1 newfield1 INT NULL; ALTER TABLE tablename1 MODIFY newfield1 VARCHAR(25) NULL;
11
Identity Columns Purpose is to auto-generate primary key values MySQL uses the non-standard keyword AUTO_INCREMENT and you must define the primary key SQL standard uses GENERATE keyword Syntax: fieldname datatype NOT NULL AUTO_INCREMENT
12
Identity Column Example Example: CREATE TABLE tablename1 (field1 INT NOT NULL AUTO_INCREMENT, field2 INT NULL, PRIMARY KEY(field1)); INSERT INTO tablename1(field2) VALUES(100); INSERT INTO tablename1(field2) VALUES(101); INSERT INTO tablename1(field2) VALUES(102); INSERT INTO tablename1(field2) VALUES(103); INSERT INTO tablename1(field2) VALUES(104);
13
Constraints 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 (in the column of the primary key). Default Constraints: Data not specifically inserted will take on default values.
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 tablename1( field1 INT NOT NULL PRIMARY KEY, field2 INT NULL ); CREATE TABLE tablename1( field1 INT NOT NULL, field2 INT NOT NULL, CONSTRAINT pk_name PRIMARY KEY(field1, field2) );
16
Foreign Key Constraints Sometimes called integrity constraints CREATE TABLE Syntax: FOREIGN KEY fk_name (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 Examples: CREATE TABLE tablename2( field1 INT NOT NULL PRIMARY KEY, field2 INT NULL, FOREIGN KEY fk_field2 (field2) REFERENCES tablename1 (field1) ); -- Doesn't work!!! (see page 204) 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
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;
20
Examples Examples: CREATE TABLE tablename1 ( field1 INT NOT NULL, field2 INT DEFAULT 10 NULL, PRIMARY KEY(field1) ); INSERT INTO tablename1(field1) VALUES(100); ALTER TABLE tablename1 ALTER field2 SET DEFAULT 1000;
21
Dropping Constraints Syntax: ALTER TABLE tablename DROP CONSTRAINT constraint_name; Example: -- Let's drop a foreign key constraint.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.