mysql 操做

来源:互联网 发布:剑灵小秦夕妍捏脸数据 编辑:程序博客网 时间:2024/06/01 09:00
<?php1,链接数据库mysql -h localhost -u root -ppassword:修改数据库密码:一:  mysqladmin -u root -p password 新密码  Enter password:原密码二:  REPLACE INTO mysql.user (Host,User,Password)  VALUES(%,用户,PASSWORD(密码));FLUSH PRIVILEGES    刷新权限或者重启2,数据库的操作create database 数据库表名;             数据库名称drop database 数据库表名;      数据库名show databases;                   显示数据库use                                 数据库名称3,表操作show tables;                                                         显示所有表desc 表名                                                                显示表的结构drop table 表名                                                          删除表(有索引的时候不允许删除)      delete from 表名                                                                删除表show create table  数据表名;                                 显示创建表语句      show index from 表名;                                                    显示一个数据表的所有索引      optimize table 表名                                                             清理碎片      truncate table 表名;                                                          清空表。比delete效率高。创建表#  若遇系统关键字做字段 用 反引号(tab键与1旁边的键)drop table if exists 表名;    #存在此表就删除create table 表名 (id int(11) unsigned not null auto_increment,  # unsigned 要写在not null 的前面字段 decimal(10,2) not null comment '商品价格',字段 varchar(30) not null comment '',字段 char(32) not null comment '',字段 smallint not null comment '',字段 mediumint not null commment '中等的',字段 tinyint not null comment '',字段 enum(0,1,2) not null comment '枚举',字段 date not null comment '时间格式就是 1970-01-01',字段 text not null comment '文本类型',字段 datetime not null comment '事件类型',字段 blob not null comment '二进制类型',  primary key (`id`),         //主键  括号里面的用反引号不会出错。  key `name` (`name`)     //创建index索引,搜索更快。  )engine=InnoDB charset=utf8 collate utf8_general_ci;engine=MyISAM TYPE=InnoDB 也是可以的。 字段的数据类型  数值 tinyint int float double decimal(10,2) smallint  字符串 char varchar text  时间和日期 date datetime  通常使用int存储时间戳  null  zerofill 前补0  //-------------------------------------------------------------级联完整性,一个表的数据处理另一个表的数据也自动处理 ,    engine=InnoDB   类型。create table type( id int(11) unsigned not null auto_increment parimary key); create table attribute(    #   未测试主键可不可以做外键    type_id smallint unsigned not null comment '类型',     foreign key (type_id[]) references type(id) on delete cascade,   #数据库级联完整性  # 外键     key type_id(type_id),  #外键索引        #     type(id) on delete  |||  on update           #  cascade  |||   restrict[不删]  |||   set null ||| no Action [什么也不干],                              );//--------------------------------------------------------4,数据操作:    查询                            select * from 表名; //查询表中的所欲数据                    select count(*) from 表名;  //统计表中有多少条数据                    //order by 排序 默认asc 正序        desc 倒序                   select * from 表名 where 条件  order by  字段1 desc,字段2 asc  //where条件排序。                   select * from 表名 order by 字段1 asc,字段2 desc,字段3 desc                     //   like匹配,两边是单引号,_一次 %任意次 not like不匹配                    select * from user where name like '%aa%';                       select * from 表名 where 1(随便一个数字就是查询所有的   或者一个空的字符串‘’)and like '{%aa%}';         查询所有的数据。   //搜索词分页使用。                      //   子查询   子查询中返回的结果不能使集合,而是一行结果。使用最多的是 in                    select * from 表一 where 字段=(select 字段 from 表二 where 字段=值);                    select * from 表一 where 字段 in (select 字段 from 表二 where 字段=值);                     // 多表查询  as语句不容易混字段。where条件是避免出现笛卡尔乘积出现。                    //复合条件查询就是where有多个条件用 and 链接。                    select 别名1.字段,别名2.字段 from 表一 别名1,表二 别名2 where 别名1.字段=别名2.字段。                    //limit 数字:取几条数据     limit 数字1,数字2:数1是偏移量   数2是条数                        select * from 表名  where  条件  limit offset,pagesize;//分页   limit 数字  取几条数据。                    // group by 分组    字段1,字段2 可以用*代替         having  分组后在继续排序                     select 字段1,字段2 from 表名 group by 字段                     select 字段1,字段2 from 表名 group by 字段 having 字段 <=> 值;                     //In查询                     select 字段 from 表名 where 字段 in(1,2,3,4);                       更新                      update 表名  set  字段名='值',字段名='值' where 条件(=   <     >);    插入:                    insert into 表名(字段1,字段2)values('值1','值2');                    把表的数据查出来再插入到表中。                    insert into 表名(字段1,字段2) select 对应的字段名 from 表名2 where 条件 limit 条数;    删除:                    delete from 表名 where 条件(=   <     >);    授权:                    // 所有都可以访问的形式                    grant select,insert,update,delete on *.* to 用户名@"%" Identified by "密码";                    // grant select,insert,update,delete on book.* to lingsi@"%" Identified by "abc123";                     // 限定的形式,localhost ,127.0.0.1                    grant select,insert,update,delete on book.* to 用户名@限定(127.0.0.1 || localhost) Identified by "密码";                    // grant select,insert,update,delete on book.* to 用户名@localhost Identified by "123456"; 1,修改字段:   alter table 数据表名   change 原字段名 新字段名 类型  属性 索引;change比modify强   alter table 数据表名  modify 字段名 类型 属性 索引;   alter table 数据表名  add 字段名 类型  属性 索引;   alter table 表名 add 字段 int(11) not null default 0;   alter table 数据表名 drop  字段名;   alter table 原数据表名 rename as 新表名;       给表重新命名   alter table 表名 engine = InnoDB;       修改表引擎   alter table goods_cat add index(`字段`);3,索引的操作   alter table 表名 add  index/unique/  索引名称(字段)   alter table 表名 add  primary key(字段)   alter table 表名 drop  index/unique 索引名称;   show indexes from 表名;   PRIMARY, INDEX, UNIQUE 这3种是一类PRIMARY 主键。 就是 唯一 且 不能为空。INDEX 索引,普通的,加快速度UNIQUE 唯一索引。 不允许有重复。FULLTEXT 是全文索引,用于在一篇文章中,检索文本信息的。对中文支持不好,不用      key   还有复合索引inner jion       left join       right jion       outer jion           full join扩展:数据库备份:mysqldump -h localhost -u root -p 数据库名>物理路径\文件名(c:123.sql);导入备份数据//-p  密码 -u 用户 -h 主机  --opt -d 表结构  -t只有数据mysql -h localhost -u root -p 数据库名<物理路径\文件名;//到处数据和表结构mysqldump --opt -d 数据库 -u root >c:12.sql; //导出表结构 --opt -dAs别名的用法,字段名之间用  (别名.字段名)  得到别名用   (表名as别名)as可以省略   select 别名.name,别名.age from 表名 别名;distinct关键字,使用select语句去除结果中重复的记录。只返回一个。默认为 All 不用写。select dietinct 字段名 from 表名。查询版本,select version();数值计算,select 10*10;普通连写方式,select version(),10+10;as连写,select version() MYSQL_VERSION,10*10 EXPRESSION;order by用法:排序查询:将查询到的结果按照指定字段顺序排序,如果第一个字段不能排列出顺序先后,就会按照后一个字段进行排序describe 表名 ,显示表结构。次数统计count() count(*) count(字段)求和    sum()平均数     avg()最大值   max()最小值     min()字段的拼接   concat()  如 update user set name=concat(name,id);  name名是name+id版本号     version()mysql  常用函数mysql_connect('localhost','root','密码');mysql_pconnect();     长连接。mysql_affected_rows()     取得前一次mysql操作锁影响的行数mysql_errno() — 返回上一个 MySQL 操作中的错误信息的数字编码mysql_error() — 返回上一个 MySQL 操作产生的文本错误信息mysql_fetch_array() — 从结果集中取得一行作为关联数组,或数字数组,或二者兼有mysql_fetch_assoc() — 从结果集中取得一行作为关联数组mysql_free_result() — 释放结果内存mysql_info() — 取得最近一条查询的信息 mysql_insert_id() — 取得上一步 INSERT 操作产生的 ID 如果AUTO_INCREMENT 的列的类型是 BIGINT,则 mysql_insert_id() 返回的值将不正确。可以在 SQL 查询中用 MySQL 内部的 SQL 函数 LAST_INSERT_ID() 来替代mysql_query() — 发送一条 MySQL 查询mysql_result() — 取得结果数据mysql_set_charset('utf8') — 设置客户端的字符集mysql_close()                   关闭数据库。判断操作是否成功:1,插入操作mysql_insert_id()>02,删除操作mysql_affected_rows()>03,修改操作mysql_affected_rows()>04,查询mysql_num_rows($result)>0数据库函数:取得当前时间可用 now() 函数格式化时间字符串用DATE_FORMA T( )。 +++++++++++++++++++++++++++++select+++++++++++++++++++++++++header("content-type:text/html;charset=utf-8");一:链接数据库$link = @mysql_connect('localhost','root','') or die("链接数据库失败");【这里写 or die()就不写errno函数】if(mysql_errno()){    //判断是否链接成功  mysql_error(); 返回数据操作的错误信息  echo '链接数据库失败'.mysql_error();   //mysql_error(); 返回数据操作的错误信息  exit();}二:选择数据库mysql_select_db('bbs85');三:,设置字符集//等同于mysql_query('set names utf8')mysql_set_charset('utf8');四:准备sql语句$sql = "select * from bbs_user";五:,发送SQL语句,并返回执行结果$result = mysql_query($sql);六:判断执行结果if(mysql_num_rows($result)>0){   //mysql_num_rows($result); 返回结果集中数据的条数  七:处理结果集  while($row = mysql_fetch_assoc($result)){    echo '<tr>';    echo '<td>'.$row['id'].'</td>';    echo '<td>'.$row['username'].'</td>';    echo '<td>'.$row['email'].'</td>';    echo '<td>'.$row['regtime'].'</td>';    echo '<td><a href="edit.php?id='.$row['id'].'">修改</a>|<a href="delete.php?id='.$row['id'].'">删除</a></td>';    echo '</tr>';  }}八:释放结果集关闭数据库mysql_free_result($result);   // 释放结果集只针对select查询mysql_close();++++++++++++++++++++++++++++++update++++delete++++++++++++++++++++++header("content-type:text/html;charset=utf-8");$id = intval($_GET['id']);if(empty($id)){  echo '<script type="text/javascript">alert("非法参数");window.location="01link.php";</script>';  exit();}//1,链接数据库$link = @mysql_connect('localhost','root','');//判断是否链接成功if(mysql_errno()){  echo '链接数据库失败'.mysql_error();  exit();}$sql = "update bbs_user set email='$email' where id='$id'";   更新$sql = "delete from bbs_user where id='$id'";           删除$result = mysql_query($sql);//mysql_affected_rows()或去最近一次操作,所影响的数据条数 update,delete if(mysql_affected_rows()>0){  echo '<script type="text/javascript">alert("修改成功");window.location="01link.php";</script>';}else{  echo '<script type="text/javascript">alert("修改失败");window.location="edit.php?id='.$id.'";</script>';}mysql_close();+++++++++++++++++++++++++++注册用户(是否被注册)+++++++++++++++++++++++++1,获取用户的输入 2,链接数据库 3,查询是否被注册 4,注册header("content-type:text/html;charset=utf-8");$username = trim($_POST['username']);$email = trim($_POST['email']);$link = mysql_connect('localhost','root','') or die("链接数据库失败");mysql_select_db('bbs85');mysql_set_charset('utf8');//查询用户名是否被注册$sql = "select * from bbs_user where username='$username'"; $result = mysql_query($sql);if(mysql_num_rows($result)>0){  echo '<script type="text/javascript">alert("该用户名已经被注册,请重新注册");window.location="add.php";</script>';  exit();}$sql = "insert into bbs_user(username,email)values('$username','$email')";$result = mysql_query($sql);if(mysql_insert_id()>0){  echo '<script type="text/javascript">alert("添加用户成功");window.location="01link.php";</script>';}else{  echo '<script type="text/javascript">alert("添加用户失败");window.location="01link.php";</script>';}mysql_close();++++++++++++++++++++++++/多个服务器数据操作方式++++++++++++++++++++++++++$link1 = mysql_connect('localhost','root','') or die("数据库1链接失败");$link2 = mysql_connect("192.168.120.49",'root','') or die('数据2链接失败');mysql_select_db('bbs85',$link1);mysql_select_db('bbs85',$link2);mysql_set_charset('utf8',$link1);mysql_set_charset('gbk',$lik2)$sql = "select * from bbs_user";$result1 = mysql_query($sql,$link1);$result2 = mysql_query($sql,$link2);if($result1 && mysql_num_rows($result1)>0){  $row1 = mysql_fetch_assoc($result1);}if($result2 && mysql_num_rows($result2)>0){  $row2 = mysql_fetch_assoc($result2);}mysql_close($link1);mysql_close($link2);+++++++++++++++++++++++++++++++++++++++++++++++++++++++++  //$row = mysql_fetch_assoc($result); 返回关联数组,下标是字段名  //$row = mysql_fetch_row($result); 返回索引数组,下标是字段的索引位置  //$row = mysql_fetch_array($result); 返回混合数组 ,下标是字段的值和索引位置  //$row = mysql_fetch_object($result);从结果集中取得一行作为对象// mixed mysql_result ( resource $result , int $row [, mixed $field ] )            资源$result,      偏移量   字段名(字段表.字段名)//取得结果集中字段的数目echo mysql_num_fields($result); 

0 0
原创粉丝点击