mysql經常使用操做

mysql> show databases; #查看都有那些庫#mysql

+--------------------+sql

| Database |數據庫

+--------------------+session

| information_schema |spa

| discuz |.net

| mysql |orm

| test |ip

+--------------------+ci

4 rows in set (0.00 sec)字符串

mysql> use discuz; #使用那個庫#

Database changed

mysql> show tables; #查看庫裏邊的表#

+-----------------------------------+

| Tables_in_discuz |

+-----------------------------------+

| pre_common_admincp_cmenu |

| pre_common_admincp_group |

| pre_common_admincp_member |

| pre_common_admincp_perm |

| pre_common_admincp_session |

+-----------------------------------+

297 rows in set (0.01 sec)

mysql> desc pre_forum_poll; #查看某個表的字段#

+-------------+-----------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------+-----------------------+------+-----+---------+-------+

| tid | mediumint(8) unsigned | NO | PRI | 0 | |

| overt | tinyint(1) | NO | | 0 | |

| multiple | tinyint(1) | NO | | 0 | |

| visible | tinyint(1) | NO | | 0 | |

| maxchoices | tinyint(3) unsigned | NO | | 0 | |

| isimage | tinyint(1) | NO | | 0 | |

| expiration | int(10) unsigned | NO | | 0 | |

| pollpreview | varchar(255) | NO | | | |

| voters | mediumint(8) unsigned | NO | | 0 | |

+-------------+-----------------------+------+-----+---------+-------+

9 rows in set (0.03 sec)

mysql> show create table pre_forum_poll\G; #查看建表語句,加\G格式化好看點,不加顯示很亂#

*************************** 1. row ***************************

Table: pre_forum_poll

Create Table: CREATE TABLE `pre_forum_poll` (

`tid` mediumint(8) unsigned NOT NULL DEFAULT '0',

`overt` tinyint(1) NOT NULL DEFAULT '0',

`multiple` tinyint(1) NOT NULL DEFAULT '0',

`visible` tinyint(1) NOT NULL DEFAULT '0',

`maxchoices` tinyint(3) unsigned NOT NULL DEFAULT '0',

`isimage` tinyint(1) NOT NULL DEFAULT '0',

`expiration` int(10) unsigned NOT NULL DEFAULT '0',

`pollpreview` varchar(255) NOT NULL DEFAULT '',

`voters` mediumint(8) unsigned NOT NULL DEFAULT '0',

PRIMARY KEY (`tid`)

) ENGINE=MyISAM DEFAULT CHARSET=gbk #引擎,字符集#

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> select user(); #查看當前登錄的用戶#

+----------------+

| user() |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.01 sec)

mysql> select database(); #查看當前使用的庫#

+------------+

| database() |

+------------+

| discuz |

+------------+

1 row in set (0.00 sec)

mysql> create database db1; #建立數據庫#

Query OK, 1 row affected (0.01 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| db1 | #剛纔建立的數據庫#

| discuz |

| mysql |

| test |

+--------------------+

5 rows in set (0.00 sec)

mysql> use db1; #使用db1庫#

Database changed

mysql> create table `tab1`(`id` int(4), `name` char(50)) ENGINE=MyISAM DEFAULT CHARSET=gbk; #建立一個tab1表,表的第一個字段爲id,類型int爲整型最多爲四位,第二個字段是name,類型爲字符串類型,最多爲50個字符,後邊能夠指定它的引擎和字符集。#

Query OK, 0 rows affected (0.00 sec)

mysql> show create table tab1\G; #查看剛纔建的表tab1的語句#

*************************** 1. row ***************************

Table: tab1

Create Table: CREATE TABLE `tab1` (

`id` int(4) DEFAULT NULL,

`name` char(50) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=gbk

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> create table `tb2` (`id` int(4),`name` char(40)); #建立表tb2,不指定引擎和字符集#

Query OK, 0 rows affected (0.01 sec)

mysql> show create table tb2\G; #查看錶二的建表語句#

*************************** 1. row ***************************

Table: tb2

Create Table: CREATE TABLE `tb2` (

`id` int(4) DEFAULT NULL,

`name` char(40) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 #默認爲myisam引擎,字符集是拉丁,可在配置文件修改,爲防止亂碼,可在建表的時候,指定字符集或在配置文件修改字符集#

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> select version(); #查看數據庫版本#

+------------+

| version() |

+------------+

| 5.1.73-log |

+------------+

1 row in set (0.00 sec)

mysql> insert into tab1 (id,name)values(1,'wang'); #給表tab1插入數據,name爲字符,要加單引號#

Query OK, 1 row affected (0.00 sec)

mysql> insert tab1(id,name)values(2,'li');

Query OK, 1 row affected (0.00 sec)

mysql> insert tab1 (id)values(3); #只插id#

Query OK, 1 row affected (0.00 sec)

mysql> insert tab1 (name)values('zhang');#只插name#

Query OK, 1 row affected (0.00 sec)

mysql> select * from tab1; #查詢表tab1的數據#

+------+-------+

| id | name |

+------+-------+

| 1 | wang |

| 2 | li |

| 3 | NULL |

| NULL | zhang |

+------+-------+

4 rows in set (0.00 sec)

mysql> update tab1 set id=3 where name='zhang'; #更改表tab1的id字段是3,name字段是zhang的行#

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from tab1; #顯示錶中的數據#

+------+-------+

| id | name |

+------+-------+

| 1 | li |

| 2 | liu |

| 3 | zhang |

+------+-------+

3 rows in set (0.00 sec)

mysql> delete from tab1 where name='liu'; #刪除tab1表中name是liu 的行#

Query OK, 1 row affected (0.01 sec)

mysql> select * from tab1;

+------+-------+

| id | name |

+------+-------+

| 1 | li |

| 3 | zhang |

+------+-------+

2 rows in set (0.00 sec)

mysql> truncate table tab1; #清空tab1表中的全部數據但不刪除表#

Query OK, 0 rows affected (0.00 sec)

mysql> select * from tab1;

Empty set (0.00 sec)

mysql> drop table tab1; #刪除整個表#

Query OK, 0 rows affected (0.00 sec)

mysql> show databases; #查看數據庫#

+--------------------+

| Database |

+--------------------+

| information_schema |

| db1 |

| db2 |

| discuz |

| mysql |

| test |

+--------------------+

6 rows in set (0.00 sec)

mysql> drop database db2; #刪除數據庫db2#

Query OK, 0 rows affected (0.04 sec)

mysql> show databases; #顯示沒有db2了#

+--------------------+

| Database |

+--------------------+

| information_schema |

| db1 |

| discuz |

| mysql |

| test |

+--------------------+

5 rows in set (0.00 sec)

mysql> select version(); #顯示數據庫版本

+-----------+

| version() |

+-----------+

| 5.6.35 |

+-----------+

相關文章
相關標籤/搜索