Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST
Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST
What is PHP?
Review
What is MySQL?
Review
What is Apache?
Review
Id Name Student-Id
You should see next is an Enter password: prompt. Enter the root password you
selected previously during the post-installation and hit Enter.
If you typed your password correctly, you will see following message in your
command prompt
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.31 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Some Simple Command of
MySQL Server
Type this command (including the semicolon!) and press Enter:
mysql> SHOW DATABASES;
MySQL will show you a list of the databases on the server. If you’re
working on a
brand new server, the list should look like this:
Some Simple Command of
MySQL Server
• The MySQL server uses the first database, named
information_schema, to keep track of all the other databases on the
server.
MySQL will show you only two databases on the server. Since you deleted the third
database test.
Finally, if at any time you want to exit the MySQL client program, just
type quit or exit (either will work). This is the only command where
the semicolon is unnecessary, but you can use one if you want to.
mysql> quit
Bye
Creating a Database
Since you used the quit command last time to you need to login again
on MySQL
to connect to your MySQL server. Type this command and hit Enter:
mysql -u root –p
Enter the root password, and hit Enter. You will see the mysql prompt
like
mysql>
Creating a Database
I chose to name the database ijdb. You can chose any name for your
database.
Now that you have a database, you need to tell MySQL that you want
to use it.
Creating a Table
1. First, when creating a row in this table, this column is not allowed to be left blank
(NOT NULL).
2. Next, when we add a new entry to the table, we want MySQL to automatically pick
a value that is one more than the highest value in the table so far
(AUTO_INCREMENT).
3. Finally, this column is to act as a unique identifier for the entries in the table, so all
values in this column must be unique (PRIMARY KEY).
Creating a Table (Example) Discussion of commands
joketext TEXT,
This third line says that we want a column called joketext, which will contain text
(TEXT).
This fourth line defines our last column, called jokedate; this will contain a
date (DATE), which cannot be left blank (NOT NULL).
If you typed the above command correctly, MySQL will respond with “Query
OK”, and your first table will be created.
If you made a typing mistake, MySQL will tell you there was a problem with
the query you typed, and will try to indicate where it had trouble.
Have a look at your new table to make sure it was created properly. Type the
following command:
mysql> SHOW TABLES;
The response should look like this
Creating a Table (Example) Discussion of commands
Let’s take a closer look at the joke table itself using a DESCRIBE command:
mysql> DESCRIBE joke;
Notice that we used double quotes (") to mark where the text of the
joke started and ended. A piece of text enclosed in quotes this way is
called a text string, and this is how you represent most data values in
SQL.
You will notice, for instance, that the dates are typed as text strings
as well, in the form "YYYY-MM-DD".
Inserting Data into a Table (example)
• If you prefer, you can type text strings surrounded with single
quotes (') instead of double quotes:
mysql> INSERT INTO joke SET
-> joketext = 'Why did the chicken cross the road? To get to the other side!',
-> jokedate = '2009-04-01';
• What happens when the text of a joke itself contains quotes. Well, if
the text contains single quotes, the easiest thing to do is surround it
with double quotes. Conversely, if the text contains double quotes,
surround it with single quotes.
Inserting Data into a Table (example)
• If the text you want to include in your query contains both single
and double quotes,
• You will have to escape the conflicting characters within your text
string.
• You can escape a character in SQL by adding a backslash (\)
immediately before it. This tells MySQL not to interpret the
character as the end of the text string.
Inserting Data into a Table (example)
To make this as clear as possible, here is an INSERT command for a joke
containing both single and double quotes:
• As you can see, I have marked the start and end of Recall here we have
the text string for the joke text using single quotes. used second
• Therefore I had to escape the three single quotes command to insert
within the string by putting backslashes before data into Table
them.
Inserting Data into a Table (example)
-+------------+
2 rows in set (0.00 sec)
Inserting Data into a Table
(example)
To View specific Column in Table
If you just want to see any specific column of Table you may use the command like
below.
+----+------------+
mysql> SELECT id, jokedate FROM joke; | id | jokedate |
+----+------------+
| 1 | 2009-04-01 |
This time you will see
+----+------------+
only selected column of +----+------------+
Table as below | 2 | 2009-04-01 |
+----+------------+
+----+----------------------+------------+
| id | LEFT(joketext, 20) | jokedate |
+----+----------------------+------------+
| 1 | Why did the chicken | 2009-04-01 |
+----+----------------------+------------+
+----+----------------------+------------+
| 2 | Knock-knock! Who\'s t | 2009-04-01 |
+----+----------------------+------------+
+----------+
| COUNT(*) |
+----------+
|2|
+----------+
1 row in set (0.02 sec)
This query displays the full text of all jokes that contain the text “chicken”
To display knock-knock jokes from April 2009 to May 2009, you could use the following
query:
mysql> SELECT joketext FROM joke WHERE
-> joketext LIKE "%knock%" AND
-> jokedate >= "2009-04-01" AND
-> jokedate < "2009-05-01";
Modifying Stored Data
This next command, for example, changes the date of all entries that
contain
the word “chicken”:
To delete all chicken jokes from your table, you’d use the following
query:
Comparison IS NOT
NULL
IS NULL
n/a
n/a
address is not T e s ts w h e t h e r fie l d a c t u a lly
null c o n t a in s a v a lu e
address is null T e s ts w h e t h e r fie l d d o e s n o t c o n t a in
This grants all privileges on all databases to a user called Fred with the password
mnb123, and allows him to pass on those privileges.
If you don’t want this user in your system, so go ahead and revoke him:
And later, when she doesn’t need to use the database any more, we can revoke her
privileges:
mysql> revoke all
-> on books.*
-> from sally;
Example
Further Self Reading
For more information, you can read about setting up a database at the
MySQL online manual at http://www.mysql.com.
https://www.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Begi
nner.html
Assignment:
Write a note about this PPT (not more then two pages).