php数据库操作命令精华大全
1、表結(jié)構(gòu)//列信息2、表數(shù)據(jù)//行信息3、表索引//把列中的行加到索引中(一般情況下一個(gè)表一定要把id這一列的所有數(shù)據(jù)都加到主鍵索引中)
2、[dos下]關(guān)閉mysql:net stop mysql
開(kāi)啟mysql:net start mysql
登陸mysql:mysql -uroot -p123 --tee=c:\mysql.log
查看數(shù)據(jù)庫(kù)命令:show database;
進(jìn)入test數(shù)據(jù)庫(kù):use test
查看數(shù)據(jù)庫(kù)表:show tables;
創(chuàng)建一個(gè)表:create table user(id int,name varchar(30),pass varchar(30));
查看表結(jié)構(gòu)或表字段:desc user
查看表數(shù)據(jù):select*from user;
查看表中的所有索引:show index from t2;
在表中插入數(shù)據(jù):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;
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù):create database txet;
刪除數(shù)據(jù)庫(kù):drop database;
修改表名:rename table user to user1;
刪除表:drop table user1;
?
表字段的類型:
1、數(shù)值:int//int(3)與長(zhǎng)度無(wú)關(guān),不夠3位時(shí)前面補(bǔ)0,默認(rèn)不顯示
float
2、字符串:char(n) 255字節(jié) 占用n個(gè)字節(jié)
varchar(n)最高65535字節(jié) 存多少占多少字節(jié)
text 65535字節(jié)
longtext 42億字節(jié)
3、日期:date time datetime year
?
數(shù)據(jù)庫(kù)的基礎(chǔ)篇字段屬性:
1、unsigned 無(wú)符號(hào),全是正數(shù)
2、zenrofill 零填充,int(3),不夠3位補(bǔ)0
3、auto_increment 自增
4、null 這一列值允許為null
5、not null 這一列值不允許為null
6、default 不允許為null給默認(rèn)值
?
用\s查看四種字符集:
1、server characterset: utf8 服務(wù)器字符集
2、Db characterset: utf8 數(shù)據(jù)庫(kù)字符集
3、client characterset: utf8 客戶端字符集
4、Conn. characterset: utf8 客戶端連接字符集
查看數(shù)據(jù)庫(kù)字符集命令:sohw create database text;
查看表字符集命令:show create table user;
PHP中設(shè)置mysql客戶端和連接字符集:$sql="set names utf8";
?
表字段索引:
1、主鍵索引
2、普通索引
檢查sql語(yǔ)句:desc select * from user where id=3\G//加\G把表顛倒一下
rows 1 表示找到一個(gè)id=3的人檢索一行找到
查看表中的所有索引:show index from user;
?
后期維護(hù)普通索引:
1、添加普通索引:alter table t2 add index in_name(name);
2、刪除普通索引:alter table t2 drop index in_name;
?
后期維護(hù)數(shù)據(jù)庫(kù)字段:
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);
?
結(jié)構(gòu)化查詢語(yǔ)言sql包含四個(gè)部分:
1、DDL //數(shù)據(jù)定義語(yǔ)言,reate,drop,alter
2、DML //數(shù)據(jù)操作語(yǔ)言,insert,update,delete
3、DQL //數(shù)據(jù)查詢語(yǔ)言,select
4、DCL //數(shù)據(jù)控制語(yǔ)句,grant,commit,rollback
?
增-insert:insert into t1(username) values('g');
改-update:update t1 set username='f' where id=6;
一次更改多個(gè)值: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、取掉列中的重復(fù)值:select distinct name form user;
4、使用where條件進(jìn)行查詢: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關(guān)鍵字: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對(duì)查詢結(jié)果排序:
升序 select * from user ordeer by id asc;注:升序可以不寫asc
降序 select * from user ordeer by id desc;
8、使用limit限定輸出個(gè)數(shù):
select * from user order by id desc limit 0,3;
select * from user order by id desc limit 3;
9、concat函數(shù)-字符串連接符:select concat("a","-","c");
10、rand函數(shù)-隨機(jī)排序:selec 8 from user order by rans() limit 3;
11、count統(tǒng)計(jì):select count(*) from user;/ /http://www.pprar.com
select count(id) from user;
select count(id) from user where name='user1';//統(tǒng)計(jì)user1出現(xiàn)的次數(shù)
12、sum求和:select sum(id) from user where name='user1';//符合要求的id 之和
13、avg平均數(shù): 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是起了個(gè)別名//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;//顯示全部用戶發(fā)送或者沒(méi)有發(fā)送的名字加內(nèi)容
普通查詢:
//得到發(fā)帖子的人-普通查詢mysql> select distinct user.name from user,post where user.id=post.uid;
嵌套查詢:
//得到發(fā)帖子的人-嵌套查詢mysql> select name from user where id in(select uid from post);
PHP操作數(shù)據(jù)庫(kù):
1、通過(guò)PHP連接上mysql數(shù)據(jù)庫(kù)
2、選擇數(shù)據(jù)庫(kù)
3、通過(guò)PHP進(jìn)行insert操作
4、通過(guò)PHP進(jìn)行delete操作
5、通過(guò)PHP進(jìn)行update操作
6、通過(guò)PHP進(jìn)行select操作
通過(guò)PHP連接mysql數(shù)據(jù)庫(kù):mysql_connect("localhost","root","123");
選擇數(shù)據(jù)庫(kù):mysql_select_db("test");
設(shè)置客戶端和連接字符集:mysql_query("set names utf8");
通過(guò)PHP進(jìn)行insert操作://從表單接收數(shù)據(jù)$uername="user1"; $passwoed="123";
//$sql="insert into t1(username,password) values('$username','$password')";
//執(zhí)行這條mysql語(yǔ)句 var_dump(mysql_query($sql));
//釋放連接資源 mysql_close($conn);
//通過(guò)PHP進(jìn)行update操作$sql="update t1 set username='user2',pssword='111' where id=8";
//通過(guò)PHP進(jìn)行delete操作:$sql="delete form t1 where id=8";
從結(jié)果集中取數(shù)據(jù):
mysql_fetch_assoc//關(guān)聯(lián)數(shù)組
mysql_fetch_row//索引數(shù)組
mysql_fetch_array//混合數(shù)組
mysql_fetch_object//對(duì)象
//通過(guò)PHP進(jìn)行select操作$sql="select form t1";
從結(jié)果集中取全部數(shù)據(jù):while($row=mysql_fech_assoc($result)){echo "
";}
mysql_insert_id//取得上一步INSERT操作產(chǎn)生的ID
mysql_num_fields//得到insert,update,delete操作影響的行數(shù)musql_num_rows//得到select操作影響的行數(shù)
//得到表總行數(shù):$sql="select count(*) form t1";
$rst=mysql_query($sql);
$tot=mysql_fetch_row($rst);
?
轉(zhuǎn)載于:https://www.cnblogs.com/php0368/p/4035048.html
總結(jié)
以上是生活随笔為你收集整理的php数据库操作命令精华大全的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Matplotlib下载和安装
- 下一篇: 动态规划_数字三角形