|
登录
[root@centos6 ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection> Server version: 5.1.49 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>
显示库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| discuz |
| mysql |
| test |
+--------------------+
4 rows in set (0.01 sec)
切换库:这里可以不加分号,其他必须加分号
mysql> use mysql
Database changed
当前库
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
当前用户
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
当前版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.49 |
+-----------+
1 row in set (0.00 sec)
查看当前库中有哪些表
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
23 rows in set (0.03 sec)
描述表结构,查看一个表中有哪些字段
mysql> desc time_zone_name;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| Name | char(64) | NO | PRI | NULL | |
| Time_zone_id | int(10) unsigned | NO | | NULL | |
+--------------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
查看一个表的建表语句
mysql> show create table time_zone_name;
+-------------------+--------------------------------------------------------------------------------+
| Table | Create Table |
+-------------------+--------------------------------------------------------------------------------+
| time_zone_name | CREATE TABLE `time_zone_name` (
`Name` char(64) NOT NULL,
`Time_zone_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`Name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Time zone names' |
+--------------------+-------------------------------------------------------------------------------+
1 row in set (0.00 sec)
创建库
mysql> create database game;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| discuz |
| game |
| mysql |
| test |
+--------------------+
5 rows in set (0.00 sec)
使用库
mysql> use game;
Database changed
创建表
mysql> create table tb1 (`id` int(4),`name` char(40)) ENGINE=MyISAM DEFAULT CHARSET=gbk;
Query OK, 0 rows affected (0.02 sec)
查看该库有多少表,查看表tb1的结构
mysql> show tables;
+----------------+
| Tables_in_game |
+----------------+
| tb1 |
+----------------+
1 row in set (0.00 sec)
mysql> desc tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
|> | name | char(40) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
查看表的创建语句
mysql> show create table tb1 \G;
*************************** 1. row ***************************
Table: tb1
Create Table: CREATE TABLE `tb1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
ERROR:
No query specified
插入一条数据
mysql> insert into tb1 values(1,'rachy');
Query OK, 1 row affected (0.00 sec)
查看表tb1内容
mysql> select * from tb1;
+------+-------+
|> +------+-------+
| 1 | rachy |
+------+-------+
1 row in set (0.00 sec)
继续插入数据
mysql> insert into tb1 values(2,'zhangsan');
Query OK, 1 row affected (0.00 sec)
查看表内容
mysql> select * from tb1;
+------+----------+
|> +------+----------+
| 1 | rachy |
| 2 | zhangsan |
+------+----------+
2 rows in set (0.00 sec)
只插入部分字段
mysql> insert into tb1 (`id`) values(3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb1 (`name`) values('lisi');
Query OK, 1 row affected (0.00 sec)
没值的字段显示NULL
mysql> select * from tb1;
+------+----------+
|> +------+----------+
| 1 | rachy |
| 2 | zhangsan |
| 3 | NULL |
| NULL | lisi |
+------+----------+
4 rows in set (0.00 sec)
也可以打乱字段次序插入数据:
mysql> insert into tb1 (`name`,`id`) values('wangwu',5);
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
+------+----------+
|> +------+----------+
| 1 | rachy |
| 2 | zhangsan |
| 3 | NULL |
| NULL | lisi |
| 5 | wangwu |
+------+----------+
5 rows in set (0.00 sec)
根据条件更改数据
mysql> update tb1 set> Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tb1;
+------+----------+
|> +------+----------+
| 1 | rachy |
| 2 | zhangsan |
| 3 | NULL |
| 4 | lisi |
| 5 | wangwu |
+------+----------+
5 rows in set (0.00 sec)
删除指定行
mysql> delete from tb1 where> Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
+------+----------+
|> +------+----------+
| 1 | rachy |
| 2 | zhangsan |
| 4 | lisi |
| 5 | wangwu |
+------+----------+
4 rows in set (0.00 sec)
清空指定库下指定表的内容,不改变表结构
mysql> truncate table game.tb1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tb1;
Empty set (0.00 sec)
mysql> desc tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
|> | name | char(40) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除一个表
mysql> drop table tb1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tb1;
ERROR 1146 (42S02): Table 'game.tb1' doesn't exist
删除一个库
mysql> drop database game;
Query OK, 0 rows affected (0.07 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| discuz |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
|
|
|