An Introduction to SQL
You already might be an experienced user of a database
management system (DBMS). You might find a DBMS at your school’s
library,at a site on the Internet, or in any other place where you retrieve
data using a computer. In this chapter, you will begin your study of
STRUCTURE QUERY LANGUAGE (SQL), which is one of the most popular
and widely used languages for retrieving and manipulating database
data. You will be using a specific version of SQL that the MYSQL DBMS
understands as you complete the chapters in the next.
In the mid-1970’s, SQL that the MYSQL was developed as the data
manipulation language for IBM’S prototype relational model DBMS,
System R. under the name SEQUEL, at IBM’s San Jose research facilities.
In 1980. the language was renamed SQL (but still pronounced ‘sequel’
although the equally popular pronunciation of S-Q-L is used in this text,
to avoid confusion with an unrelated hardware product name SEQUEL.
Most DBMS use a version of SQL as their data manipulation language.
In this lesson, you will learn the basics of working in MySQL. You
will learn how to assign data types to columns in a database. You also
will learn about a special type of value, called a null value. And learn
how to handle these values and adding data to them. Finally, you will
learn how to describe a table’s layout using MySQL.
SQL is a query programming language that manages RDBMS. MySQL is
a relational database management system that uses SQL. SQL is
primarily used to query and operate database systems. MySQL allows you to
handle, store, modify and delete data and store data in an organized way.
MySQL (/ˌmaɪˌɛsˌkjuːˈɛl/) is an open-source relational database management
system (RDBMS). Its name is a combination of "My", the name of co-
founder Michael Widenius's daughter, and "SQL", the abbreviation for
Structured Query Language
What is Database
It is a structured set of data held in computer / server,which can
be accessed programs / software for use in their system.
Benefits of Learning SQL - is a used in any platform when it regards to
database learning this would be a great help when starting to learn how
to connect databases using other programming languages.
Database Structure
In SQL databases are often displayed as a set of tables wherein
tables contains a row for each individual and a column for each data of
an individual.
DATA STRUCTURE
(FULL SCHEMA)
Table DATA
tbl_users
INSERT INTO
Database
db_ecommerce
Table
DATA
tbl_shops
INSERT INTO
CREATING A DATABASE
CREATE DATABASE database_ecommerce;
CREATING A TABLE
CREATE A TABLE table_name (variable datatype);
Or
CREATE A TABLE table_name (variable datatype constraint);
DATA STRUCTURE
(FULL SCHEMA)
tbl_users
UID userName emailAddress ownedShop
1 MBCSchool [email protected] MBCshop
2 John Doe [email protected] none
3 Alan Blake [email protected] shop
4 Ray Hogan [email protected] none
DATABASE STRUCTURE
(Primary Key)
The unique data that is used to identify an individual set of data.
PS: This is open auto generated by the program
UID
1
2
3
4
CONSTRAINTS
1. NOT NULL - will not accept empty values. Occasionally, when you enter a
new row into a table or modify an existing row, the values for one or more
columns are unknown or unavailable. For example, you can add a customer’s
name and address to a table even though the customer does not have an
assigned sales rep or established credit limit. In other cases, some values
might never be known-- perhaps there is a customer that does not have a
sales rep. In SQL, you handle this situation by using a special value to
represent cases in which an actual value is unknown, unavailable , or not
applicable. This special value is called a null data value, or simply a null.
When creating a table, you can specify whether to allow nulls in the individual
columns.
Question: Should a user be allowed to enter null values for the primary key?
Implementation of Nulls
In SQL, you use the NOT NULL clause in a CREATE TABLE command
to indicate that cannot contain null values. The default is to allow nulls,
column for which you do not specify NOT NULL, can accept null values.
For example, suppose that the LAST_NAME and FIRST_NAME
column in the REP table cannot accept null values, but other columns in the
REP table can.
2. UNIQUE - will not accept duplicate values.
3. Primary Key - will not accept empty and duplicate values. Another
important concept of database design is that of the primary key. In the
simplest terms, the primary key is the unique identifier for a table.
AUTO_ INCREMENT
This is a keyword is used so that your primary key would be automatically
generated for you.
PS: ONLY WORKS WITH NUMERICAL DATA
MOST COMMON SQL DATA TYPE
NUMERIC DATA
INTEGER / INT - Stores numeric values from - 1247483648 to 2147483647.
DOUBLE - Stores numeric values with decimals with lower floating point than
float.
FLOAT - Stores numeric value with decimals with a high floating point.
BOOL - Stores TRUE = 1 or FALSE = 0
TEXT DATA
VARCHAR - Set of characters with a width or size up to 8,000 characters.
TEXT - Set of characters with the size of 2GB text data.
MOST COMMON SQL DATA TYPE
TIME DATA
DATE - Stores data in the format YYY-MMM-DDD hh:mm:ss.
TIME - Stores time in the format hh:mm:ss.
DATETIME - Stores Data and time in the format YYY-MMM-DDD hh:mm:ss.
MISCELLANEOUS DATA
BLOB - Stores large binary data.
CREATING A DATABASE
Before creating tables, you must create a database by executing the
CREATE DATABASE command followed by the name of the database. To
create a database name dbenrolled
Create daTabase dbenrolled
CREATE A DATABASE tblStudents (
The INSERT command
The INSERT command adds rows to a table. You type INSERT INTO
followed by the name of the table into which you are adding data. They you
type of the VALUES command followed by the specific values to be inserted
in parentheses. When adding rows to character columns, make sure you
enclose the values in single quotation marks. (for example , ‘kaiser’). you also
must enter the value in the appropriate case, because character data is stored
exactly as you enter it.
INSERT INTO tblstudents (
FirstName,
LastName,
PhoneNumber,
Sholarship,
DateEnrolled
)VALUES
("MBCian", "METRO", "09214850225", FALSE, "2022-06-20"),
("MBCian", "BUSINESS", "0921485022", TRUE, "2022-06-20")
# SID, FirstName, LastName, PhoneNumber, Sholarship, DateEnrolled
'10', 'MBCian', 'METRO', '09214850225', '0', '2022-06-20'
'11', 'MBCian', 'BUSINESS', '0921485022', '1', '2022-06-20'
Syntax Usage Example
Column / Fields Table Name
SELECT * FROM Customers;
SQL Command SQL command
INSERTING DATA
INSERT INTO table_name(p1,p2..) VALUES (v1, v2)