SlideShare a Scribd company logo
Introduction to
Database Systems
An overview of last lecture
• Structured Query Language (SQL)
• Manipulating Databases Using SQL
Sequence [TodaysAgenda]
Content of Lecture
• Manipulating Tables Using SQL
Manipulating Tables Using SQL
Select a database
Tables can only exist within a database. Therefore one
needs to select a database first before one can
perform table manipulation. The SQL syntax is:
For example, to select a database named “Amazon”
USE <database_name>;
USE Amazon;
Manipulating Tables Using SQL
With a database selected, all the table
manipulation using SQL would now occur
within the selected database
Create a table List the tables Examine a table
Alter an existing
table
Delete a table
Manipulating Tables Using SQL
Create a table
SQL syntax:
CREATE TABLE <table_name>
(
<column_name> <data_type>,
<column_name> <data_type>,
…
<column_name> <data_type>
);
No comma for the
last column
A column name cannot
have any spaces
Manipulating Tables Using SQL
To store text in a column, choose from the following
data types
Data Type Meaning Example
CHAR(n) If all the data have the same
number of characters, use
CHAR(n)
E.g. Gender CHAR(1)
The data is either 'M' or 'F',
both are 1 character
VARCHAR(n) If all the data have different
number of characters, use
VARCHAR(n)
E.g. LastName VARCHAR(20)
The data cannot have more
than 20 characters
Create a table
Manipulating Tables Using SQL
To store text in a column, choose from the following
data types
Data Type Meaning Example
ENUM(valid_text1
, valid_text2…)
Provide a list of allowed
text for a column. Any
text not listed will result
in error
E.g. DepartmentName
ENUM('Information Technology',
'Business', 'Engineering')
Only three valid pieces of text is
allowed: Information Technology,
Business and Engineering
Create a table
Every allowed text needs to
be enclosed in quotes ('')
Manipulating Tables Using SQL
To store numbers in a column, choose from the
following data types
Data Type Meaning Example
INT Column can store any integer
from that is between
-2147483648 to 2147483648
E.g. Quantity INT
DECIMAL(m, n) Column can store m digits with
n of the digits being after the
decimal point (.)
E.g. Salary DECIMAL(5, 2)
The input data must be
between -999.99 and 999.99
Create a table
Manipulating Tables Using SQL
To store numbers in a column, choose from the
following data types
Data Type Meaning Example
INT Column can store any integer
from that is between
-2147483648 to 2147483648
E.g. Quantity INT
DECIMAL(m, n) Column can store m digits with
n of the digits being after the
decimal point (.)
E.g. Salary DECIMAL(5, 2)
The input data must be
between -999.99 and 999.99
Create a table
There are 10 digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Manipulating Tables Using SQL
To store date and time in a column, choose from the
following data types
Data Type Meaning Example
DATE Column can store date that is in
the format of ‘year-month-day’
E.g. Birthdate DATE
Data could be '1998-09-04'
DATETIME Column can store both date and
time that is in the format of ‘year-
month-day hour:minute:second’
E.g. Arrival DATETIME
Data could be '2011-11-14
09:41:30'
Create a table
Manipulating Tables Using SQL
For example, to create a “Book” table with the
following columns
Column Name Data Type
ISBN Integer
Title
Characters (No more than 100
characters)
Quantity Integer
Create a table
The SQL would be as follows
If you execute this SQL, a table named “Book” with 3
columns would be created
CREATE TABLE Book
(
ISBN INT,
Title VARCHAR(100),
Quantity INT
);
Manipulating Tables Using SQL
Create a table
SQL syntax:
For example, to see a list of all the tables that have
been created within the selected database
Manipulating Tables Using SQL
List the tables
SHOW TABLES;
SHOW TABLES;
SQL syntax:
DESCRIBE provides information about the columns of
the table including
Column name, data type, whether the column
accepts null or not, is the column a primary
key, etc.
Manipulating Tables Using SQL
Examine a table
DESCRIBE <table_name>;
For example, to figure out the data type assigned to
each column of the “Book” table
Output of executing the SQL
Manipulating Tables Using SQL
DESCRIBE Book;
Field Type Null …
ISBN int(11) YES …
Quantity int(11) YES …
Title varchar(100) YES …
Examine a table
Metadata
There are three ways that one can alter an
existing table
Manipulating Tables Using SQL
Alter an existing table
Altering Table
Add a column
Alter a column
Delete a column
SQL syntax:
For example, to add a column named “NumOfPages”
of type INT to the “Book” table
Manipulating Tables Using SQL
Add a column
ALTER TABLE <table_name> ADD
<column_name> <data_type>;
ALTER TABLE Book ADD NumOfPages INT;
SQL syntax:
For example, to change the column named “Title” to
“Title1” with data type of VARCHAR(120)
Manipulating Tables Using SQL
Alter a column
ALTER TABLE <table_name> CHANGE
<column_name> <new_column_name>
<new_data_type>;
ALTER TABLE Book CHANGE Title Title1
VARCHAR(120);
SQL syntax:
For example, to change the data type of “Title” to
VARCHAR(120)
Manipulating Tables Using SQL
ALTER TABLE <table_name> CHANGE
<column_name> <new_column_name>
<new_data_type>;
ALTER TABLE Book CHANGE Title Title
VARCHAR(120);
Alter a column
SQL syntax:
For example, to delete the column “NumOfPages”
from the “Book” table
Manipulating Tables Using SQL
Delete a column
ALTER TABLE <table_name> DROP
<column_name>;
ALTER TABLE Book DROP NumOfPages;
SQL syntax:
For example, to delete the table named “Book”
Manipulating Tables Using SQL
Delete a table
DROP TABLE <table_name>;
DROP TABLE Book;
Manipulating Tables Using SQL
Enforcing Data Integrity Using SQL
Since data are stored in tables, MySQL therefore
enforce data integrity (i.e. data correctness) by
allowing restrictions to be placed on the tables
NOT NULL UNIQUE
PRIMARY KEY FOREIGN KEY
Manipulating Tables Using SQL
Enforcing Data Integrity Using SQL
NOT NULL
By declaring a column as NOT NULL, this ensures that
the column values cannot be NULL. SQL syntax is:
CREATE TABLE <table_name>
(
<column_name> <data_type> NOT NULL,
…
);
CREATE TABLE Book
(
ISBN INT,
Title VARCHAR(100) NOT NULL,
Quantity INT
);
Manipulating Tables Using SQL
Enforcing Data Integrity Using SQL
For example, to ensure that every book must have a non
NULL “Title”
NOT NULL
Manipulating Tables Using SQL
Enforcing Data Integrity Using SQL
To set a column to NOT NULL in an existing table, the
SQL syntax is:
For example, to set the column “Title” to NOT NULL
ALTER TABLE <table_name> CHANGE
<column_name> <new_column_name>
<new_data_type> NOT NULL;
ALTER TABLE Book CHANGE Title Title
VARCHAR(100) NOT NULL;
NOT NULL
Manipulating Tables Using SQL
Enforcing Data Integrity Using SQL
UNIQUE
By declaring a column as UNIQUE, this ensures that
every column value is different. SQL syntax is:
CREATE TABLE <table_name>
(
<column_name> <data_type> UNIQUE,
…
);
CREATE TABLE Book
(
ISBN INT,
Title VARCHAR(100) NOT NULL UNIQUE,
Quantity INT
);
Enforcing Data Integrity Using SQL
For example, to ensure that every book has a non NULL
and unique title
You can have
multiple constraints
per column
UNIQUE
Manipulating Tables Using SQL
Manipulating Tables Using SQL
Enforcing Data Integrity Using SQL
To set a column to UNIQUE in an existing table, the SQL
syntax is:
For example, to set the column “Title” to UNIQUE
ALTER TABLE <table_name> CHANGE
<column_name> <new_column_name>
<new_data_type> UNIQUE;
ALTER TABLE Book CHANGE Title Title
VARCHAR(100) UNIQUE;
UNIQUE
30

More Related Content

PDF
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
PDF
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
DOCX
PDF
MySQL for beginners
PDF
Database management system unit 1 Bca 2-semester notes
PDF
DBMS.pdf
PPTX
sql.pptx
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
MySQL for beginners
Database management system unit 1 Bca 2-semester notes
DBMS.pdf
sql.pptx
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...

Similar to Lecture 14 Database.ppt (20)

PDF
DBMS unit-3.pdf
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
PPTX
Unit - II.pptx
PDF
Dms 22319 micro project
PPTX
DBMS Part-3.pptx
PPTX
sql12.pptxsql12.pptxsql12.pptxsql12.pptx
PPT
Introduction to structured query language (sql)
PPT
chapter 8 SQL.ppt
PDF
Complete SQL Tutorial In Hindi By Rishabh Mishra (Basic to Advance).pdf
PPTX
MySQL Essential Training
PPTX
SQL.pptx for the begineers and good know
DOCX
SQL Tutorial for BCA-2
PPTX
SQL: Data Definition Language(DDL) command
PPTX
SQL.pptx
PDF
Chapter – 6 SQL Lab Tutorial.pdf
PDF
Sql12
PDF
SQL for data scientist And data analysist Advanced
DOCX
unit-5 sql notes.docx
PDF
MySQL notes - Basic Commands and Definitions
PDF
Intruduction to SQL.Structured Query Language(SQL}
DBMS unit-3.pdf
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
Unit - II.pptx
Dms 22319 micro project
DBMS Part-3.pptx
sql12.pptxsql12.pptxsql12.pptxsql12.pptx
Introduction to structured query language (sql)
chapter 8 SQL.ppt
Complete SQL Tutorial In Hindi By Rishabh Mishra (Basic to Advance).pdf
MySQL Essential Training
SQL.pptx for the begineers and good know
SQL Tutorial for BCA-2
SQL: Data Definition Language(DDL) command
SQL.pptx
Chapter – 6 SQL Lab Tutorial.pdf
Sql12
SQL for data scientist And data analysist Advanced
unit-5 sql notes.docx
MySQL notes - Basic Commands and Definitions
Intruduction to SQL.Structured Query Language(SQL}
Ad

More from ssuserd527bb (6)

PPTX
Social Stratification (1).pptx
PPTX
Acculturation vs Assimilation.pptx
PPTX
Role and Status (1).pptx
PPTX
Social Institutions.pptx
PPTX
madiha.pptx
PPT
Lec 1 Database.ppt
Social Stratification (1).pptx
Acculturation vs Assimilation.pptx
Role and Status (1).pptx
Social Institutions.pptx
madiha.pptx
Lec 1 Database.ppt
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Introduction to Artificial Intelligence
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
System and Network Administraation Chapter 3
PPT
Introduction Database Management System for Course Database
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Digital Systems & Binary Numbers (comprehensive )
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction to Artificial Intelligence
VVF-Customer-Presentation2025-Ver1.9.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
System and Network Administraation Chapter 3
Introduction Database Management System for Course Database
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
CHAPTER 2 - PM Management and IT Context
Which alternative to Crystal Reports is best for small or large businesses.pdf
Reimagine Home Health with the Power of Agentic AI​
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PTS Company Brochure 2025 (1).pdf.......
Designing Intelligence for the Shop Floor.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Digital Systems & Binary Numbers (comprehensive )

Lecture 14 Database.ppt

  • 2. An overview of last lecture • Structured Query Language (SQL) • Manipulating Databases Using SQL
  • 3. Sequence [TodaysAgenda] Content of Lecture • Manipulating Tables Using SQL
  • 4. Manipulating Tables Using SQL Select a database Tables can only exist within a database. Therefore one needs to select a database first before one can perform table manipulation. The SQL syntax is: For example, to select a database named “Amazon” USE <database_name>; USE Amazon;
  • 5. Manipulating Tables Using SQL With a database selected, all the table manipulation using SQL would now occur within the selected database Create a table List the tables Examine a table Alter an existing table Delete a table
  • 6. Manipulating Tables Using SQL Create a table SQL syntax: CREATE TABLE <table_name> ( <column_name> <data_type>, <column_name> <data_type>, … <column_name> <data_type> ); No comma for the last column A column name cannot have any spaces
  • 7. Manipulating Tables Using SQL To store text in a column, choose from the following data types Data Type Meaning Example CHAR(n) If all the data have the same number of characters, use CHAR(n) E.g. Gender CHAR(1) The data is either 'M' or 'F', both are 1 character VARCHAR(n) If all the data have different number of characters, use VARCHAR(n) E.g. LastName VARCHAR(20) The data cannot have more than 20 characters Create a table
  • 8. Manipulating Tables Using SQL To store text in a column, choose from the following data types Data Type Meaning Example ENUM(valid_text1 , valid_text2…) Provide a list of allowed text for a column. Any text not listed will result in error E.g. DepartmentName ENUM('Information Technology', 'Business', 'Engineering') Only three valid pieces of text is allowed: Information Technology, Business and Engineering Create a table Every allowed text needs to be enclosed in quotes ('')
  • 9. Manipulating Tables Using SQL To store numbers in a column, choose from the following data types Data Type Meaning Example INT Column can store any integer from that is between -2147483648 to 2147483648 E.g. Quantity INT DECIMAL(m, n) Column can store m digits with n of the digits being after the decimal point (.) E.g. Salary DECIMAL(5, 2) The input data must be between -999.99 and 999.99 Create a table
  • 10. Manipulating Tables Using SQL To store numbers in a column, choose from the following data types Data Type Meaning Example INT Column can store any integer from that is between -2147483648 to 2147483648 E.g. Quantity INT DECIMAL(m, n) Column can store m digits with n of the digits being after the decimal point (.) E.g. Salary DECIMAL(5, 2) The input data must be between -999.99 and 999.99 Create a table There are 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • 11. Manipulating Tables Using SQL To store date and time in a column, choose from the following data types Data Type Meaning Example DATE Column can store date that is in the format of ‘year-month-day’ E.g. Birthdate DATE Data could be '1998-09-04' DATETIME Column can store both date and time that is in the format of ‘year- month-day hour:minute:second’ E.g. Arrival DATETIME Data could be '2011-11-14 09:41:30' Create a table
  • 12. Manipulating Tables Using SQL For example, to create a “Book” table with the following columns Column Name Data Type ISBN Integer Title Characters (No more than 100 characters) Quantity Integer Create a table
  • 13. The SQL would be as follows If you execute this SQL, a table named “Book” with 3 columns would be created CREATE TABLE Book ( ISBN INT, Title VARCHAR(100), Quantity INT ); Manipulating Tables Using SQL Create a table
  • 14. SQL syntax: For example, to see a list of all the tables that have been created within the selected database Manipulating Tables Using SQL List the tables SHOW TABLES; SHOW TABLES;
  • 15. SQL syntax: DESCRIBE provides information about the columns of the table including Column name, data type, whether the column accepts null or not, is the column a primary key, etc. Manipulating Tables Using SQL Examine a table DESCRIBE <table_name>;
  • 16. For example, to figure out the data type assigned to each column of the “Book” table Output of executing the SQL Manipulating Tables Using SQL DESCRIBE Book; Field Type Null … ISBN int(11) YES … Quantity int(11) YES … Title varchar(100) YES … Examine a table Metadata
  • 17. There are three ways that one can alter an existing table Manipulating Tables Using SQL Alter an existing table Altering Table Add a column Alter a column Delete a column
  • 18. SQL syntax: For example, to add a column named “NumOfPages” of type INT to the “Book” table Manipulating Tables Using SQL Add a column ALTER TABLE <table_name> ADD <column_name> <data_type>; ALTER TABLE Book ADD NumOfPages INT;
  • 19. SQL syntax: For example, to change the column named “Title” to “Title1” with data type of VARCHAR(120) Manipulating Tables Using SQL Alter a column ALTER TABLE <table_name> CHANGE <column_name> <new_column_name> <new_data_type>; ALTER TABLE Book CHANGE Title Title1 VARCHAR(120);
  • 20. SQL syntax: For example, to change the data type of “Title” to VARCHAR(120) Manipulating Tables Using SQL ALTER TABLE <table_name> CHANGE <column_name> <new_column_name> <new_data_type>; ALTER TABLE Book CHANGE Title Title VARCHAR(120); Alter a column
  • 21. SQL syntax: For example, to delete the column “NumOfPages” from the “Book” table Manipulating Tables Using SQL Delete a column ALTER TABLE <table_name> DROP <column_name>; ALTER TABLE Book DROP NumOfPages;
  • 22. SQL syntax: For example, to delete the table named “Book” Manipulating Tables Using SQL Delete a table DROP TABLE <table_name>; DROP TABLE Book;
  • 23. Manipulating Tables Using SQL Enforcing Data Integrity Using SQL Since data are stored in tables, MySQL therefore enforce data integrity (i.e. data correctness) by allowing restrictions to be placed on the tables NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY
  • 24. Manipulating Tables Using SQL Enforcing Data Integrity Using SQL NOT NULL By declaring a column as NOT NULL, this ensures that the column values cannot be NULL. SQL syntax is: CREATE TABLE <table_name> ( <column_name> <data_type> NOT NULL, … );
  • 25. CREATE TABLE Book ( ISBN INT, Title VARCHAR(100) NOT NULL, Quantity INT ); Manipulating Tables Using SQL Enforcing Data Integrity Using SQL For example, to ensure that every book must have a non NULL “Title” NOT NULL
  • 26. Manipulating Tables Using SQL Enforcing Data Integrity Using SQL To set a column to NOT NULL in an existing table, the SQL syntax is: For example, to set the column “Title” to NOT NULL ALTER TABLE <table_name> CHANGE <column_name> <new_column_name> <new_data_type> NOT NULL; ALTER TABLE Book CHANGE Title Title VARCHAR(100) NOT NULL; NOT NULL
  • 27. Manipulating Tables Using SQL Enforcing Data Integrity Using SQL UNIQUE By declaring a column as UNIQUE, this ensures that every column value is different. SQL syntax is: CREATE TABLE <table_name> ( <column_name> <data_type> UNIQUE, … );
  • 28. CREATE TABLE Book ( ISBN INT, Title VARCHAR(100) NOT NULL UNIQUE, Quantity INT ); Enforcing Data Integrity Using SQL For example, to ensure that every book has a non NULL and unique title You can have multiple constraints per column UNIQUE Manipulating Tables Using SQL
  • 29. Manipulating Tables Using SQL Enforcing Data Integrity Using SQL To set a column to UNIQUE in an existing table, the SQL syntax is: For example, to set the column “Title” to UNIQUE ALTER TABLE <table_name> CHANGE <column_name> <new_column_name> <new_data_type> UNIQUE; ALTER TABLE Book CHANGE Title Title VARCHAR(100) UNIQUE; UNIQUE
  • 30. 30