Ass 1 Database Design and Development
Ass 1 Database Design and Development
INTRODUCTION TO SQL
SQL stands for Structured Query Language
SQL lets you access and manipulate databases.
It is the standard language for interacting with relational database management systems
(RDBMS) like MySQL, PostgreSQL, Oracle, Microsoft SQL Server, and SQLite
What Can SQL do?
SQL can:
retrieve data from a database
execute queries against a database
insert records in a database
update records in a database
delete records from a database
create new databases
create new tables in a database
create stored procedures in a database
create views in a database
set permissions on tables, procedures, and views
Some of The Most Important SQL Commands
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
SQL STATEMENTS
Create a database
SQL can be used to create a new database. For example
Delete Database
The drop statement is used to delete a database
Create database my_db;
DROP DATABASE my_db;
Table creation
Databases consist of one or more tables that store data in a structured format. The CREATE TABLE
statement is used to create a new table in a database. For example
StudentID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
this SQL statement creates a table named Student with columns for “StudentID” , “LastName” ,
“FirstName” , “Address” “City” ,
and “Country “
Insertion
SQL allows you to insert new records in a table using the INSERT INTO statement
To insert multiple rows of data, we use the same INSERT INTO statement, but with multiple values;
make sure you separate each set of values with a comma ,.
VALUES
Querying Data:
SQL's primary purpose is to query data. The SELECT statement is used to select data from a
database. For example
FROM table_name;
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list
the different (distinct) values.
Deleting Data:
SQL allows you to delete records from a table using the DELETE FROM statement: For example
where StudentID=3;
This deletes the students with StudentID 3 from the Student table.
Update Data
The UPDATE statement is used to modify the existing records in a table.
UPDATE Student
SET FirstName = ‘Mary'
WHERE StudentID = 1;
WHERE Syntax;
FROM table_name
WHERE condition;
The following SQL statement selects all the students from the city “Nairobi", in the "students" table:
Example
SQL requires single quotes around text values. However, numeric fields should not be enclosed in
quotes:
Example
To change the data type of a table, you can use the ALTER TABLE command with the ALTER
or MODIFY keyword. To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
Creating Relationships:
SQL enables you to establish relationships between tables using foreign keys. This helps maintain
data integrity. For instance, if you have a Student table and a Course table, you can create a
relationship like this:
ALTER TABLE Student
ADD COLUMN CourseID int);
Creating a table “course” to use to create a foreign key to “Student” table
CREATE TABLE course (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(255),
Faculty VARCHAR(255),
Year DATE,
Sem FLOAT,
Credits INT
);
INSERT INTO course (CourseID, CourseName, Faculty, Year, Sem, Credits)
VALUES
(1, 'Math 101', 'Mathematics', '2023-01-15', 1.0, 3),
(2, 'History 101', 'History', '2023-01-20', 1.0, 3),
(3, 'Biology 101', 'Biology', '2023-02-10', 2.0, 4),
-- Assuming CourseID is the common field in both tables
ALTER TABLE Student
ADD FOREIGN KEY (CourseID) REFERENCES Course(CourseID);
This ensures that the CourseID column in the Student table references the CourseID column in the
Course table.
Indexing Data:
SQL allows you to create indexes on columns to improve query performance. For example:
CREATE INDEX idx_LastName ON Student(LastName);
This creates an index on the LastName column in the Student table.
Figure 1 shows the selected columns where the “CourseID “has null values in Student table
NB:SQL is not case sensitive, select and SELECT are the same Statements