0% found this document useful (0 votes)
85 views8 pages

Ass 1 Database Design and Development

SQL is a standard language for interacting with relational database management systems that allows users to create, modify, retrieve, and delete data from databases. SQL commands fall into several categories including DDL for defining database schema, DML for manipulating data, DCL for controlling access, DQL for querying data, and TCL for managing transactions. Common SQL statements include SELECT to query data, INSERT and UPDATE to modify data, CREATE and ALTER to manage database and table structure, and DELETE to remove data.

Uploaded by

ruthykemunto111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views8 pages

Ass 1 Database Design and Development

SQL is a standard language for interacting with relational database management systems that allows users to create, modify, retrieve, and delete data from databases. SQL commands fall into several categories including DDL for defining database schema, DML for manipulating data, DCL for controlling access, DQL for querying data, and TCL for managing transactions. Common SQL statements include SELECT to query data, INSERT and UPDATE to modify data, CREATE and ALTER to manage database and table structure, and DELETE to remove data.

Uploaded by

ruthykemunto111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPLORE SQL AND GIVE ANY OTHER LANGUAGE THAT IS USED IN THE DATABASE

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

CREATE DATABASE my_Ruth;

This command creates a new database named “my_Ruth”

 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

CREATE TABLE Student (

StudentID int,

LastName varchar(255),

FirstName varchar(255),

Address varchar(255),

City varchar(255),

Country 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 ,.

INSERT INTO muna (StudentID, LastName, FirstName, Address, City, Country)

VALUES

(5, 'Alfred', 'Summy', 'Mexico05097', 'Mexico'),

(6, 'Johnstone', 'Kingsley', 'Liverpool', 'L10AA', 'UK'),


(4, 'Kenny', 'Wallen', 'Utawala0055', 'Nairobi', 'Kenya');

 Querying Data:
SQL's primary purpose is to query data. The SELECT statement is used to select data from a
database. For example

SELECT * FROM Student;

The query retrieves all records from the student table

 Select Used to return specified columns in a table

SELECT column1, column2, ...

FROM table_name;

 SELECT * is used to return all columns in a table

 The SQL SELECT DISTINCT Statement

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.

SELECT DISTINCT Syntax

SELECT DISTINCT column1, column2, ...

 Deleting Data:
SQL allows you to delete records from a table using the DELETE FROM statement: For example

delete from muna

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;

 The SQL WHERE Clause


The WHERE clause is used to filter records.It is used to extract only those records that fulfill a
specified condition.

WHERE Syntax;

SELECT column1, column2, ...

FROM table_name

WHERE condition;

 WHERE Clause Example

The following SQL statement selects all the students from the city “Nairobi", in the "students" table:

Example

SELECT * FROM Student

WHERE city ='Nairobi';

 Using where clause with Text Fields vs. Numeric Fields

SQL requires single quotes around text values. However, numeric fields should not be enclosed in
quotes:

Example

SELECT * FROM Student

WHERE StudentID =1;

 SQL ALTER TABLE Statement


The ALTER TABLE statement adds, deletes, or modifies columns in an existing table.
 ALTER TABLE - ADD Column

To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;
The following SQL adds a "country" column to the "students" table:
Example
ALTER TABLE Student
ADD county varchar (255);

 ALTER TABLE - RENAME COLUMN


To rename a column in a table, use the following syntax:
ALTER TABLE table name
RENAME COLUMN old name to new_name;

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.

 SQL null values


What is a NULL Value?
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a
value to this field. Then, the field will be saved with a NULL value.
To test null values use syntax
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
For example
SELECT LastName, Country, Address
FROM Student
WHERE CourseID IS NULL;

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

Figure 2 tables in my database”my_Ruth”

 Languages used with SQL / Types of SQL


SQL Commands are divided into five broad categories – DDL, DML, DCL, TCL, and
DQL.
Data Definition Language (DDL)
It deals with the structure (schema) of a database, including the creation, modification, and deletion of
database objects. Examples of DDL statements are CREATE DATABASE, ALTER DATABASE,
CREATE TABLE, ALTER TABLE
Data manipulation language
It focuses on manipulating and retrieving data stored in a relational database management system
(RDBMS). The statements include SELECT, DELETE, INSERT INTO, and UPDATE
Data Control Language (DCL):
used to control and manage access to data within a database. DCL helps in ensuring data security and
access control within a database system. The statements used are; GRANT and REVOKE
Data Query Language (DQL)
DQL statements allow users to interact with the data stored in a database by specifying what data they
want to retrieve, how to filter it, and how it should be presented. The key statement is SELECT, others
include SORTING, GROUPING, ALIASES, and JOINING TABLES
Transaction Control Language (TCL):
TCL is used for managing transactions in the database ensuring data integrity TCL commands include
COMMIT (to save changes made during a transaction) ROLLBACK (to undo changes in a transaction),
and SAVEPOINT (to set a point within a transaction to which you can later roll back).

You might also like