Declaration:
brackets<> :
denote required portions of syntax for the command,
square bracketsas []:
denote additional modifiers
parentheses ():
denote optional portions of commands.
1.Creating a database
*If you are at the command prompt
methed one:
command:
mysqladmin -p create <database_name>
eg:
piniheaven@piniheaven:~$ sudo mysqladmin -p create Students
Enter password:
piniheaven@piniheaven:~$ mysql -uroot -p #login,and check whether the database Students was created successfully
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41
......
......
mysql> SHOW DATABASES; #we can see the database Students here
+--------------------+
| Database |
+--------------------+
| information_schema |
| MyDatabase |
| Students |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
command:
CREATE DATABASE [ if not exists ] <database_name>
eg:
mysql> CREATE DATABASE Teacher;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MyDatabase |
| Students |
| Teacher |
| mysql |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.00 sec)
2.Connecting to database
*If you are at the command prompt
methed one:
command:
mysql -u <username> -p <databasename>
eg:
fish@piniheaven:~$ mysql -u root -p Stutents;
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
.......
*if within the MySQL CLI
method two:
connect <databasename>
eg:
mysql> connect Students;
Connection id: 43
Current database: Students
3.Creating a table
command:
CREATE [ temporary ] TABLE [ if not exists ] <tablename>
[ ( <column definition,> ... ) ] [ <table options> ] [ <selectstatement> ]
explanation:
✦ If there is a table of the same name, the existing table is hidden.
✦ if not exists keyword prevents the table from being created if a table of thesame name exists.
✦ temporary keyword creates a table that exists only for the duration of the connection.
✦ table options are used to create different types of tables.MyISAM is the default table type in MySQL.
More information about table types wil be introduce in later articles.
eg:
mysql> CREATE TABLE IF NOT EXISTS Scores
-> (
-> id int,
-> name varchar(30),
-> subject varchar(20),
-> score float
-> );
Query OK, 0 rows affected (0.12 sec)
mysql> DESCRIBE Scores; #show the structure of Scores
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| subject | varchar(20) | YES | | NULL | |
| score | float | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> CREATE TEMPORARY TABLE IF NOT EXISTS Scores
-> (
-> id int
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> DESCRIBE Scores; #the existing table Scores is hidden.
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
...... #logout
..... #login
mysql> DESCRIBE Scores; #when we logout,the temporary table Scores is no longer exists
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| subject | varchar(20) | YES | | NULL | |
| score | float | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
brackets<> :
denote required portions of syntax for the command,
square bracketsas []:
denote additional modifiers
parentheses ():
denote optional portions of commands.
1.Creating a database
*If you are at the command prompt
methed one:
command:
mysqladmin -p create <database_name>
eg:
piniheaven@piniheaven:~$ sudo mysqladmin -p create Students
Enter password:
piniheaven@piniheaven:~$ mysql -uroot -p #login,and check whether the database Students was created successfully
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41
......
......
mysql> SHOW DATABASES; #we can see the database Students here
+--------------------+
| Database |
+--------------------+
| information_schema |
| MyDatabase |
| Students |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
*if within the MySQL CLI
method two:command:
CREATE DATABASE [ if not exists ] <database_name>
eg:
mysql> CREATE DATABASE Teacher;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| MyDatabase |
| Students |
| Teacher |
| mysql |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.00 sec)
2.Connecting to database
*If you are at the command prompt
methed one:
command:
mysql -u <username> -p <databasename>
eg:
fish@piniheaven:~$ mysql -u root -p Stutents;
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
.......
*if within the MySQL CLI
method two:
connect <databasename>
eg:
mysql> connect Students;
Connection id: 43
Current database: Students
3.Creating a table
command:
CREATE [ temporary ] TABLE [ if not exists ] <tablename>
[ ( <column definition,> ... ) ] [ <table options> ] [ <selectstatement> ]
explanation:
✦ If there is a table of the same name, the existing table is hidden.
✦ if not exists keyword prevents the table from being created if a table of thesame name exists.
✦ temporary keyword creates a table that exists only for the duration of the connection.
✦ table options are used to create different types of tables.MyISAM is the default table type in MySQL.
More information about table types wil be introduce in later articles.
eg:
mysql> CREATE TABLE IF NOT EXISTS Scores
-> (
-> id int,
-> name varchar(30),
-> subject varchar(20),
-> score float
-> );
Query OK, 0 rows affected (0.12 sec)
mysql> DESCRIBE Scores; #show the structure of Scores
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| subject | varchar(20) | YES | | NULL | |
| score | float | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> CREATE TEMPORARY TABLE IF NOT EXISTS Scores
-> (
-> id int
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> DESCRIBE Scores; #the existing table Scores is hidden.
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
...... #logout
..... #login
mysql> DESCRIBE Scores; #when we logout,the temporary table Scores is no longer exists
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| subject | varchar(20) | YES | | NULL | |
| score | float | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)