侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL数据库操作的基本命令

数据库  /  管理员 发布于 8年前   453

一、创建数据库:

 create data data _name;

 php中创建数据库的两种方法:(mysql_create_db(),mysql_query())

 $conn = mysql_connect(“localhost”,”username”,”password”) or die ( “could not connect to localhost”); mysql_create_db(“data _name”) or die (“could not create data ”); $string = “create data data _name”; mysql_query( $string) or die (mysql_error()); 

二、选定数据库

在创建表之前,必须要选定要创建的表所在的数据库

选定数据库:

 通过命令行客户端:

use data _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 data ”);

三、创建表

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 data ”); $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 firstalter 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 data data _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参数C>


  • 上一条:
    解决mysql创建数据库后出现:Access denied for user 'root'@'%' to database 'xxx'的问题
    下一条:
    Linux CentOS MySQL数据库安装配置教程
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客