zoukankan      html  css  js  c++  java
  • php数据库操作命令精华大全

    1、表结构//列信息2、表数据//行信息3、表索引//把列中的行加到索引中(一般情况下一个表一定要把id这一列的所有数据都加到主键索引中)

    2、[dos下]关闭mysql:net stop mysql
    开启mysql:net start mysql
    登陆mysql:mysql -uroot -p123 --tee=c:mysql.log
    查看数据库命令:show database;
    进入test数据库:use test
    查看数据库表:show tables;
    创建一个表:create table user(id int,name varchar(30),pass varchar(30));
    查看表结构或表字段:desc user
    查看表数据:select*from user;
    查看表中的所有索引:show index from t2;
    在表中插入数据:insert into user(id,name,pass) values(1,"hello","123");
    查看表中的id:select*from user where id=1;
    删除表中的id:delete from user where id=1;
    修改表中的值:update user set name='hello' where id=1;
    退出myaql:exit;
    创建一个数据库:create database txet;
    删除数据库:drop database;
    修改表名:rename table user to user1;
    删除表:drop table user1;

     

    表字段的类型:
    1、数值:int//int(3)与长度无关,不够3位时前面补0,默认不显示
    float
    2、字符串:char(n) 255字节 占用n个字节
    varchar(n)最高65535字节 存多少占多少字节
    text 65535字节
    longtext 42亿字节
    3、日期:date time datetime year

     

    数据库的基础篇字段属性:
    1、unsigned 无符号,全是正数
    2、zenrofill 零填充,int(3),不够3位补0
    3、auto_increment 自增
    4、null 这一列值允许为null
    5、not null 这一列值不允许为null
    6、default 不允许为null给默认值

     

    用s查看四种字符集:
    1、server characterset: utf8 服务器字符集
    2、Db characterset: utf8 数据库字符集
    3、client characterset: utf8 客户端字符集
    4、Conn. characterset: utf8 客户端连接字符集
    查看数据库字符集命令:sohw create database text;
    查看表字符集命令:show create table user;
    PHP中设置mysql客户端和连接字符集:$sql="set names utf8";

     

    表字段索引:
    1、主键索引
    2、普通索引
    检查sql语句:desc select * from user where id=3G//加G把表颠倒一下
    rows 1 表示找到一个id=3的人检索一行找到
    查看表中的所有索引:show index from user;

     

    后期维护普通索引:
    1、添加普通索引:alter table t2 add index in_name(name);
    2、删除普通索引:alter table t2 drop index in_name;

     

    后期维护数据库字段:
    1、添加字段
    alter table t1 add age int;
    2、修改字段
    alter table t1 modify age int not null default 20;
    3、删除字段
    alter table t1 drop age;
    4、修改字段名
    alter table t1 change name username varchar(30);

     

    结构化查询语言sql包含四个部分:
    1、DDL //数据定义语言,reate,drop,alter
    2、DML //数据操作语言,insert,update,delete
    3、DQL //数据查询语言,select
    4、DCL //数据控制语句,grant,commit,rollback

     

    增-insert:insert into t1(username) values('g');
    改-update:update t1 set username='f' where id=6;
    一次更改多个值:update t1 set id=10, username='bb' where id =7;
    删-delete:
    delete from t1 where id=6;
    delete from t1 where id in(1,2,5);
    delete from t1 where id=1 or id=3 or id=5;
    delete from t1 where id>=3 and id<=5;
    delete from t1 where id between 3 and 5;
    查-select:

    1、选择特定的字段:select id,name from user where id=3;
    2、给字段取别名-as:select pass as p,id from user where id=3;
    select pass p,id from user where id=3;
    3、取掉列中的重复值:select distinct name form user;
    4、使用where条件进行查询:select * from user where id>=3 and id<=5;
    5、查询空值null:select *from user where pass is null;
    select *from user where pass is not null;
    6、搜索like关键字:select * form user wher name like '%3%';
    select * form user wher name like '%3%' or name like '%1%';
    select * form user wher name regexp '.*3.*';
    select * form user wher name regexp '(.*3.*)|(.*5.*)';
    7、使用order by对查询结果排序:
    升序 select * from user ordeer by id asc;注:升序可以不写asc
    降序 select * from user ordeer by id desc;
    8、使用limit限定输出个数:
    select * from user order by id desc limit 0,3;
    select * from user order by id desc limit 3;
    9、concat函数-字符串连接符:select concat("a","-","c");
    10、rand函数-随机排序:selec 8 from user order by rans() limit 3;
    11、count统计:select count(*) from user;/ /http://www.pprar.com
    select count(id) from user;
    select count(id) from user where name='user1';//统计user1出现的次数
    12、sum求和:select sum(id) from user where name='user1';//符合要求的id 之和
    13、avg平均数: select avg(id) from user;
    14、max最大值: select max(id) from user;
    15、min最小值: select min(id) from user;
    16、分组聚合:select name,count(id) tot from mess group by name order by tot desc;//tot是起了个别名//group by必须写在order by的前面
    select name,count(id) tot from mess group by name having tot>=5;//group by必须写在having的前面//分组后加条件必须用having,而非where
    17、在id字段后加uid字段:alter table post add uid int after id;
    18、多表查询:
    (1)普通查询-多表
    (2)嵌套查询-多表
    (3)左连接查询-多表
    普通查询:
    select user.name,count(post.id) from user,post where user.id=post.uid group by post.uid;
    左连接查询:
    select user.name,post.title,post.content from user left join post on user.id=post.uid;//显示全部用户发送或者没有发送的名字加内容
    普通查询:
    //得到发帖子的人-普通查询mysql> select distinct user.name from user,post where user.id=post.uid;
    嵌套查询:
    //得到发帖子的人-嵌套查询mysql> select name from user where id in(select uid from post);

    PHP操作数据库:


    1、通过PHP连接上mysql数据库
    2、选择数据库
    3、通过PHP进行insert操作
    4、通过PHP进行delete操作
    5、通过PHP进行update操作
    6、通过PHP进行select操作
    通过PHP连接mysql数据库:mysql_connect("localhost","root","123");
    选择数据库:mysql_select_db("test");
    设置客户端和连接字符集:mysql_query("set names utf8");
    通过PHP进行insert操作://从表单接收数据$uername="user1"; $passwoed="123";
    //$sql="insert into t1(username,password) values('$username','$password')";
    //执行这条mysql语句 var_dump(mysql_query($sql));
    //释放连接资源 mysql_close($conn);
    //通过PHP进行update操作$sql="update t1 set username='user2',pssword='111' where id=8";
    //通过PHP进行delete操作:$sql="delete form t1 where id=8";
    从结果集中取数据:
    mysql_fetch_assoc//关联数组
    mysql_fetch_row//索引数组
    mysql_fetch_array//混合数组
    mysql_fetch_object//对象
    //通过PHP进行select操作$sql="select form t1";
    从结果集中取全部数据:while($row=mysql_fech_assoc($result)){echo "

    "; print_r($row) echo "

    ";}
    mysql_insert_id//取得上一步INSERT操作产生的ID
    mysql_num_fields//得到insert,update,delete操作影响的行数musql_num_rows//得到select操作影响的行数
    //得到表总行数:$sql="select count(*) form t1";
    $rst=mysql_query($sql);
    $tot=mysql_fetch_row($rst);

  • 相关阅读:
    HDU 2116 Has the sum exceeded
    HDU 1233 还是畅通工程
    HDU 1234 开门人和关门人
    HDU 1283 最简单的计算机
    HDU 2552 三足鼎立
    HDU 1202 The calculation of GPA
    HDU 1248 寒冰王座
    HDU 1863 畅通工程
    HDU 1879 继续畅通工程
    颜色对话框CColorDialog,字体对话框CFontDialog使用实例
  • 原文地址:https://www.cnblogs.com/php0368/p/4035048.html
Copyright © 2011-2022 走看看