• 技术福利:mysql数据库的基本命令汇总整理


    一、创建数据库:

    create database database_name;
    php中创建数据库的两种方法:(mysql_create_db(),mysql_query())
    $conn = mysql_connect(“localhost”,”username”,”password”) or
    die ( “could not connect to localhost”);
    1.
    mysql_create_db(“database_name”) or
    die (“could not create database”);
    2.
    $string = “create database database_name”;
    mysql_query( $string) or
    die (mysql_error());

    二、选定数据库
    在创建表之前,必须要选定要创建的表所在的数据库
    选定数据库:
    通过命令行客户端:use database_name
    通过php: mysql_select_db()
    $conn = mysql_connect(“localhost”,”username”,”password”) or
    die ( “could not connect to localhost”);
    mysql_select_db(“test”,$conn) or
    die (“could not select database”);
    三、创建表
    create table table_name
    如:
    create table table_name
    (
    column_1 column_type column attributes,
    column_2 column_type column attributes,
    column_3 column_type column attributes,
    primary key (column_name),
    index index_name(column_name)
    )
    在命令行客户端需要键入整个命令
    在php中使用,mysql_query()函数
    如:
    $conn = mysql_connect(“localhost”,”username”,”password”) or
    die ( “could not connect to localhost”);
    mysql_select_db(“test”,$conn) or
    die (“could not select database”);
    $query = “create table my_table (col_1 int not null primary key,
    col_2 text
    )”;
    mysql_query($query) or
    die (mysql_error());

    四、创建索引
    index index_name(indexed_column)

    五、表的类型
    ISAM MyISAM BDB Heap
    声明表类型的语法:
    create table table_name type=table_type
    (col_name column attribute);
    默认使用MyISAM
    六、修改表
    alter table table_name
    更改表名
    alter table table_name rename new_table_name
    或者(高版本中)
    rename table_name to new_table_name
    添加和删除列
    添加列:alter table table_name add column column_name colomn attributes
    例如: alter table my_table add column my_column text not null
    first 指定插入的列位于表的第一列
    after 把新列放在已经存在的列的后面
    例如:alter table my_table add column my_next_col text not null first
    alter table my_table add column my_next_col text not null after my_other _column
    删除列:alter table table_name drop column column name
    添加和删除索引:
    alter table table_name add index index_name (column_name1,column_name2,……)
    alter table table_name add unique index_name (column_name)
    alter table table_name add primary key(my_column)
    alter table table_name drop index index_name
    如:alter table_name test10 drop primary key
    更改列定义:
    用change或是modify命令可以更改列的名称或是属性。要更改列的名称,还必须重新定义列的属性。例如:
    alter table table_name change original_column_name new_column_name int not null
    注意:必须要重新定义列的属性!!!
    alter table table_name modify col_1 clo_1 varchar(200)

    七、向表中输入信息(insert)
    insert into table_name (column_1,column_2,column_3,…..)
    values (value1,value2,value3,……)
    如果要存入字符串,则需要使用单引号“’”将字符串括起来,但是需要注意字符的转意
    如:insert into table_name (text_col,int_col) value (’hello world’,1)
    需要转义的字符有:单引号’ 双引号” 反斜杠 百分号% 下划线_
    可以连续使用两个单引号转义单引号

    八、updata语句
    updata table_name set col__1=vaule_1,col_1=vaule_1 where col=vaule
    where部分可以有任何比较运算符
    如:
    table folks
    id fname iname salary
    1 Don Ho 25000
    2 Don Corleone 800000
    3 Don Juan 32000
    4 Don Johnson 44500
    updata folks set fname=’Vito’ where id=2
    updata folks set fname=’Vito’ where fname=’Don’
    updata folks set salary=50000 where salary<50000

    九、删除表、数据库
    drop table table_name
    drop database database_name
    在php中可以通过mysql_query()函数使用drop table命令
    在php中删除数据库需要使用mysql_drop_db()函数

    十、列出数据库中所有可用表(show tables)
    注意:使用该命前必须先选定数据库
    在php中,可以使用mysql_list_tables()得到表中的清单

    十一、查看列的属性和类型
    show columns from table_name
    show fields from table_name
    使用mysql_field_name()、mysql_field_type()、mysql_field_len()可以得到类似信息!

    十二、基本的select语句
    要求指出进行选择的表,以及要求的列名称。若要选定所有的列,可用*代表所有的字段名
    select column_1,column_2,column_3 from table_name
    或者
    select * from table_name
    用mysql_query()可向Mysql发送查询

    十三、where子句
    限制从查询(select)返回的记录行
    select * from table_name where user_id = 2
    如果要对存储字符串(char、varchar等类型)的列进行比较,就需要在where子句中用单引号把要比较的字符串括起来
    如:select * from users where city = ‘San Francisco’
    通过向where子句添加and或是or,可以一次比较几个运算符
    select * from users where userid=1 or city=’San Francisco’
    select 8 from users where state=’CA’ and city=’San Francisco’
    注意:空值不能和表中的任何运算符比较,对于空值,需要使用is null或是is not null谓词
    select * from users where zip!=’1111′ or zip=’1111′ or zip is null
    如果要找到包含任何值(除空值以外)的所有记录,可以
    select * from table_name where zip is not null

    十四、使用distinct
    当使用distinct时,Mysql引擎将删除有一样结果的行。
    select distinct city,state from users where state=’CA’

    十五、使用between
    使用between可以选择在某个范围内的值,between可用于数字,日期,文本字符串。
    如:
    select * from users where lastchanged between 20000614000000 and 20000614235959
    select * from users where lname between ‘a’ and ‘m’
    十六、使用in/not in
    若某列可能返回好几个可能的值,就可以使用in谓词
    select * from users where state=’RI’ or state=’NH’ or state=’VT’ or state=’MA’ or state=’ME’
    可改写为:select * from users where state in (‘RI’,'NH’,'VY’,'MA’,'ME’)

    如果要达到相同的结果,但结果集相反,可使用not in 谓词
    select * from user where state not in (‘RI’,'NH’,'VT’,'MA’,'ME’)

    十七、使用like
    如果需要使用通配符,则要使用like
    select * from users where fname like ‘Dan%’ %匹配零个字符
    select * from users where fname like ‘J___’ 匹配以J开头的任意三字母词
    Mysql中like不区分字母大小写

    十八、order by
    order by语句可以指定查询中返回的行的顺序,可对任意列类型排序,通过在末尾放置asc或是desc以设置按升序或是降序排列,如果不设置,默认使用asc
    select * from users order by lname,fname
    可以按照需要根据任意多的列排序,也可以混合使用asc和desc
    select * from users order by lname asc, fname desc
    十九、limit
    limit限制从查询中返回的行数,可以指定开始的行数和希望返回的行数
    得到表中的前5行:
    select * from users limit 0,5
    select * from users order by lname,fname limit 0,5
    得到表的第二个5行:
    select * from users limit 5,5
    二十、group by 与聚合函数
    使用group by后Mysql就能创建一个临时表,记录下符合准则的行与列的所有信息
    count() 计算每个集合中的行数
    select state,count(*) from users group by state
    *号指示应该计算集合中的所有行
    select count(*) from users
    计算表中所有的行数
    可以在任何函数或列名后使用单词as,然后指定一个作为别名的名称。如果需要的列名超过一个单词,就要使用单引号把文本字符串括起来
    sum() 返回给定列的数目
    min() 得到每个集合中的最小值
    max() 得到每个集合中的最大值
    avg() 返回集合的品均值
    having
    限制通过group by显示的行,where子句显示在group by中使用的行,having子句只限制显示的行。


    二十一、连接表
    在select语句的from部分必须列出所有要连接的表,在where部分必须显示连接所用的字段。
    select * from companies,contacts where companies.company_ID=contacts.company_ID
    当对一个字段名的引用不明确时,需要使用table_name.column_name语法指定字段来自于哪个表

    二十二、多表连接
    在select后面添加额外的列,在from子句中添加额外的表,在where子句中添加额外的join参数–>

  • 相关阅读:
    ASP.NET MVC中获取URL地址参数的两种写法
    SQL Server之存储过程基础知识
    ASP.NET MVC 四种Controller向View传值方法
    Js数据类型、Json格式、Json对象、Json字符串
    调用微信内置的方法及wx.config的配置问题
    ref和out的使用及区别
    ASP.NET MVC post请求接收参数的三种方式
    Asp.Net Mvc 路由机制
    Asp.Net MVC中Action跳转小结
    JS应用MD5散列计算头像URL
  • 原文地址:https://www.cnblogs.com/frombyte/p/6861640.html
Copyright © 2020-2023  润新知