Chap4 Introduction To SQL and MySQL
Chap4 Introduction To SQL and MySQL
mysql.exe –u root
Intro. to SQL and MySQL
The users table will have these 6 columns to store records like the
sample data here
First thing first
1. Create database
2. Create table
1. How many columns?
• 6 columns
2. Column name?
• user_id, first_name, last_name, email, password, reg_date
3. Column type?
• 3 primay categories
1. Text
2. Numbers
3. Dates and Times
MySQL Data Type
Type Size Description
CHAR Length bytes A fixed length field from 0 to 255 char long
VARCHAR String length + 1 bytes A variable length field from 0 to 255 char long
TEXT String length + 2 bytes A string with a max length of 65,535 char
• To store STRING
• CHAR
– stored as a string the length of the column
• VARCHAR
– will be as long as the stored string itself!
– Tend to take up less disc space
Choosing other column properties
• SHA( )
one way to encrypt data
• NOW( )
will insert the current date and
time
mysql> INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES
• Syntax
– SELECT which column FROM which table
• To specify the columns to be returned
All of the records but only two of the columns are returned
by this query
Using conditionals
To limit the returned data from the table using
SELECT statement
Syntax
SELECT * FROM tablename WHERE column=‘value’;
Example:
SELECT email FROM users WHERE last_name=‘Tom’;
+---------+------------+-----------+
| user_id | first_name | last_name |
+---------+------------+-----------+
| 4 | George | Harrison |
| 2 | John | Lennon |
| 1 | Larry | Ullman |
| 3 | Paul | McCartney |
| 5 | Ringo | Starr |
+---------+------------+-----------+
5 rows in set (0.00 sec)
+---------+------------+-----------+
| user_id | first_name | last_name |
+---------+------------+-----------+
| 5 | Ringo | Starr |
| 3 | Paul | McCartney |
| 1 | Larry | Ullman |
| 2 | John | Lennon |
| 4 | George | Harrison |
+---------+------------+-----------+
5 rows in set (0.00 sec)
Limiting query result
mysql> quit
Bye
FUNCTIONS
1. TEXT FUNCTIONS
2. NUMERIC FUNCTIONS
3. DATE & TIME FUNCTIONS
4. FORMATTING THE DATE AND TIME
Text Functions
mysql> SELECT CONCAT(first_name, ' ', last_name) FROM users;
+------------------------------------+
| CONCAT(first_name, ' ', last_name) |
+------------------------------------+
| John Lennon |
| Paul McCartney |
| George Harrison |
| Ringo Starr |
+------------------------------------+
4 rows in set (0.58 sec)
Thank you.