2014年4月22日 星期二

【JDBC 00】建立資料庫

DB 操作:

查詢: show databases;
新增: create database Database_Name;
刪除: drop database Database_Name;
開啓: use Database_Name;

Table 操作:

新增: create table Table_Name ( ... );
查詢全部: show tables;
刪除: drop table Table_Name;

Schema :

查詢表格: show columns from Table_Name;

Data:

新增: insert into Table_Name(no,name) values (1,'Tom');


以 root 帳號登入
ElvisdeMacBook-Pro:bin elvismeng$ pwd
/Applications/XAMPP/bin
ElvisdeMacBook-Pro:bin elvismeng$ ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 56
Server version: 5.6.16 Source distribution

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

mysql> 

新增資料庫
mysql> CREATE DATABASE phone;
Query OK, 1 row affected (0.01 sec)

mysql>

開啟資料庫
mysql> use phone;
Database changed
mysql> 

新增資料表 Table

mysql> CREATE TABLE student (
    -> ID integer,
    -> name char(30)
    -> );
Query OK, 0 rows affected (0.07 sec)

mysql>

查詢資料庫所有資料表 Table
mysql> show tables;
+-----------------+
| Tables_in_phone |
+-----------------+
| student         |
+-----------------+
1 row in set (0.00 sec)

mysql> 

查詢指定的資料表 Table
mysql> SHOW COLUMNS FROM student;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| ID    | int(11)  | YES  |     | NULL    |       |
| name  | char(30) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> 

新增資料 Table
mysql> INSERT INTO student (ID, name) values (1,'Tom');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO student (ID, name) values (2,'Bob');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO student (ID, name) values (3,'Joe');
Query OK, 1 row affected (0.01 sec)

mysql> 

查詢輸入資料 Table
mysql> SELECT * FROM student;
+------+------+
| ID   | name |
+------+------+
|    1 | Tom  |
|    2 | Bob  |
|    3 | Joe  |
+------+------+
3 rows in set (0.01 sec)

mysql> 

Reference:
http://bonny.com.tw/web/xampp/0603newmysql.htm

沒有留言:

張貼留言