Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Working with Your MySQL Database:

 Introduction of SQL,
 Inserting data into the Database,

 Retrieving Data from the Database,


 Updating Records in the Database,
 Altering Tables After Creation,
 Deleting Records from the Database,
 Dropping Tables,
 Dropping a Whole Database
 MySQL is the most popular database system used with
PHP.
 MySQL is a very popular, open source database.
 Handles very large databases; very fast performance.

 The data in a MySQL database are stored in tables. A table is a


collection of related data, and it consists of columns and rows.
 MySQL is a database system used on the web
 MySQL is a database system that runs on a server
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, and easy to use
 MySQL uses standard SQL
 MySQL compiles on a number of platforms
 MySQL is free to download and use
 MySQL is developed, distributed, and supported by
Oracle Corporation
 MySQL is named after co-founder Monty Widenius's
daughter: My
 For example:
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 241 to server version: 3.23.49

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

 To exit the MySQL Shell, just type QUIT or EXIT:


mysql> QUIT
mysql> exit

5
 Once logged in, you can try some simple queries.
 For example:

mysql> SELECT VERSION(), CURRENT_DATE;


+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 3.23.49 | 2002-05-26 |
+-----------+--------------+
1 row in set (0.00 sec)

 Note that most MySQL commands end with a semicolon (;)


 MySQL returns the total number of rows found, and the total
time to execute the query.

6
 Here's another query. It demonstrates that
you can use mysql as a simple calculator:

mysql> SELECT SIN(PI()/4), (4+1)*5;


+-------------+---------+
| SIN(PI()/4) | (4+1)*5 |
+-------------+---------+
| 0.707107 | 25 |
+-------------+---------+

7
 You can also enter multiple statements on a
single line. Just end each one with a semicolon:

mysql> SELECT VERSION(); SELECT NOW();


+--------------+
| VERSION() |
+--------------+
| 3.22.20a-log |
+--------------+
+---------------------+
| NOW() |
+---------------------+
| 2004 00:15:33 |
+---------------------+

8
 mysql determines where your statement
ends by looking for the terminating
semicolon, not by looking for the end of
the input line.
 Here's a simple multiple-line statement:
mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
+--------------------+--------------+
| USER() | CURRENT_DATE |
+--------------------+--------------+
| joesmith@localhost | 1999-03-18 |
+--------------------+--------------+
9
 If you decide you don't want to execute a
command that you are in the process of
entering, cancel it by typing \c

mysql> SELECT
-> USER()
-> \c
mysql>

10
 To get started on your own database, first check
which databases currently exist.
 Use the SHOW statement to find out which
databases currently exist on the server:

mysql> show databases;


+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.01 sec)

11
 To create a new database, issue the “create
database” command:
◦ mysql> create database webdb;
 To the select a database, issue the “use”
command:
◦ mysql> use webdb;

12
 Once you have selected a database, you can
view all database tables:
mysql> show tables;
Empty set (0.02 sec)
 An empty set indicates that I have not created
any tables yet.

13
 Let’s create a table for storing pets.
 Table: pets
name: VARCHAR(20)
owner: VARCHAR(20)
species: VARCHAR(20)
sex: CHAR(1)
birth: DATE
date: DATE
VARCHAR is
usually used
to store string
data.

14
 To create a table, use the CREATE TABLE command:

mysql> CREATE TABLE pet (


-> name VARCHAR(20),
-> owner VARCHAR(20),
-> species VARCHAR(20),
-> sex CHAR(1),
-> birth DATE, death DATE);
Query OK, 0 rows affected (0.04 sec)

15
To verify that the table has been created:
mysql> show tables;
+------------------+
| Tables_in_test |
+------------------+
| pet |
+------------------+
1 row in set (0.01 sec)

16
 To view a table structure, use the DESCRIBE
command:

mysql> describe pet;


+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)

17
 To delete an entire table, use the DROP TABLE
command:

mysql> drop table pet;


Query OK, 0 rows affected (0.02 sec)

18
 Use the INSERT statement to enter data into
a table.
 For example:

INSERT INTO pet VALUES


('Fluffy','Harold','cat','f',
'1999-02-04',NULL);
 The next slide shows a full set of sample
data.

19
 mysql>create database bvm;
 mysql>show databases;
 mysql>use bvm;
 mysql>create table [table name] (personid
int(50) not null auto_increment primary
key,firstname varchar(35),middlename
varchar(50),lastname varchar(50),age INT(10)
);
 mysql>Show tables;
 mysql>describe table_name;
 mysql>select * from table_name;
 mysql>create database bvm;
 mysql>show databases;
 mysql>use bvm;
 mysql>create table [table name] (personid
int(50) not null auto_increment primary
key,firstname varchar(35),middlename
varchar(50),lastname varchar(50),age INT(10)
);
 mysql>Show tables;
 mysql>describe table_name;
 mysql>select * from table_name;
 Insert
 Update
 Delete
 Select
 Different practical (for insert , update ,delete
,select)
 create database test2;
 show databases;
 use test2;

CREATE TABLE `users` (


`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
);

 Show tables;
 ALTER TABLE table_name
ADD column_name datatype;

 ALTER TABLE table_name


DROP COLUMN column_name;

 ALTER TABLE table_name


MODIFY COLUMN column_name datatype;
 ALTER TABLE [table name] change [old
column name] [new column name] varchar
(50);

 ALTER TABLE [table name] add unique


([column name]); (Add unique from table)

 ALTER TABLE [table name] drop index [colmn


name]; (Delete unique from table)
DELETE FROM table_name
WHERE condition;

 DELETE FROM table_name;


 Drop table table name;

 TRUNCATE TABLE table_name;

 The TRUNCATE TABLE statement is used to delete the data inside a


table, but not the table itself.)
 Drop database [database name];
Chapter 5
 Web database architecture,
 Querying a database from the web
 Putting new information in the database,
 Using prepared statements,
 Using other PHP-database interfaces.
Thank You…
www.priyankbhojak.co.in

You might also like