日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php数据库操作命令精华大全

發布時間:2023/12/20 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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=3\G//加\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);

?

轉載于:https://www.cnblogs.com/php0368/p/4035048.html

總結

以上是生活随笔為你收集整理的php数据库操作命令精华大全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。