0% found this document useful (0 votes)
2 views

Database - MySQL

The document provides an overview of databases, specifically focusing on relational databases and MySQL as a popular relational database management system (RDBMS). It covers key concepts such as data organization, data types, SQL operations, and benefits of using MySQL. Additionally, it includes examples of SQL statements for data manipulation and table creation.

Uploaded by

abrahamsamuel898
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)
2 views

Database - MySQL

The document provides an overview of databases, specifically focusing on relational databases and MySQL as a popular relational database management system (RDBMS). It covers key concepts such as data organization, data types, SQL operations, and benefits of using MySQL. Additionally, it includes examples of SQL statements for data manipulation and table creation.

Uploaded by

abrahamsamuel898
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/ 5

DATABASE

Database
Is a collection of data stored in a format that can easily be accessed
DBMS is used to manage Database

Categories
1.​ Relational : Relation stores data in tables linked together
2.​ Not Relational (No Sql)

Examples of RDBMS
1.​ MySQL
2.​ SQLServer
3.​ Oracle

No Relational
No tables and relationships
Don’t understand SQL

MySQL

Introduction to MySQL

MySQL is a popular open-source relational database management system


(RDBMS). It's widely used for storing, managing, and accessing data in various
applications.

1. Relational Database

●​ Data is organized into tables containing rows and columns.


●​ Each table represents a specific entity or concept (e.g., customers, products,
orders).
●​ Rows represent individual records within a table (e.g., a customer record).
●​ Columns represent attributes or properties of those records (e.g., customer
name, email).

2. Data Types

●​ MySQL supports various data types to store different kinds of data (e.g.,
integers, strings, dates, decimals).
●​ Choosing the appropriate data type helps optimize storage and ensures data
integrity.

3. SQL (Structured Query Language)

●​ SQL is a standard language for interacting with relational databases.


●​ You can use SQL to perform various operations on your data:
○​ Create, alter, and drop tables
○​ Insert, update, and delete data
○​ Retrieve data using SELECT statements with WHERE clauses for
filtering
○​ Join tables based on relationships

4. Key Concepts

●​ Primary Key: A unique identifier for each row within a table (usually an
auto-incrementing integer).
●​ Foreign Key: Creates a link between two tables, enforcing data integrity.
●​ Indexes: Speed up data retrieval by creating an index on frequently used
columns.

5. Benefits of Using MySQL

●​ Open-source and Free: Freely available to use and modify.


●​ Cross-Platform: Runs on various operating systems (Windows, Linux,
macOS).
●​ Scalability: Can handle small to large databases efficiently.
●​ Reliability: Offers robust features for data security and integrity.

Data Types

1. Numeric Data Types:

INT: Stores whole numbers (positive, negative, or zero) of varying sizes depending
on the specific implementation (often 32-bit integers).

SMALLINT: Stores smaller whole numbers, typically using less storage space than
INT.

BIGINT: Stores very large whole numbers.

DECIMAL: Stores numbers with decimal precision for accurate calculations involving
money or other precise measurements.

FLOAT: Stores approximate floating-point numbers for representing real numbers.


DOUBLE: Similar to FLOAT but offers higher precision for decimal numbers.

2. Character and String Data Types:

CHAR: Stores fixed-length character strings. Each character uses a predefined


amount of space regardless of the actual characters used.

VARCHAR: Stores variable-length character strings. This is more space-efficient for


storing text of varying lengths.

TEXT: Stores large amounts of text data (often used for long descriptions or content).

3. Date and Time Data Types:

DATE: Stores date information (year, month, day).

TIME: Stores time information (hours, minutes, seconds).

DATETIME: Stores both date and time information in a single column.

TIMESTAMP: Similar to DATETIME but often used with automatic timestamp


generation on updates.

4. Boolean Data Type:

BOOLEAN: Stores true or false values.

5. Binary Data Types:

BLOB (Binary Large OBject): Stores large binary data (e.g., images, audio, files).

VARBINARY: Stores variable-length binary data.

6. Other Data Types:

ENUM: Allows selecting a value from a predefined set of options.


STATEMENTS
SELECT:
You can select a single or multiple columns
You can use alias, AS
To get unique results, use DISTINCT
​ SELECT DISTINCT state FROM customers
CREATE
INSERT INTO
WHERE clause:
​ SELECT * FROM customer WHERE id = 3
​ SQL Operators: >, >=, <, <=, =, !=, <>
​ Isn’t case sensitive to specify order
AND, OR, NOT:
​ AND has higher preceedence. Use parenthesis
IN : Is a short form for using multiple ORs operator
​ SELECt * FROM customers WHERE state IN (‘VA’, ‘FL’, ‘GA’)
SELECt * FROM customers WHERE state NOT IN (‘VA’, ‘FL’, ‘GA’)

BETWEEN : compares value within a range


​ SELECT * FROM customers WHERE points BETWEEN 1000 AND 3000
LIKE : find strings that matches a pattern
​ SELECT * FROM customer WHERE last_name LIKE ‘b%’
​ Not case sensitive
Using REGEXP:
SELECT * FROM customer WHERE last_name REGEXP ‘ay’
SELECT * FROM customer WHERE last_name REGEXP ‘^ay’ — last name
must start with ay
SELECT * FROM customer WHERE last_name REGEXP ‘ay$’ – last name
must end with ay
SELECT * FROM customer WHERE last_name REGEXP ‘ay | ade | olu’ —
multiple conditions
SELECT * FROM customer WHERE last_name REGEXP ‘[st]de’ — returns
last name that has either s or t before d anywhere in the last name

^ beginning
$ end
| logical or
[abcd]e —any coming before e

IS NULL: SELECT * FROM customer WHERE phone IS NULL
SORTING
ORDER BY first_name DESC
LIMIT :
​ SELECT * FROM customer LIMIT 10
Should always come at the end
Offset
SELECT * FROM customer LIMIT 3 10 —- offset is 3, then selects 10 records

Creating a Table
CREATE TABLE ‘clients’ (
‘Client_id’ int(11) NOT NULL,
‘Name’ varchar(50) NOT NULL,
‘Address’ varchar(50) NOT NULL,
‘Phone’ varch(20) DEFAULT NULL,
PRIMARY KEY (‘client_id’)
)

Put values in a Table


INSERT INTO ‘clients’ VALUES (1, ‘Doe’, “kentucky”, “343434334”)

NOTE:The order of clauses are important

You might also like