INFORMATION MANAGEMENT
WEEK 3:
Introduction to
Structured Query Language
OBJECTIVES:
► Introduce to SQL
► Learn the command in creating
databases
► Learn the command in creating tables
► Learn to change table structures
What is SQL?
► Structured Query Language
► Communicate with databases
► Used to create and edit databases.
► Also used to create queries, forms,
and reports
What is SQL?
► It is the language that you need to
learn for you to be able to create
a database and table, insert
records, update and delete
records.
Table
► A table is inside a database, and the data
are inside the table.
► The database must be created first, then
the table and after you create the table
you can now insert the records.
Table Basics
► A Table is an object
► Database data is stored in Tables
► Each table has a unique name
► Columns have various attributes, such as
column name and data type
► Rows contain records or data for the
columns
7
Introduction to SQL
► SQL functions fit into two broad categories:
► Data definition language
► SQL includes commands to:
► Create database objects, such as tables, indexes, and views
► Define access rights to those database objects
► Data manipulation language
► Includes commands to insert, update, delete, and retrieve
data within database tables
8
Data Definition Language
► Examine simple database model and
database tables that will form basis for
many SQL examples
► Understand data environment
► CREATE, DROP, ALTER
DATABASE DESIGN
studentNumber lastname firstname
A database and a table has its name
For naming a database add db
For naming a table add tbl
Creating Database
To create a new database, you should use the
CREATE DATABASE command which takes the following
syntax:
CREATE DATABASE databaseName;
Ex:
CREATE DATABASE dbStudents;
Check the created Database
show the list of the available databases by
running the following command:
SHOW DATABASES;
If your database is part of the list, hence the
database was created successfully.
Select Database
► For you to be able to use or work on a particular database, you
have to select it from the list of the available databases. After
selecting a database, you can perform tasks such as creating
tables within the database.
► To select a database, you should use the USE command. It takes
the syntax given below:
USE databaseName;
► The output shows that the MariaDB command prompt has changed
from none to the name of the database that has been selected.
► You can now go ahead and create tables within the Demo
database.
Creating Tables
For you to be able to create a table, you must have
selected a database. The table can be created
using the CREATE TABLE statement. Here is the
syntax for the command:
CREATE TABLE tablename
(columnname datatype);
Creating Tables cont’d
Example:
CREATE TABLE tblstudents
(studentNumber int NOT NULL,
lastname varchar(20),
firstname varchar(20),
PRIMARY KEY(studentNumber));
Creating Tables - Rules
► Table and column names must start
with a letter
► They can not exceed 30 characters
► They can not be key words such as
create, insert, select, etc.
Creating Tables - Constraints
A constraint is a rule.
Some examples constraints are:
• unique - no two entries will be the same
• not null - no entry can be blank
• **primary key - unique identification of each row**
• primary keys will be very important to you as your
knowledge of databases progresses
Data Types
Showing Table Structure
To see the structure of any particular table, you
can use the DESCRIBE command, commonly
abbreviated as DESC. It takes the following
syntax:
DESCRIBE tableName;
Week 3 Exercise 1:
Answer the following statements.
1. Create the database. The database name is
dbcitcs.
2. Create the table together with its constraints. The
table name is tblcitcs.
► Fields: Types: Contraints:
studID integer Primary key
lname string Not null
fname string Not null
course string Not Null
yr integer Not Noll
section char Not Null
3. Describe the structures of your table.
Week 3 Exercise 1:
Answer the following statements.
3. Describe the structures of your table.
Altering Table Structures
► All changes in the table structure are made by using
the ALTER TABLE command followed by a keyword
that produces the specific change you want to
make.
► Three options are available:
► ADD, MODIFY, and DROP
Altering Table Structures
► ADD to add a column
► MODIFY to change column characteristics
► DROP to delete a column from a table.
► Most RDBMSs do not allow you to delete a column
unless the column does not contain any values;
► otherwise, such an action might delete crucial data
used by other tables.
Altering Table Structures
► The basic syntax to add or modify columns is:
ALTER TABLE tablename
{ADD | MODIFY}
( columnname datatype [ {ADD | MODIFY}
columnname datatype] );
Adding a Column
► You can alter an existing table by adding one or
more columns.
ALTER TABLE tableName
ADD (columnName dataType);
Changing a Column’s Data
type
► Using the ALTER syntax, you can change the data
type of a specific column by using the following
command:
ALTER TABLE tableName
MODIFY (columnName dataType);
Note: the columnName is the name of the column you
want to change the data type.
Dropping a Column
► You can modify a table by deleting a column.
ALTER TABLE tableName
DROP columnName;
Deleting a table from the
Database
► A table can be deleted from the database using
the Drop table command.
DROP TABLE tableName;
Deleting the Database
► The database can also be deleted using the Drop
table command.
DROP DATABASE databaseName;
Week 3 Exercise 2:
Answer the following statements.
studID lname fname course yr section
1. Add a new column named ADDRESS after the Section field
Week 3 Exercise 2:
Answer the following statements.
studID lname fname course yr section addres
s
2. Add a new column named SEMESTER before studID field
Week 3 Exercise 2:
Answer the following statements.
semest studID lnam fnam cours yr sectio addre
er e e e n ss
3. Remove the course column .
Week 3 Exercise 2:
Answer the following statements.
semest studID lnam fnam yr sectio addre
er e e n ss
4. Change the datatype of lname with varchar of 100 characters.
Week 3 Exercise 2:
Answer the following statements.
5. Describe the structures of your table.