Chapter 4
Chapter 4
Chapter 4
Introduction of SQL,
Inserting data into the Database,
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
5
Once logged in, you can try some simple queries.
For example:
6
Here's another query. It demonstrates that
you can use mysql as a simple calculator:
7
You can also enter multiple statements on a
single line. Just end each one with a semicolon:
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:
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:
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:
17
To delete an entire table, use the DROP TABLE
command:
18
Use the INSERT statement to enter data into
a table.
For example:
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;
Show tables;
ALTER TABLE table_name
ADD column_name datatype;