0% found this document useful (0 votes)
18 views22 pages

CC104 - Lesson 4 Part 2 Rev

The document provides SQL code snippets for creating and managing databases, tables, and records, including commands for creating databases, altering tables, inserting data, and querying records. It explains the basic structure of SQL queries, the use of logical operators, and special operators like BETWEEN, LIKE, and IN for filtering data. Additionally, it includes examples of queries to demonstrate how to retrieve specific customer information based on various conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views22 pages

CC104 - Lesson 4 Part 2 Rev

The document provides SQL code snippets for creating and managing databases, tables, and records, including commands for creating databases, altering tables, inserting data, and querying records. It explains the basic structure of SQL queries, the use of logical operators, and special operators like BETWEEN, LIKE, and IN for filtering data. Additionally, it includes examples of queries to demonstrate how to retrieve specific customer information based on various conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Review

Code used to create a database


CREATE DATABASE database_name;
Code used to active a database
USE database_name;
Code used to create table
CREATE TABLE table_name
(column_name1 data_type, column_name2 data_type,……..);

Code used to display the table structure


DESCRIBE table_name;
Code used to view all available databases and tables
SHOW databases; SHOW tables;
Code use if you want to Add Column
syntax: ALTER TABLE table_name
ADD COLUMN column_name data type;

If you want to position the column you want to add or you want to place it in the first
column
use keyword AFTER / FIRST
syntax: ALTER TABLE table_name
ADD COLUMN column_name data type AFTER column_name; OR
ADD COLUMN column_name data type FIRST;
Code when you want to modify/update the column_name
syntax:
ALTER TABLE table_name
CHANGE old_column_name new_column_name data_type;

Code when you want to change the data type of the column
syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;
Code when you want to delete column_name
syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Code used to insert data in the table


syntax:
1. INSERT INTO table_name (column_name1, column_name2,….)
VALUES(value1,value2,value3,….), (value1,value2,value3,….);

Or

2. INSERT INTO table_name


VALUES(value1,value2,value3,….), (value1,value2,value3,….);
Single Table Queries
Deleting Record

Syntax: DELETE FROM table_name


WHERE some_condition;

The SQL DELETE statement removes one or more rows from a database
table based on a condition specified in the WHERE clause.

DELETE FROM tblstudent


WHERE S_id =20042058;
Lesson 4
Basic Query Structure CC-104: Information Management

SQL is based on set and relational operations with certain modifications


and enhancements

SELECT DESIRED ATTRIBUTE

FROM ONE OR MORE TABLE

WHERE CONDITION ABOUT TUPLE OF THE TABLE


SQL queries use the SELECT statement

General form is:


SELECT A1, A2, ... FROM r1, r2, ...
WHERE P;

The SELECT statement is used to select data The WHERE clause is used to filter
from a database. records.
The data returned is stored in a result table, It is used to extract only those
called the result-set. records that fulfill a specified
condition.
Query: List the Name of the Customers with Customer No 128
Simple
Condition in
Where clause
Query:
Identify the Customer whose credit
limit is not equal to 20000.00
Using Compound condition
Logical Operator
AND Operator When the AND operator connects simple
conditions, all the simple condition must be true in order for
the compound condition to be TRUE.
OR Operator When the OR operator connects simple
conditions, the compound condition will be TRUE whenever
anyone of the simple conditions is TRUE.
NOT Operator Reverses the truth of the original condition will
be false; if the original is false the new condition will be true.
Query: List the number and name of customer that has a balance of more than 10,000
and have credit limit of 20,000,
Query: List the name of customer does not handle by agent
code 10
Special Operators

BETWEEN TRUE if the operand is within the range of


comparisons

LIKE TRUE if the operand matches a pattern

IN TRUE if the operand is equal to one of a list


of expressions

IS NULL operator is used to test for non-empty values


(NOT NULL values).
Query: List all Customer whose balances are between 6,000 and 20,000
SQL LIKE OPERATOR

The LIKE operator is used in a WHERE clause to


search for a specified pattern in a column.
There are two wildcards often used in conjunction
with the LIKE operator:
● The percent sign (%) represents zero, one, or
multiple characters
● The underscore sign (_) represents one, single
character
Query: List the name of customer whose name start with
Letter A
Using IN OPERATOR
The IN operator allows you to specify multiple values in a WHERE clause.

Query: List the name of customer who handle by Sale agent 5 and 10
Query: List all customers that do have assigned sales agent

You might also like