|
UnixWare 7.1.3/7.1.44内建PostgreSQL与MySQL数据库的资源, 而我们将以简单的范例 来解释如何于UnixWare 7.1.3/7.1.4平台上运用MySQL数据库的操作.
在安装完MySQL数据库后请依照以下步骤 (请先确定/usr/local/mysql/bin已经设定于$PATH变量中):
# ps -e|grep mysql 27475 pts/2 00:00:00 mysqld_safe 27506 pts/2 00:00:00 mysqld 27507 pts/2 00:00:00 mysqld 27508 pts/2 00:00:00 mysqld 27509 pts/2 00:00:00 mysqld 27510 pts/2 00:00:00 mysqld 27511 pts/2 00:00:00 mysqld 27512 pts/2 00:00:00 mysqld 27513 pts/2 00:00:00 mysqld 27514 pts/2 00:00:00 mysqld 27515 pts/2 00:00:00 mysqld 27688 pts/2 00:00:00 mysql 27689 pts/2 00:00:00 mysqld
若否, 可以利用这样的命令启动MySQL数据库 :
/etc/init.d/mysql start
(2) 建立数据库
# mysqladmin -uroot -pyour_passwd create testdb
(3) 连结到数据库, 并且切换至适才建立的数据库
# mysql -uroot -pyour_passwd
将能够看到类似以下的讯息
Welcome to the MySQL monitor. Commands end with or \g. Your MySQL connection id is 9 to server version: 4.0.18-standard
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
(接着, 我们使用 use 命令切换数据库)
mysql> use testdb
Database changed
这表示已经切换到我们刚才建立的 testdb 数据库了
(4) 建立数据库所需的表格
我们可以利用这样的命令检查目前数据库内的table :
mysql> show tables; Empty set (0.00 sec)
它会显示目前数据库内并没有任何表格存在
接着, 我们利用这样的命令建立表格 :
mysql> CREATE TABLE `clients` ( -> `id` int(11) NOT NULL auto_increment, -> `name` text NOT NULL, -> `address1` text NOT NULL, -> `address2` text NOT NULL, -> `city` text NOT NULL, -> `postcode/zip` text NOT NULL, -> `county` text NOT NULL, -> `phone` text NOT NULL, -> `fax` text NOT NULL, -> KEY `id` (`id`) -> ) TYPE=MyISAM AUTO_INCREMENT=1
Query OK, 0 rows affected (0.15 sec)
(5) 同时我们新增数据进去刚刚建立的表格内
mysql> INSERT INTO clients valueS (1,'Margaret Smith','123 The Street','Davidsons Mains','Edinburgh','EH4 1PD','Midlothian','+44 131 555 1234','+44 131 666 2345'); Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO clients valueS (2,'Jimbo Jones','34 Main Road','Leafy Suburb','Birmingham','BH4 7QD','','+44 666 666 7777','+44 666 666 9999'); Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO clients valueS (3,'Bernard Worthington','1 Barnton Avenue','Leafy Suburb','Birmingham','BH14 6QH','','+44 666 666 9999','+44 666 666 0000'); Query OK, 1 row affected (0.00 sec)
(6) 利用 select 命令取出适才新增的数据
mysql> select * from clients;
+--+------------+------------+---------+----------+--------------+--------+------+----------+ |id| name | address1 | address2| city | postcode/zip | county | phone| fax | +--+------------+------------+---------+----------+--------------+--------+------+----------+ |1 | Margaret Smith | 123 The Street | DavidsonsMains | Edinburgh | EH4 1PD | Midlothian | +44 131 555 1234 | +44 131 666 2345 || 2 | Jimbo Jones | 34 Main Road | LeafySuburb | Birmingham | BH4 7QD | | +44 666 666 7777 | +44 666 666 9999 || 3 | Bernard Worthington | 1 Barnton Avenue | Leafy Suburb | Birmingham | BH14 6QH | | +44666 666 9999 | +44 666 666 0000 | +--+------------+------------+---------+----------+--------------+--------+------+----------+ 3 rows in set (0.00 sec)
以上就是一个很简单的MySQL数据库操作范例
| |