Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS122 Using Relational Databases and SQL

Similar presentations


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

1 CS122 Using Relational Databases and SQL
10/22/17 10/16/17 CS122 Using Relational Databases and SQL 8. Data Manipulation and Definition Daniel Firpo Department of Computer Science California State University, Los Angeles 1

2 Outline Data manipulation Data definition INSERT DELETE UPDATE
10/22/17 10/16/17 Outline Data manipulation INSERT DELETE UPDATE Data definition CREATE a table MODIFY a table DELETE a table 8. Data Manipulation & Definition CS1222_F2018 8-2 2

3 Database Definition Language (DDL)
1/17/2019 Database Definition Language (DDL) Subset of SQL commands to create and modify database objects Can create an entire database using SQL rather than a graphical user interface With DDL you gain more control over exactly what database objects are built and what they are named DDL saved into a script can be easily ported from one database installation to another 8. Data Manipulation & Definition CS1222_F2018

4 8. Data Manipulation & Definition CS1222_F2018
1/17/2019 Naming Tips Use consistent names Do not include spaces inside your table or fields Avoid the use of SQL reserved words and punctuation marks in table and field names  Keep names short, but long enough to be descriptive Be consistent in your use of abbreviations Name primary key and foreign key fields consistently Avoid generic field names, such as ID or Date 8. Data Manipulation & Definition CS1222_F2018

5 Databases MySQL Instance of MySQL running on a server computer can contain multiple databases New MySQL database can be created with Create Database followed by the name to be used for the new database or Create Database If Not Exists followed by the name of the database There are no other options for the Create Database command Can switch between databases by typing Use followed by the database name

6 8. Data Manipulation & Definition CS1222_F2018
1/17/2019 Creating a Table Null means that the field may contain null (empty) values. Not Null means that the field cannot contain null values. Syntax CREATE TABLE tablename ( Fieldname1 DataType NULL | NOT NULL, Fieldname2 DataType NULL | NOT NULL ) Please refer to earlier slides for datatypes in MySQL 8. Data Manipulation & Definition CS1222_F2018

7 Creating a Table: Example
1/17/2019 Create a table called Contracts with the ArtistID from the Artists table and a ContractDate field CREATE TABLE tblContracts ( ArtistID Integer NOT NULL, ContractDate DateTime NOT NULL ) A left parenthesis comes before the first field in the list. Each field entry except the last one is followed by a comma. The last field in the list is followed by a right parenthesis. 8. Data Manipulation & Definition CS1222_F2018

8 8. Data Manipulation & Definition CS1222_F2018
1/17/2019 Dropping a Table Syntax DROP Table tablename Example: Remove the tblContracts table from the database DROP Table tblContracts 8. Data Manipulation & Definition CS1222_F2018

9 Adding Columns to an Existing Table
1/17/2019 Adding Columns to an Existing Table Syntax ALTER TABLE tablename ADD fieldname DataType NULL | NOT NULL 8. Data Manipulation & Definition CS1222_F2018

10 Adding Columns to an Existing Table
1/17/2019 Adding Columns to an Existing Table Add a text field called ContractType with a length of 10 to the tblContracts table ALTER TABLE tblContracts ADD ContractType VarChar(10) NULL 8. Data Manipulation & Definition CS1222_F2018

11 Altering Existing Columns in a Table
1/17/2019 Altering Existing Columns in a Table Syntax ALTER TABLE tablename MODIFY fieldname DataType NULL | NOT NULL 8. Data Manipulation & Definition CS1222_F2018

12 Altering Existing Columns in a Table: Example
1/17/2019 Altering Existing Columns in a Table: Example Change the length on the ContractType field to 15 in the tblContracts table ALTER TABLE tblContracts MODIFY ContractType VARCHAR(15) NULL 8. Data Manipulation & Definition CS1222_F2018

13 8. Data Manipulation & Definition CS1222_F2018
1/17/2019 Dropping a Column Syntax ALTER TABLE tablename DROP COLUMN columnname Example: Remove the Contracttype column from the tblContracts table ALTER TABLE tblContracts DROP COLUMN ContractType 8. Data Manipulation & Definition CS1222_F2018

14 Identity Columns and Sequences
1/17/2019 Identity Columns and Sequences Techniques to automatically generate a sequential number with each row that is inserted Never have to insert a value to it, but it will always be unique within that table Make excellent primary keys in cases where there is no naturally occurring primary key 8. Data Manipulation & Definition CS1222_F2018

15 Defining an Identity Column
1/17/2019 Defining an Identity Column CREATE TABLE tblX ( ID INT auto_increment Primary key, anotherfield int NULL ) 8. Data Manipulation & Definition CS1222_F2018

16 Sequences Vs. Guid Columns
An identity column (or sequence) is unique within a given a table in a given database on a given server With distributed database, identity column may not\be unique across every instance of the table One solution is to use the identity column in conjunction with a locationID as the primary key Another solution Globally Unique Identifier (GUID), which is supported in some databases A GUID is a 16-byte ID guaranteed to be unique across every instance of a distributed database GUIDs are cumbersome to work with: C1C5FCE2-879E-4A96-B845- 9F3FC4A10EC8


Download ppt "CS122 Using Relational Databases and SQL"

Similar presentations


Ads by Google