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

Slides:



Advertisements
Similar presentations
Database Chapters.
Advertisements

MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
Introduction to Structured Query Language (SQL)
1 Chapter 2: Creating and Modifying Database Tables.
Using Relational Databases and SQL
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Database Management System LICT 3011 Eyad H. Elshami.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Oracle Data Definition Language (DDL)
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 SQL Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Advanced Database Management System
SQL: DDL John Ortiz Cs.utsa.edu.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
1 SQL-2 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
1 Chapter 2: Creating and Modifying Database Objects.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
UNIVERSITAS BINA DARMA 2013 DATA DEFINITION LANGUAGE (DDL)
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language Data Definition.
Using Relational Databases and SQL
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
CS122 Using Relational Databases and SQL
Database Management System
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
CS1222 Using Relational Databases and SQL
Data Definition Language
Data Definition Language
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Instructor: Samia arshad
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
SQL (Structured Query Language)
Presentation transcript:

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

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

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

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

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

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

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

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’);

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

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

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;

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;

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’);

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

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

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;

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.

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

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

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

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( , ‘Some tagline goes here.’);

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

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’);

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;

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 ;

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;

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;

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

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’);