SlideShare a Scribd company logo
1 of 40
BTEC- HND In Computing
Module Name: Database Design & Development
Unit Number: 4
Week 7
Creating Tables
1
HND-Database Design & Development
Unit Learning Outcomes
• LO1. Use an appropriate design tool to design a relational
database system for a substantial problem.
• LO2. Develop a fully functional relational database system,
based on an existing system design.
• LO3. Test the system against user and system requirements.
• LO4. Produce technical and user documentation.
HND-Database Design & Development 2
Objectives
BE ABLE TO IDENTIFY:
• The basic concepts and principles of SQL
• The different components of SQL
• How to use SQL (DDL) to perform basic create
operations
o Create
o Insert
o Modify
o Delete data
How to use SQL(DML) to perform basic database queries
HND-Database Design & Development 3
Relational Tables Can Answer
Many Queries
HND-Database Design & Development 4
Student Enrolment Course
•How many courses are there & what are their names?
• Which students are enrolled for Java?
• How many students take 3 or more courses?
SQL - Structured Query
Language
• SQL was developed at IBM around 1975...
• SQL is a declarative language - says what not how
• SQL is a powerful language,
• SQL is an abstract & portable interface to RDMBs
HND-Database Design & Development 5
Structured Query Language
(contd.)
3 COMPONENETS
o Data Definition Language (DDL) for creating a DB
o Data Control Language (DCL) for administering a
DB
o Data Manipulation Language (DML) to insert,
delete and modify data in a table
HND-Database Design & Development 6
SQL Syntax
SQL uses English keywords & user-defined names
HND-Database Design & Development 7
CREATE TABLE Staff (
StaffNo INTEGER,
Salary FLOAT,
Lname CHAR(20)
);
INSERT INTO Staff VALUES (32, 25000.0, 'Smith');
By convention, keywords are upper-case
Text data is enclosed using single quotes (‘ ' ‘)
Round brackets (‘(‘) are used to group related items
Commas (‘,’) separate items in a list
Statements are terminated with a semicolon (‘;’)
SQL Terminology
SQL does not use formal relational terminology
Formal Informal (SQL)
Relation Table
Tuple Row
Attribute Column
Cardinality No. of rows
Degree No. of columns
Relationships Foreign keys
Constraints Assertions
HND-Database Design & Development 8
Structured Query Language
(contd.)
• Creating tables…
o Tables can be created using CREATE TABLE
statement
o There are different data types available in SQL2
HND-Database Design & Development 9
Structured Query Language
(contd.)
• Integer (INT, INTEGER & SMALLINT)
• Real numbers (FLOAT, REAL, DOUBLE)
• Formatted numbers DECIMAL(i,j) or DEC(i,j) or NUMERIC(i,j)
• Character strings
o Fixed length: CHAR(n) or CHARACTER(n)
o Variable length: VARCHAR(n)
• Date and Time: DATE and TIME data types are supported in
SQL2
HND-Database Design & Development 10
Structured Query Language
(contd.)
Example
HND-Database Design & Development 11
CREATE TABLE Student (
name CHAR(20),
address CHAR(25),
age INTEGER,
gpa REAL
);
Structured Query Language
(contd.)
• Primary Key constraint
o PRIMARY KEY
HND-Database Design & Development 12
CREATE TABLE Student (
stdid CHAR (10) PRIMARY KEY,
name CHAR(20),
address CHAR(25),
age INTEGER,
gpa REAL
);
Structured Query Language
(contd.)
Alternative:
HND-Database Design & Development 13
CREATE TABLE Student (
stdid CHAR (10),
name CHAR(20),
address CHAR(25),
age INTEGER,
gpa REAL,
PRIMARY KEY (stid)
);
Structured Query Language
(contd.)
• Other candidate keys…
o UNIQUE - identifies an individual tuple uniquely in a relation or table. A table
can have more than one unique key unlike primary key. Unique key constraints
can accept only one NULL value for column.
HND-Database Design & Development 14
CREATE TABLE Student (
Stdid CHAR (10) PRIMARY KEY
Name CHAR(20),
Address CHAR(25),
Age INTEGER,
Gpa REAL,
NIC CHAR(10) UNIQUE);
Structured Query Language
(contd.)
• Referential Integrity Constraints
o FOREIGN KEY………REFERENCES
• Student (stdId, name ,address , age , gpa)
• Grade( subjectId, stdId , grade)
HND-Database Design & Development 15
CREATE TABLE Grade
(
subjectId CHAR(4),
stdId CHAR(10),
grade CHAR(2),
PRIMARY KEY(subjectId, stdId),
FOREIGN KEY(stdId) REFERENCES Student
)
Structured Query Language
(contd.)
• Alternative
o FOREIGN KEY
HND-Database Design & Development 16
CREATE TABLE Grade (
subjectId CHAR(4),
stdId CHAR(10) REFERENCES Student(stdId),
grade CHAR(2),
PRIMARY KEY(subjectId,stdid)
);
Structured Query Language
(contd.)
• The use of a constraint name allows identification of a
constraint which can be dropped later (using the ALTER
TABLE command)
HND-Database Design & Development 17
CREATE TABLE Grade (
subjectId CHAR(4),
stdId CHAR(10) ,
grade CHAR(2),
PRIMARY KEY(subjectId,stdid),
CONSTRAINT fk_Grade FOREIGN KEY(stdid)
REFERENCES Student(stdId)
);
Structured Query Language
(contd.)
• Specifying NOT NULL constraints…
o NOT NULL
HND-Database Design & Development 18
CREATE TABLE Student (
Name CHAR(20) NOT NULL,
Address CHAR(25),
Age INTEGER,
Gpa REAL
);
Structured Query Language
(contd.)
• Specifying default values…
o DEFAULT - is used to set a default value for a column.
o The default value will be added to all new records, if no other value is specified.
HND-Database Design & Development 19
CREATE TABLE Student (
Name CHAR(20),
Address CHAR(25) DEFAULT ‘Malabe’,
Age INTEGER,
Gpa REAL
);
Structured Query Language
(contd.)
• Specifying valid values…
o CHECK constraints - is used to limit the value range that can be placed in a
column.
HND-Database Design & Development 20
CREATE TABLE Emp (
….
age INT CHECK (age BETWEEN 0 AND 120),
….);
Structured Query Language
(contd.)
• Referential Triggered Action:
o Operations
• ON UPDATE
• ON DELETE
o Actions
• SET NULL
• CASCADE
• SET DEFAULT
HND-Database Design & Development 21
Structured Query Language
(contd.)
ON DELETE CASCADE
• It specifies that the child data is deleted when the
parent data is deleted.
ON UPDATE
• Optional. It specifies what to do with the child data
when the parent data is updated. You have the options of
NO ACTION, CASCADE, SET NULL, or SET DEFAULT.
HND-Database Design & Development 22
Structured Query Language
(contd.)
NO ACTION
• It is used in conjunction with ON DELETE or ON
UPDATE. It means that no action is performed with the
child data when the parent data is deleted or updated.
Cascade
• It is used in conjunction with ON DELETE or ON
UPDATE. It means that the child data is either deleted or
updated when the parent data is deleted or updated.
HND-Database Design & Development 23
Structured Query Language
(contd.)
SET NULL
• It is used in conjunction with ON DELETE or ON
UPDATE. It means that the child data is set to NULL
when the parent data is deleted or updated.
SET DEFAULT
• It is used in conjunction with ON DELETE or ON
UPDATE. It means that the child data is set to their
default values when the parent data is deleted or
updated.
HND-Database Design & Development 24
Structured Query Language
(contd.)
HND-Database Design & Development 25
CREATE TABLE Employee (
NIC VARCHAR(10) PRIMARY KEY,
name VARCHAR(50) UNIQUE NOT NULL,
address VARCHAR(25) DEFAULT 'Colombo',
works_in INTEGER,
CONSTRAINT fk_EmpDept FOREIGN KEY (works_in)
REFERENCES Dept ON DELETE CASCADE
ON UPDATE NO ACTION
);
Structured Query Language
(contd.)
• Dropping tables
DROP TABLE Employee [RESTRICT|CASCADE]
o RESTRICT drops if no other constraints (such as foreign
keys or views exist)
o CASCADE drops all constraints & views that reference it
* SQL Server 2000 has only RESTRICT option which is the
default
HND-Database Design & Development 26
Structured Query Language
(contd.)
• Altering tables…
o ALTER TABLE can be used to add or drop a column,
change a column definition, and adding or dropping
table constraints
The new column has null values. Hence, NOT NULL
cannot be used here
HND-Database Design & Development 27
ALTER TABLE Employee ADD Job VARCHAR(12)
Structured Query Language
(contd.)
Dropping a column…
Changing column definition…
Dropping a constraint
HND-Database Design & Development 28
ALTER TABLE Employee DROP COLUMN Job
ALTER TABLE Employee ALTER COLUMN job vachar(50)
ALTER TABLE Employee DROP fk_EmpDept
Structured Query Language
(contd.)
• We can modify data using the following three commands:
INSERT, DELETE and UPDATE
• INSERT statement:
o Inserting a single row
• *The order of values must be the same as in the
CREATE TABLE statement
HND-Database Design & Development 29
INSERT INTO Dept VALUES (1, 'Sales', 'BoC Merchant Tower')
Structured Query Language
(contd.)
Inserting to user-specified columns
• * Only the columns are specified are filled with values.
Unspecified columns are filled with NULL if there is no
default values.
HND-Database Design & Development 30
INSERT INTO Employee (NIC, name, works_in)
VALUES ('781111111V', 'Ajith Perera', 1)
Structured Query Language
(contd.)
• Inserting multiple rows…
INSERT INTO Employees
<Select statement>*
*we’ll learn the select statement in detail later
HND-Database Design & Development 31
Structured Query Language
(contd.)
• Deleting tuples from tables…
o Deleting all records
o Deleting only specified records
DELETE FROM <table>
WHERE <selection-condition>
o Completely removing a table is a DDL operation
HND-Database Design & Development 32
DELETE FROM Dept
DELETE FROM Dept WHERE dno = 2
DROP TABLE Staff
Structured Query Language
(contd.)
• Updating tuples in a table
UPDATE <table>
SET <column> = <expression>
WHERE <selection condition>
HND-Database Design & Development 33
Structured Query Language
(contd.)
• Updating all tuples
• Updating selected tuples
UPDATE Employee
SET works_in = 2
WHERE NIC = ‘781111111V’
HND-Database Design & Development 34
UPDATE Employee SET works_in = 1
Exercises…
• Consider the following schema:
Student(StudentNo:Integer; name:varchar(50),
major:char(4); GPA:float)
Write SQL statements to perform the following
1. Create the above table
2. Insert the following information:
HND-Database Design & Development 35
StudentNo Name Major GPA
1 Sampath EE 3.5
2 Nishani CSE 34
Exercises… (contd.)
3. Update Sampath’s GPA to 3.7.
4. Delete student table
5. Add a column address (i.e. address: varchar(50) ) to the
Student table.
6. Change the data type of address column into varchar(100).
7. Add a checking rule to GPA column, GPA values should
between 0 and 4.
8. Remove the check constraint added to the GPA column.
9. Create table Major(majorId , description).
10. Change student table to reflect Primary Key.
HND-Database Design & Development 36
Summary
• SQL is the standard query language for RDBMS
• Three main categories of SQL
o DDL, Data Definition Language
o DCL, Data Control Language
o DML, Data Manipulation Language
• CREATE belongs to DDL
HND-Database Design & Development 37
Next Lecture
SQL ctd..
HND-Database Design & Development 38
Q & A
HND-Database Design & Development 39
Thank You.
HND-Database Design & Development 40

More Related Content

Similar to BTEC- HND In Computing-Creating Table_Week6.pptx

L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxL1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxDIPESH30
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Data Con LA
 
Turning a Search Engine into a Relational Database
Turning a Search Engine into a Relational DatabaseTurning a Search Engine into a Relational Database
Turning a Search Engine into a Relational DatabaseMatthias Wahl
 
U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)Michael Rys
 
Data Warehouse and Business Intelligence - Recipe 2
Data Warehouse and Business Intelligence - Recipe 2Data Warehouse and Business Intelligence - Recipe 2
Data Warehouse and Business Intelligence - Recipe 2Massimo Cenci
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Citus Data
 
Database Performance
Database PerformanceDatabase Performance
Database PerformanceBoris Hristov
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
Relational Database Design Bootcamp
Relational Database Design BootcampRelational Database Design Bootcamp
Relational Database Design BootcampMark Niebergall
 

Similar to BTEC- HND In Computing-Creating Table_Week6.pptx (20)

Manipulating Data in Style with SQL
Manipulating Data in Style with SQLManipulating Data in Style with SQL
Manipulating Data in Style with SQL
 
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxL1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
 
MongoDB 3.0
MongoDB 3.0 MongoDB 3.0
MongoDB 3.0
 
Hsqldb tutorial
Hsqldb tutorialHsqldb tutorial
Hsqldb tutorial
 
Turning a Search Engine into a Relational Database
Turning a Search Engine into a Relational DatabaseTurning a Search Engine into a Relational Database
Turning a Search Engine into a Relational Database
 
Kace & SQL
Kace & SQLKace & SQL
Kace & SQL
 
lecture_34e.pptx
lecture_34e.pptxlecture_34e.pptx
lecture_34e.pptx
 
U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)U-SQL Meta Data Catalog (SQLBits 2016)
U-SQL Meta Data Catalog (SQLBits 2016)
 
Data Warehouse and Business Intelligence - Recipe 2
Data Warehouse and Business Intelligence - Recipe 2Data Warehouse and Business Intelligence - Recipe 2
Data Warehouse and Business Intelligence - Recipe 2
 
SQL
SQLSQL
SQL
 
SQL
SQL SQL
SQL
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 
Hsqldb tutorial
Hsqldb tutorialHsqldb tutorial
Hsqldb tutorial
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
PO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - SqlPO WER - Piotr Mariat - Sql
PO WER - Piotr Mariat - Sql
 
Relational Database Design Bootcamp
Relational Database Design BootcampRelational Database Design Bootcamp
Relational Database Design Bootcamp
 
Les09
Les09Les09
Les09
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

BTEC- HND In Computing-Creating Table_Week6.pptx

  • 1. BTEC- HND In Computing Module Name: Database Design & Development Unit Number: 4 Week 7 Creating Tables 1 HND-Database Design & Development
  • 2. Unit Learning Outcomes • LO1. Use an appropriate design tool to design a relational database system for a substantial problem. • LO2. Develop a fully functional relational database system, based on an existing system design. • LO3. Test the system against user and system requirements. • LO4. Produce technical and user documentation. HND-Database Design & Development 2
  • 3. Objectives BE ABLE TO IDENTIFY: • The basic concepts and principles of SQL • The different components of SQL • How to use SQL (DDL) to perform basic create operations o Create o Insert o Modify o Delete data How to use SQL(DML) to perform basic database queries HND-Database Design & Development 3
  • 4. Relational Tables Can Answer Many Queries HND-Database Design & Development 4 Student Enrolment Course •How many courses are there & what are their names? • Which students are enrolled for Java? • How many students take 3 or more courses?
  • 5. SQL - Structured Query Language • SQL was developed at IBM around 1975... • SQL is a declarative language - says what not how • SQL is a powerful language, • SQL is an abstract & portable interface to RDMBs HND-Database Design & Development 5
  • 6. Structured Query Language (contd.) 3 COMPONENETS o Data Definition Language (DDL) for creating a DB o Data Control Language (DCL) for administering a DB o Data Manipulation Language (DML) to insert, delete and modify data in a table HND-Database Design & Development 6
  • 7. SQL Syntax SQL uses English keywords & user-defined names HND-Database Design & Development 7 CREATE TABLE Staff ( StaffNo INTEGER, Salary FLOAT, Lname CHAR(20) ); INSERT INTO Staff VALUES (32, 25000.0, 'Smith'); By convention, keywords are upper-case Text data is enclosed using single quotes (‘ ' ‘) Round brackets (‘(‘) are used to group related items Commas (‘,’) separate items in a list Statements are terminated with a semicolon (‘;’)
  • 8. SQL Terminology SQL does not use formal relational terminology Formal Informal (SQL) Relation Table Tuple Row Attribute Column Cardinality No. of rows Degree No. of columns Relationships Foreign keys Constraints Assertions HND-Database Design & Development 8
  • 9. Structured Query Language (contd.) • Creating tables… o Tables can be created using CREATE TABLE statement o There are different data types available in SQL2 HND-Database Design & Development 9
  • 10. Structured Query Language (contd.) • Integer (INT, INTEGER & SMALLINT) • Real numbers (FLOAT, REAL, DOUBLE) • Formatted numbers DECIMAL(i,j) or DEC(i,j) or NUMERIC(i,j) • Character strings o Fixed length: CHAR(n) or CHARACTER(n) o Variable length: VARCHAR(n) • Date and Time: DATE and TIME data types are supported in SQL2 HND-Database Design & Development 10
  • 11. Structured Query Language (contd.) Example HND-Database Design & Development 11 CREATE TABLE Student ( name CHAR(20), address CHAR(25), age INTEGER, gpa REAL );
  • 12. Structured Query Language (contd.) • Primary Key constraint o PRIMARY KEY HND-Database Design & Development 12 CREATE TABLE Student ( stdid CHAR (10) PRIMARY KEY, name CHAR(20), address CHAR(25), age INTEGER, gpa REAL );
  • 13. Structured Query Language (contd.) Alternative: HND-Database Design & Development 13 CREATE TABLE Student ( stdid CHAR (10), name CHAR(20), address CHAR(25), age INTEGER, gpa REAL, PRIMARY KEY (stid) );
  • 14. Structured Query Language (contd.) • Other candidate keys… o UNIQUE - identifies an individual tuple uniquely in a relation or table. A table can have more than one unique key unlike primary key. Unique key constraints can accept only one NULL value for column. HND-Database Design & Development 14 CREATE TABLE Student ( Stdid CHAR (10) PRIMARY KEY Name CHAR(20), Address CHAR(25), Age INTEGER, Gpa REAL, NIC CHAR(10) UNIQUE);
  • 15. Structured Query Language (contd.) • Referential Integrity Constraints o FOREIGN KEY………REFERENCES • Student (stdId, name ,address , age , gpa) • Grade( subjectId, stdId , grade) HND-Database Design & Development 15 CREATE TABLE Grade ( subjectId CHAR(4), stdId CHAR(10), grade CHAR(2), PRIMARY KEY(subjectId, stdId), FOREIGN KEY(stdId) REFERENCES Student )
  • 16. Structured Query Language (contd.) • Alternative o FOREIGN KEY HND-Database Design & Development 16 CREATE TABLE Grade ( subjectId CHAR(4), stdId CHAR(10) REFERENCES Student(stdId), grade CHAR(2), PRIMARY KEY(subjectId,stdid) );
  • 17. Structured Query Language (contd.) • The use of a constraint name allows identification of a constraint which can be dropped later (using the ALTER TABLE command) HND-Database Design & Development 17 CREATE TABLE Grade ( subjectId CHAR(4), stdId CHAR(10) , grade CHAR(2), PRIMARY KEY(subjectId,stdid), CONSTRAINT fk_Grade FOREIGN KEY(stdid) REFERENCES Student(stdId) );
  • 18. Structured Query Language (contd.) • Specifying NOT NULL constraints… o NOT NULL HND-Database Design & Development 18 CREATE TABLE Student ( Name CHAR(20) NOT NULL, Address CHAR(25), Age INTEGER, Gpa REAL );
  • 19. Structured Query Language (contd.) • Specifying default values… o DEFAULT - is used to set a default value for a column. o The default value will be added to all new records, if no other value is specified. HND-Database Design & Development 19 CREATE TABLE Student ( Name CHAR(20), Address CHAR(25) DEFAULT ‘Malabe’, Age INTEGER, Gpa REAL );
  • 20. Structured Query Language (contd.) • Specifying valid values… o CHECK constraints - is used to limit the value range that can be placed in a column. HND-Database Design & Development 20 CREATE TABLE Emp ( …. age INT CHECK (age BETWEEN 0 AND 120), ….);
  • 21. Structured Query Language (contd.) • Referential Triggered Action: o Operations • ON UPDATE • ON DELETE o Actions • SET NULL • CASCADE • SET DEFAULT HND-Database Design & Development 21
  • 22. Structured Query Language (contd.) ON DELETE CASCADE • It specifies that the child data is deleted when the parent data is deleted. ON UPDATE • Optional. It specifies what to do with the child data when the parent data is updated. You have the options of NO ACTION, CASCADE, SET NULL, or SET DEFAULT. HND-Database Design & Development 22
  • 23. Structured Query Language (contd.) NO ACTION • It is used in conjunction with ON DELETE or ON UPDATE. It means that no action is performed with the child data when the parent data is deleted or updated. Cascade • It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is either deleted or updated when the parent data is deleted or updated. HND-Database Design & Development 23
  • 24. Structured Query Language (contd.) SET NULL • It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is set to NULL when the parent data is deleted or updated. SET DEFAULT • It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is set to their default values when the parent data is deleted or updated. HND-Database Design & Development 24
  • 25. Structured Query Language (contd.) HND-Database Design & Development 25 CREATE TABLE Employee ( NIC VARCHAR(10) PRIMARY KEY, name VARCHAR(50) UNIQUE NOT NULL, address VARCHAR(25) DEFAULT 'Colombo', works_in INTEGER, CONSTRAINT fk_EmpDept FOREIGN KEY (works_in) REFERENCES Dept ON DELETE CASCADE ON UPDATE NO ACTION );
  • 26. Structured Query Language (contd.) • Dropping tables DROP TABLE Employee [RESTRICT|CASCADE] o RESTRICT drops if no other constraints (such as foreign keys or views exist) o CASCADE drops all constraints & views that reference it * SQL Server 2000 has only RESTRICT option which is the default HND-Database Design & Development 26
  • 27. Structured Query Language (contd.) • Altering tables… o ALTER TABLE can be used to add or drop a column, change a column definition, and adding or dropping table constraints The new column has null values. Hence, NOT NULL cannot be used here HND-Database Design & Development 27 ALTER TABLE Employee ADD Job VARCHAR(12)
  • 28. Structured Query Language (contd.) Dropping a column… Changing column definition… Dropping a constraint HND-Database Design & Development 28 ALTER TABLE Employee DROP COLUMN Job ALTER TABLE Employee ALTER COLUMN job vachar(50) ALTER TABLE Employee DROP fk_EmpDept
  • 29. Structured Query Language (contd.) • We can modify data using the following three commands: INSERT, DELETE and UPDATE • INSERT statement: o Inserting a single row • *The order of values must be the same as in the CREATE TABLE statement HND-Database Design & Development 29 INSERT INTO Dept VALUES (1, 'Sales', 'BoC Merchant Tower')
  • 30. Structured Query Language (contd.) Inserting to user-specified columns • * Only the columns are specified are filled with values. Unspecified columns are filled with NULL if there is no default values. HND-Database Design & Development 30 INSERT INTO Employee (NIC, name, works_in) VALUES ('781111111V', 'Ajith Perera', 1)
  • 31. Structured Query Language (contd.) • Inserting multiple rows… INSERT INTO Employees <Select statement>* *we’ll learn the select statement in detail later HND-Database Design & Development 31
  • 32. Structured Query Language (contd.) • Deleting tuples from tables… o Deleting all records o Deleting only specified records DELETE FROM <table> WHERE <selection-condition> o Completely removing a table is a DDL operation HND-Database Design & Development 32 DELETE FROM Dept DELETE FROM Dept WHERE dno = 2 DROP TABLE Staff
  • 33. Structured Query Language (contd.) • Updating tuples in a table UPDATE <table> SET <column> = <expression> WHERE <selection condition> HND-Database Design & Development 33
  • 34. Structured Query Language (contd.) • Updating all tuples • Updating selected tuples UPDATE Employee SET works_in = 2 WHERE NIC = ‘781111111V’ HND-Database Design & Development 34 UPDATE Employee SET works_in = 1
  • 35. Exercises… • Consider the following schema: Student(StudentNo:Integer; name:varchar(50), major:char(4); GPA:float) Write SQL statements to perform the following 1. Create the above table 2. Insert the following information: HND-Database Design & Development 35 StudentNo Name Major GPA 1 Sampath EE 3.5 2 Nishani CSE 34
  • 36. Exercises… (contd.) 3. Update Sampath’s GPA to 3.7. 4. Delete student table 5. Add a column address (i.e. address: varchar(50) ) to the Student table. 6. Change the data type of address column into varchar(100). 7. Add a checking rule to GPA column, GPA values should between 0 and 4. 8. Remove the check constraint added to the GPA column. 9. Create table Major(majorId , description). 10. Change student table to reflect Primary Key. HND-Database Design & Development 36
  • 37. Summary • SQL is the standard query language for RDBMS • Three main categories of SQL o DDL, Data Definition Language o DCL, Data Control Language o DML, Data Manipulation Language • CREATE belongs to DDL HND-Database Design & Development 37
  • 38. Next Lecture SQL ctd.. HND-Database Design & Development 38
  • 39. Q & A HND-Database Design & Development 39
  • 40. Thank You. HND-Database Design & Development 40