MySQL Primer--Create Database,Create Table

                              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)


 *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)








  


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值