欢迎来访~

MySQL基本操作举例

本文通过一个例子简要说明了MySQL的基本操作。

 

1.登录数据库

[root@hello2099 ~]# mysql -u root -p

[root@hello2099 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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.

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

mysql>
 

2.查看数据库

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

3.创建数据库

创建一个数据库game 并查看
mysql> create database game;
mysql> show databases;
mysql> create database game;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| game               |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)
 

4.创建数据库表

在game库中创建表hero,hero表的字段和属性为: id int(20) ,name char(40),level int(2) 并查看。
mysql> use game;
mysql> create table hero (id  int(20),name  char(40),level  int(2));
mysql> desc hero;
mysql> use game;
Database changed

mysql> create table hero (id  int(20),name  char(40),level  int(2));
Query OK, 0 rows affected (0.01 sec)

mysql> desc hero;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(20)  | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
| level | int(2)   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>
 

5.插入数据

在表hero表中插入5条以上的测试数据并查看。
mysql> insert into hero values (1,’hello’,96); 
mysql> insert into hero values (2,’test’,94); 
mysql> insert into hero values (3,’xuegod’,97); 
mysql> insert into hero values (4,’daxiong’,96); 
mysql> insert into hero values (5,’top’,98); 
mysql> insert into hero values (6,’tree’,98); 
mysql> select * from hero;
mysql> insert into hero values (1,'hello',96); 
Query OK, 1 row affected (0.00 sec)

mysql> insert into hero values (2,'test',94); 
Query OK, 1 row affected (0.00 sec)

mysql> insert into hero values (3,'xuegod',97); 
Query OK, 1 row affected (0.00 sec)

mysql> insert into hero values (4,'daxiong',96); 
Query OK, 1 row affected (0.00 sec)

mysql> insert into hero values (5,'top',98); 
Query OK, 1 row affected (0.00 sec)

mysql> insert into hero values (6,'tree',98); 
Query OK, 1 row affected (0.00 sec)

mysql> select * from hero;
+------+---------+-------+
| id   | name    | level |
+------+---------+-------+
|    1 | hello   |    96 |
|    2 | test    |    94 |
|    3 | xuegod  |    97 |
|    4 | daxiong |    96 |
|    5 | top     |    98 |
|    6 | tree    |    98 |
+------+---------+-------+
6 rows in set (0.00 sec)
 

6.数据表操作

查找出id大于4的记录
mysql>  select * from hero where id>4;
mysql>  select * from hero where id>4;
+------+------+-------+
| id   | name | level |
+------+------+-------+
|    5 | top  |    98 |
|    6 | tree |    98 |
+------+------+-------+
2 rows in set (0.00 sec)
 

7.常用的基础SQL语句

其它常见的SQL语句有很多,这里就不一一列出了。
详见我的另一边博文:MySQL常用命令
 
 
 
 
 
-The End-
 
 
赞(0)
可附来源转载:一只北极熊 » MySQL基本操作举例

富强、民主、文明、和谐、自由、平等、公正、法治、爱国、敬业、诚信、友善