MySQL基本操作
2014-08-26 14:56
162 查看
首先查看MySQL的帮助信息
mysql> help #查看可用的命令 For information about MySQL products and services, visit: http://www.mysql.com/ For developer information, including the MySQL Reference Manual, visit: http://dev.mysql.com/ To buy MySQL Enterprise support, training, or other products, visit: https://shop.mysql.com/ List of all MySQL commands: Note that all text commands must be first on line and end with ';' ? (\?) Synonym for `help'. clear (\c) Clear the current input statement. connect (\r) Reconnect to the server. Optional arguments are db and host. delimiter (\d) Set statement delimiter. edit (\e) Edit command with $EDITOR. ego (\G) Send command to mysql server, display result vertically. exit (\q) Exit mysql. Same as quit. go (\g) Send command to mysql server. help (\h) Display this help. nopager (\n) Disable pager, print to stdout. notee (\t) Don't write into outfile. pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. print (\p) Print current command. prompt (\R) Change your mysql prompt. quit (\q) Quit mysql. rehash (\#) Rebuild completion hash. source (\.) Execute an SQL script file. Takes a file name as an argument. status (\s) Get status information from the server. system (\!) Execute a system shell command. tee (\T) Set outfile [to_outfile]. Append everything into given outfile. use (\u) Use another database. Takes database name as argument. charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. warnings (\W) Show warnings after every statement. nowarning (\w) Don't show warnings after every statement. For server side help, type 'help contents' mysql> help contents #列出所有可以的topic You asked for help about help category: "Contents" For more information, type 'help <item>', where <item> is one of the following categories: Account Management Administration Compound Statements Data Definition Data Manipulation Data Types Functions Functions and Modifiers for Use with GROUP BY Geographic Features Help Metadata Language Structure Plugins Table Maintenance Transactions User-Defined Functions Utility mysql> help Data Manipulation #查看数据操作相关的帮助信息 You asked for help about help category: "Data Manipulation" For more information, type 'help <item>', where <item> is one of the following topics: CALL DELETE DO DUAL HANDLER INSERT INSERT DELAYED INSERT SELECT JOIN LOAD DATA REPLACE SELECT TRUNCATE TABLE UNION UPDATE mysql> help SELECT #查看SELECT关键字的帮助信息 Name: 'SELECT' Description: Syntax: SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]] SELECT is used to retrieve rows selected from one or more tables, and can include UNION statements and subqueries. See [HELP UNION], and http://dev.mysql.com/doc/refman/5.1/en/subqueries.html. The most commonly used clauses of SELECT statements are these: o Each select_expr indicates a column that you want to retrieve. There must be at least one select_expr. o table_references indicates the table or tables from which to retrieve rows. Its syntax is described in [HELP JOIN]. o The WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be selected. where_condition is an expression that evaluates to true for each row to be selected. The statement selects all rows if there is no WHERE clause. In the WHERE expression, you can use any of the functions and operators that MySQL supports, except for aggregate (summary) functions. See http://dev.mysql.com/doc/refman/5.1/en/expressions.html, and http://dev.mysql.com/doc/refman/5.1/en/functions.html. SELECT can also be used to retrieve rows computed without reference to any table. URL: http://dev.mysql.com/doc/refman/5.1/en/select.html[/code]
连接与断开 MySQL服务器(MySQL账号与当前Linux系统上面的账号没有关系)[root@localhost ~]# mysql -h localhost -u root -p -h代表MySQL服务器运行的主机名,-u代表登陆MySQL所使用的用户名,-p代表登陆MySQL的密码 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.1.52 Source distribution 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> quit #退出MySQL,'\q'和Ctrl+D也可以退出MySQL连接 ByeMySQL的一个命令通常是由SQL语句组成,随后跟着一个分号。(有些例外不需要分号,比如上面提到的quit)SQL关键字不区分大小写,而数据库相关的名称是区分大小写。
显示当前MySQL服务器所有的数据库mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql>
访问数据库或者选择数据库mysql> USE test #后面可以加分号,通常情况下不加 Database changed
创建数据库mysql> CREATE DATABASE menagerie; mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | menagerie | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> USE menagerie Database changed mysql> SHOW TABLES; #刚创建的数据库是空的 [code]Empty set (0.00 sec)
创建表,并插入数据mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), ->sex CHAR(1), birth DATE, death DATE); mysql> DESCRIBE pet; #查看表pet的描述信息 +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> INSERT INTO pet VALUES('Puffball', 'Diane', 'hamster', 'f', '1999-03-30', NULL); #插入数据到表pet mysql> select * from pet; #查看表pet的信息 +----------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+-------+ | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +----------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
从表中检索信息mysql> SELECT * FROM pet; #显示表pet所有信息 +----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+------------+ | Puffball | Diane | hamster | f | 1999-03-30 | NULL | | Fulffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+--------+---------+------+------------+------------+ 5 rows in set (0.00 sec) mysql> SELECT * FROM pet WHERE species='cat' AND sex='m'; 检索种类为猫类,并且性别为雄性的行 +-------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+-------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | +-------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec) mysql> SELECT name, birth FROM pet; #显示部分列,这里显示名称和出生日期 +----------+------------+ | name | birth | +----------+------------+ | Puffball | 1999-03-30 | | Fulffy | 1993-02-04 | | Claws | 1994-03-17 | | Bowser | 1979-08-31 | | Slim | 1996-04-29 | +----------+------------+ 5 rows in set (0.00 sec)模式匹配:友情链接:MySQL正则表达式
MySQL提供标准的SQL模式匹配,以及一种基于想Unix实用程序如vi,grep和sed的扩展正则表达式模式匹配的格式。SQL模式匹配使用“_"匹配任何单个字符,而”%"匹配任意数目字符(包括零字符),以及SQL的模式匹配是忽略大小写的。mysql> SELECT * FROM pet WHERE birth LIKE '199%'; #模式匹配出生日期是1990~1999之间的行,这里使用LIKE模式匹配关键字,也可以使用REGEXP关键字 +----------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+-------+ | Puffball | Diane | hamster | f | 1999-03-30 | NULL | | Fulffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | +----------+--------+---------+------+------------+-------+ 4 rows in set, 1 warning (0.02 sec)
计数行:mysql> SELECT COUNT(*) FROM pet; #显示总共的行数 +----------+ | COUNT(*) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec) mysql> SELECT species, COUNT(*) FROM pet GROUP BY species; #显示种类列和计数列,并按照种类分类 +---------+----------+ | species | COUNT(*) | +---------+----------+ | cat | 2 | | dog | 1 | | hamster | 1 | | snake | 1 | +---------+----------+ 4 rows in set (0.00 sec)
查询当前选择了哪个数据库:mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | menagerie | +------------+ 1 row in set (0.00 sec)
参考:http://dev.mysql.com/doc/refman/5.1/zh/tutorial.html
相关文章推荐
- MYSQL常用命令(二) 基本操作命令
- MySQL的基本操作
- mysql基本操作
- MySQL常用操作基本
- MySQL的基本操作
- MYSQL安装与基本操作
- Mysql 基本操作 引用:http://www.blogjava.net/TrampEagle/archive/2006/01/07/26978.html
- Linux下mysql基本操作小结
- MySQL基本操作
- 2009-03-04读书记录:《网页制作与PHP语言应用》第六章——MySQL基本操作
- mysql基本操作
- MySQL常用命令及基本操作总结
- MySQL常用命令及基本操作总结
- MySQL常用命令及基本操作总结
- MySQL的基本操作命令
- Ubuntu Linux:MySQL安装与MYSQL基本操作
- Mysql常识与基本操作
- Mysql 基本命令操作
- MySQL常用操作基本操作
- php操作mysql的几条基本命令