Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language (Data definition Language)

Similar presentations


Presentation on theme: "Structured Query Language (Data definition Language)"— Presentation transcript:

1 Structured Query Language (Data definition Language)
SQL - DDL Structured Query Language (Data definition Language)

2 Data Definition Language (SQL)
A data definition language or data description language (DDL) is a syntax similar to a computer programming language for defining data structures, especially database schemasD SQL (Structured Query Language) – has a confusing name because of the word Query. SQL – DML (Data Manipulation Language) allows the user/administrator to add, delete, modify and query data that is already in database tables. SQL – DDL (Data Definition Language) allows the user/administrator to create, delete and change the structure of the tables and there inter-connection. DDL doesn’t evolve querying and DML does a lot more that just querying. However SQL is the combination of DML and DDL.

3 ER – diagram

4 Creating A Table CREATE TABLE members ( Comments
userID varchar(40) NOT NULL, password varchar(20) NOT NULL, first varchar(20) NOT NULL, last varchar(30) NOT NULL, roles int, UNIQUE (userID), PRIMARY KEY (userID) )ENGINE=InnoDB; Comments Making a field a primary key makes it UNIQUE and NOT NULL automatically so the NOT NULL and UNIQUE is overkill.

5 Foreign Key (total participation)
CREATE TABLE membersChallangeQuestions ( memberID varchar(40) , question varchar(100) NOT NULL, answer varchar(50) NOT NULL, FOREIGN KEY (memberID) REFERENCES members(userID), PRIMARY KEY (memberID) )ENGINE=InnoDB; Comments FOREIGN KEY requires that the memberID can only contain values that are in the userID column of the MEMBERS table. To delete a member with a challenge question would now require the question get deleted first. This table must be created after the members table.

6 Alter an existing Table’s definition
ALTER TABLE members ADD middle varchar(20); // add a middle name with 20 characters ALTER TABLE members ADD middle varchar(20) AFTER first; ALTER TABLE members ADD middle varchar(20) BEFORE last; ALTER TABLE members ALTER middle varchar(30); // change middle name to 30 characters ALTER TABLE members DROP middle; // get rid of middle name

7 Rename and create Like Table
CREATE TABLE newMembers LIKE members // create an empty table with same attributes CREATE TABLE allMembers LIKE members // create a table with same attributes AS ( SELECT * FROM members ORDER BY last) // and populate with ordered data records RENAME TABLE members TO oldMembers // archive members into old members

8 Change the name of a column with FK’s
ALTER TABLE members RENAME COLUMN userID to memberID // This will fail because membersChallangeQuestion required the userID field // We (the data base administrator DBA) need to do this in a few steps ALTER TABLE `memberschallangequestions` DROP FOREIGN KEY IF EXISTS `object_ibfk_1` ALTER TABLE membersChallangeQuestions ADD FOREIGN KEY (memberid) REFERENCES members(memberID)

9 M-N (many to many relation)
CREATE TABLE membersPhones ( memberID varchar(40) NOT NULL, phoneID varchar(20) NOT NULL, FOREIGN KEY (memberID) REFERENCES members(userID), FOREIGN KEY (phoneID) REFERENCES phones(phoneID), PRIMARY KEY (memberID, phoneID), )ENGINE=InnoDB; Comments A new table to allow for the diamond with M-N relationship connecting members  phones Since the combination of memberID and phoneID is a Primary Key, duplicates pairs and NULLs will not be allowed.

10 Truncate a table TRUNCATE TABLE oldMembers // SQL-DDL will delete all rows in the oldMembers table DELETE FROM oldMembers // SQL-DML will delete all rows in the oldMembers table TRUNCATE is much faster than DELETE which will delete each row one at a time, checking for constraints, recalculating indexes each time etc.


Download ppt "Structured Query Language (Data definition Language)"

Similar presentations


Ads by Google