天天看点

Mysql数据库基本命令大全

1、当需要通过yum安装mysql数据库

首先需要进行yum源的更新

[root@server ~]# yum install mysql-community-server mysql-community-devel

2、源码包安装

3、创建数据库

<code>mysql&gt; create database database_name </code><code>default</code> <code>character set utf8;</code>

4、选定数据库

<code>mysql&gt; use database_name;</code>

5、创建表

<code>mysql&gt; create table table_name</code>

<code>  </code><code>-&gt;(</code>

<code>  </code><code>-&gt;column_1 column_type column attributes,</code>

<code>  </code><code>-&gt;column_2 column_type column attributes,</code>

<code>  </code><code>-&gt;column_3 column_type column attributes,</code>

<code>  </code><code>-&gt;primary key (column_name),</code>

<code>  </code><code>-&gt;index index_name(column_name)</code>

<code>  </code><code>-&gt;) engine=innodb </code><code>default</code> <code>charset=utf8 auto_increment=1;</code>

6、创建索引

<code>mysql&gt; alter table table_name add index index_name(column_name);</code>

<code>mysql&gt; create index index_name on table_name(column_name);</code>

<code>mysql&gt; create unique index index_name on table_name(column_name);              </code><code>#建立唯一索引</code>

7、修改表

1)更改表名

<code>mysql&gt; alter table table_name rename new_table_name;</code>

2)添加列

<code>mysql&gt; alter table table_name add column column_name colomn attributes;</code>

例如: 

<code>mysql&gt; alter table my_table add column my_column text not </code><code>null</code><code>;</code>

    first 指定插入的列位于表的第一列

    after 把新列放在已经存在的列的后面

例如:           

<code>mysql&gt; alter table my_table add column my_col text not </code><code>null</code> <code>first;</code>

<code>mysql&gt; alter table my_table add column my_col text not </code><code>null</code> <code>after my_other _column;</code>

3)删除列

<code>mysql&gt; alter table table_name drop column column_name;</code>

4)添加索引

<code>mysql&gt; alter table table_name add index index_name (column_name1,column_name2,……);</code>

<code>mysql&gt; alter table table_name add unique index_name (column_name);</code>

<code>mysql&gt; alter table table_name add primary key(my_column);</code>

     删除索引

<code>mysql&gt; alter table table_name drop index index_name;</code>

 如:

<code>mysql&gt; alter table test10 drop primary key;</code>

5)更改列定义

用change或是modify命令可以更改列的名称或是属性。要更改列的名称,还必须重新定义列的属性。例如:

<code>mysql&gt; alter table table_name change original_column_name new_column_name int not </code><code>null</code><code>;</code>

  注意:必须要重新定义列的属性!!!

<code>mysql&gt; alter table table_name modify col_1 clo_2 varchar(200);</code>

8、插入表

<code>mysql&gt; insert into table_name (column_1,column_2,…..)  values (value1,value2,……);</code>

如果要存入字符串,则需要使用单引号“’”将字符串括起来,但是需要注意字符的转意

如:

<code>mysql&gt; insert into table_name (text_col,int_col) value (\’hello world\’,1);</code>

需要转义的字符有:单引号’ 双引号”  反斜杠\  百分号%  下划线_

可以连续使用两个单引号转义单引号

9、更新表

<code>mysql&gt; updata table_name set col__1=vaule_1 where col=vaule;</code>

10、删除表/库

<code>mysql&gt; drop table table_name;</code>

<code>mysql&gt; drop database database_name;</code>

11、查看表/库

<code>mysql&gt; show tables;</code>

<code>mysql&gt; show databases;</code>

12、查看列的属性和类型

<code>mysql&gt; show columns from table_name;</code>

<code>mysql&gt; show fields  from table_name;</code>

13、查找语句

<code>mysql&gt; select column_1,column_2,column_3 from table_name;</code>

14、修改密码

<code>mysql&gt; update mysql.user set authentication_string=password(</code><code>'123456'</code><code>) where user=</code><code>'root'</code> <code>and Host = </code><code>'localhost'</code><code>;</code>

<code>mysql&gt; alter user root@localhost identified by </code><code>'123456'</code><code>;</code>

<code>mysql&gt; UPDATE user SET Password=PASSWORD(</code><code>'123456'</code><code>) where USER=’root’;</code>

<code>mysqladmin  -uroot  -p  old_password  password  new_password</code>

15、用户授权

<code>mysql&gt; GRANT ALL PRIVILEGES ON mysql.* TO tom@% identified by </code><code>'123456'</code><code>;</code>

第一个*号 是代表所有表 ,第二个* 是代表改数据库下的所有表。

16、使用where

限制从查询(select)返回的记录行

<code>mysql&gt; select * from table_name where user_id = 2;</code>

 如果要对存储字符串(char、varchar等类型)的列进行比较,就需要在where子句中用单引号把要比较的字符串括起来

<code>mysql&gt; select * from users where city =‘San Francisco’;</code>

 通过向where子句添加and或是or,可以一次比较几个运算符

<code>mysql&gt; select * from users where userid=1 or city=‘San Francisco’;</code>

<code>mysql&gt; select 8 from users where state=’CA’ and city=’San Francisco’;</code>

 注意:空值不能和表中的任何运算符比较,对于空值,需要使用is null或是is not null谓词

<code>mysql&gt; select * from users where zip!=’1111′ or zip=’1111′ or zip is </code><code>null</code><code>;</code>

 如果要找到包含任何值(除空值以外)的所有记录,可以

<code>mysql&gt; select * from table_name where zip is not </code><code>null</code><code>;</code>

17、使用between

 使用between可以选择在某个范围内的值,between可用于数字,日期,文本字符串。

<code>mysql&gt; select * from users where lastchanged between 20000614000000 and 20000614235959;</code>

<code>mysql&gt; select * from users where lname between ‘a’ and ‘m’;</code>

18、使用in/not in

 若某列可能返回好几个可能的值,就可以使用in谓词

<code>mysql&gt; select * from users where state=’RI’ or state=’NH’ or state=’VT’ or state=’MA’ or state=’ME’;</code>

    可改写为:

<code>mysql&gt; select * from users where state </code><code>in</code> <code>(‘RI’,</code><code>'NH’,'</code><code>VY’,</code><code>'MA’,'</code><code>ME’);</code>

如果要达到相同的结果,但结果集相反,可使用not in 谓词

<code>mysql&gt; select * from user where state not </code><code>in</code> <code>(‘RI’,</code><code>'NH’,'</code><code>VT’,</code><code>'MA’,'</code><code>ME’);</code>

19、使用like

如果需要使用通配符,则要使用like

<code>mysql&gt; select * from users where fname like ‘Dan%’;    </code><code>#%匹配零个字符</code>

<code>mysql&gt; select * from users where fname like ‘J___’;    </code><code>#匹配以J开头的任意三字母词</code>

<code>                                          </code><code>#mysql中like不区分字母大小写</code>

20、order by

 order by语句可以指定查询中返回的行的顺序,可对任意列类型排序,通过在末尾放置asc或是desc以设置按升序或是降序排列,如果不设置,默认使用asc

<code>mysql&gt; select * from users order by lname,fname;</code>

 可以按照需要根据任意多的列排序,也可以混合使用asc和desc

<code>mysql&gt; select * from users order by lname asc, fname desc;</code>

21、limit

 limit限制从查询中返回的行数,可以指定开始的行数和希望返回的行数

  得到表中的前5行:

<code>mysql&gt; select * from users limit 0,5;</code>

<code>mysql&gt; select * from users order by lname,fname limit 0,5;</code>

  得到表的第二个5行:

<code>mysql&gt; select * from users limit 5,5;</code>

22、group by 与聚合函数

 使用group by后Mysql就能创建一个临时表,记录下符合准则的行与列的所有信息

 count()   计算每个集合中的行数

<code>mysql&gt; select state,count(*) from users group by state;</code>

   *号指示应该计算集合中的所有行

<code>mysql&gt; select count(*) from users;</code>

计算表中所有的行数

可以在任何函数或列名后使用单词as,然后指定一个作为别名的名称。如果需要的列名超过一个单词,就要使用单引号把文本字符串括起来

 sum() 返回给定列的数目

 min() 得到每个集合中的最小值

 max() 得到每个集合中的最大值

 avg() 返回集合的品均值

 having

 限制通过group by显示的行,where子句显示在group by中使用的行,having子句只限制显示的行。

23、连接表

 在select语句的from部分必须列出所有要连接的表,在where部分必须显示连接所用的字段。

<code>mysql&gt; select * from companies,contacts where companies.company_ID=contacts.company_ID;</code>

 当对一个字段名的引用不明确时,需要使用table_name.column_name语法指定字段来自于哪个表

      本文转自MQ_douer 51CTO博客,原文链接:http://blog.51cto.com/douer/1934762,如需转载请自行联系原作者