kowala's home

kowala's home
這裡是我的學習筆記,陸續增加中。
http://kowala21.blogspot.com

2011-06-14

建立 MySQL 資料庫的基本步驟(安裝後的動作)

一開始進入MySQL,首先就是開個帳號,然後再弄個資料庫,做幾張表格,
填入一些中英文字來做查表測試,這些都完成後,就可以回到程式語言中,
撰寫程式來連線了。以下就上述步驟來個實際演練。

1.安裝完成MySQL後,就是進到資料庫中,預設的帳號是root。

C:\Users\abc>mysql -u root -p    [Enter]
Enter password: **********    [Enter]
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.49-community MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

2.先開個帳號,並設定權限,來供程式連線用,下面的設定是開放一切權限給他,
但僅能在本機執行,設定 ID/PWD = abc/123 。

mysql> grant all on *.* to abc@localhost identified by '123';

3.再來是建立資料庫了,先查看有哪些內建的資料庫。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.01 sec)

有3個資料庫,其中有一個是 test,來看看內容。

mysql> show tables from test;
Empty set (0.02 sec)

很好,是空的資料庫,正好拿來測試用。先建個表格,再填一些資料...

mysql>  use test;
Database changed

建個表格
mysql> create table news(akey int not null primary key,day DATE,title VARCHAR(20
),content VARCHAR(50));
Query OK, 0 rows affected (0.20 sec)

查看一下剛剛建立的news表格格式

mysql> describe news;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| akey    | int(11)     | NO   | PRI | NULL    |       |
| day     | date        | YES  |     | NULL    |       |
| title   | varchar(20) | YES  |     | NULL    |       |
| content | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> SHOW COLUMNS FROM news;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| akey    | int(11)     | NO   | PRI | NULL    |       |
| day     | date        | YES  |     | NULL    |       |
| title   | varchar(20) | YES  |     | NULL    |       |
| content | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

由上例可知,這兩個指令結果是一樣的。
describe news;
SHOW COLUMNS FROM news;

接著輸入一些紀錄,來測試查詢。

mysql> insert into news values(1,'2010-08-21','UPDATE Syntax','The UPDATE statem
ent updates columns...');
Query OK, 1 row affected (0.05 sec)

看看記錄存進去沒...
mysql> select * from news;
+------+------------+---------------+-----------------------------------------+
| akey | day        | title         | content                                 |
+------+------------+---------------+-----------------------------------------+
|    1 | 2010-08-21 | UPDATE Syntax | The UPDATE statement updates columns... |
+------+------------+---------------+-----------------------------------------+
1 row in set (0.39 sec)

很好,可以開始撰寫程式了...

MySQL 5.1参考手册

沒有留言:

張貼留言

請提供您的寶貴意見 ;-)