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

歡迎訪問 生活随笔!

生活随笔

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

php

PHP从零开始--字段修饰符数据操作SQL语言

發(fā)布時間:2023/12/13 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP从零开始--字段修饰符数据操作SQL语言 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

      • 一、 字段修飾符
        • 1.1主鍵
        • 1.2自動增長
        • 1.3非空
        • 1.4默認值
        • 1.5外鍵
      • 二、 對數(shù)據(jù)的操作
        • 2.1增加數(shù)據(jù)
        • 2.2刪除數(shù)據(jù)
        • 2.3更新數(shù)據(jù)
        • 2.4查詢數(shù)據(jù)
          • 2.4.1查詢所有的數(shù)據(jù)
          • 2.4.2查詢指定字段
          • 2.4.3去除重復字段
          • 2.4.4where表達式詳解
          • 2.4.5分組查詢
          • 2.4.6排序
      • 三、 SQL語言
          • 3.1DML
          • 3.2DDL
          • 3.3DCL

一、 字段修飾符

1.1主鍵

主鍵使用primary key修飾 ,它是唯一,可以防止用戶插入相同的數(shù)據(jù)
如果字段加上主鍵修飾符,如果再加入重復的數(shù)據(jù)會報錯。

1.2自動增長

auto_increment 是自動增長的修飾符,用戶沒插入一條數(shù)據(jù),修飾的字段會自動增加
這樣創(chuàng)建的表插入數(shù)據(jù)默認是從1開始的

1.3非空

not null 如果字段用not null 修飾了,但是插入數(shù)據(jù)的時候這個字段沒有給值會報錯
create table student (sid int primary key auto_increment,sname varchar(20) not null,sex char(2));

1.4默認值

default 數(shù)字
或者default 字符串
意思是用戶如果某個字段設(shè)置了默認值,如果插入數(shù)據(jù)的時候這個字段沒有給值就采用默認值
create table student (sid int primary key auto_increment,sname varchar(20) not null,sex char(2) default ‘男’);

1.5外鍵

其中一個表中的字段用primary key修飾,它叫主鍵,主鍵所在的表叫主表,另外一張表也有一個字段和主表中的主鍵相關(guān)聯(lián),tid叫做student表的外鍵。

用外鍵約束,如果刪除一個表中的數(shù)據(jù),會提示報錯,這樣就保證了數(shù)據(jù)的一致性和完整性。
完整代碼如下:
創(chuàng)建教師表:
create table teacher (tid int primary key auto_increment,tname varchar(20));

創(chuàng)建學生外鍵表:

create table student(sid int primary key auto_increment,sname varchar(20),age int,tid int,foreign key(tid) references teacher(tid));

二、 對數(shù)據(jù)的操作

2.1增加數(shù)據(jù)

insert into 表名 (字段1,字段2…) values (值1,值2…) ;

或者同時插入多條數(shù)據(jù)

2.2刪除數(shù)據(jù)

delete from 表名 where 條件表達式

delete from yuan where name =‘jerry’;

2.3更新數(shù)據(jù)

update 表名 set 字段=新值,字段2=新值… where 條件
必須加條件 否則整個表的數(shù)據(jù)都會更新

update yuan set name=‘a(chǎn)aa’,sal=6000 where id=3;

2.4查詢數(shù)據(jù)

2.4.1查詢所有的數(shù)據(jù)

select * from 表名

select * from yuan;

2.4.2查詢指定字段

select 字段1,字段2… from 表名

select id,name,sal from yuan;

2.4.3去除重復字段

select distinct 字段名 from 表名;

select distinct name from yuan;

2.4.4where表達式詳解

算術(shù)表達式:+ - * / %
比較運算符: > >= < <= <>表示不等 =表示等于
邏輯運算符: and or not
(1) 查詢工資大于3000
select * from 表名 where sal>3000;

select * from yuan where sal>3000;

(2) 查詢工資不等于6000
select * from yuan where sal<>6000;

(3) 查詢工資等于6000
select * from yuan where sal=6000
(4) 查詢姓名是jim而且性別是女的信息
select * from yuan where name=’jim’ and sex=’女’;
(5) 查詢不是女生的信息
select * from yuan where not sex=’女’;

(6) 查詢工作在3000和5000之間的
between 表示范圍 相當于>=3000 and <=5000
select * from yuan where sal between 3000 and 6000;

(7) 查詢地區(qū)是保定 南京 滄州

in(值1,值2,值3) 表示是里面的任意一個值

(8) like 模糊查詢
like后面可以跟字符串表示模糊查詢,%代表任意字符,一個_代表一個字符

比如查詢姓名叫張x 張xx

select * from yuan where name like ‘張%’;

比如查詢叫x華 xx華 最后一個字是華
select * from yuan where name like ‘%華’;

比如查詢中間帶華的
select * from yuan where name like ‘%華%’;

2.4.5分組查詢

count(*) 統(tǒng)計數(shù)量
max(字段名)最大值
min(字段名)最小值
sum(字段名)求和
avg(字段名)求平均值

數(shù)據(jù)表信息如下:

? 求男生和女生的人數(shù)
select sex,count(*) from yuan group by sex;

? 求部門工資的最大值
select b_id,max(sal) from yuan group by b_id;

? 求每個地區(qū)的平均工資是多少
select area,avg(sal) from yuan group by area;

? 分組以后再加條件用having
查詢按地區(qū)分組然后平均工資小于等于6000的信息
select area,avg(sal) from yuan group by area having avg(sal)<=6000;

2.4.6排序

(1)升序和降序
按工資排序 取前三名

desc是降序 asc是升序
select * from yuan order by sal desc;

select * from yuan order by sal desc limit 3;

(2)limit詳解
limit后面可以跟兩個參數(shù),第一個參數(shù)是開始的索引號,默認是0開始,第二個參數(shù)取的個數(shù)(長度)
select * from yuan order by sal desc limit 0,3;

三、 SQL語言

SQL(structured query language)是結(jié)構(gòu)化查詢語言的意思,mysql、oracle用的都是sql語言,只不過根據(jù)軟件的不同兒語法稍有區(qū)別,就和我們的普通話和方言的意思是一樣的。
上面的增刪改查 對數(shù)據(jù)庫和表的操作等都是sql語句。
SQL語言

3.1DML

DML(data manipulation language)數(shù)據(jù)操縱語言:就是我們最經(jīng)常用到的 SELECT、UPDATE、INSERT、DELETE。 主要用來對數(shù)據(jù)庫的數(shù)據(jù)進行一些操作。

SELECT 列名稱 FROM 表名稱 UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值 INSERT INTO table_name (1,2,...) VALUES (1,2,....) DELETE FROM 表名稱 WHERE 列名稱 =
3.2DDL

DDL(data definition language)數(shù)據(jù)庫定義語言:其實就是我們在創(chuàng)建表的時候用到的一些sql,比如說:CREATE、ALTER、DROP等。DDL主要是用在定義或改變表的結(jié)構(gòu),數(shù)據(jù)類型,表之間的鏈接和約束等初始化工作上

CREATE TABLE 表名稱 ( 列名稱1 數(shù)據(jù)類型, 列名稱2 數(shù)據(jù)類型, 列名稱3 數(shù)據(jù)類型, .... )ALTER TABLE table_name ALTER COLUMN column_name datatypeDROP TABLE 表名稱 DROP DATABASE 數(shù)據(jù)庫名稱
3.3DCL

DCL(Data Control Language)數(shù)據(jù)庫控制語言:是用來設(shè)置或更改數(shù)據(jù)庫用戶或角色權(quán)限的語句,包括(grant,deny,revoke等)語句。這個比較少用到。

在公司呢一般情況下我們用到的是DDL、DML這兩種。

總結(jié)

以上是生活随笔為你收集整理的PHP从零开始--字段修饰符数据操作SQL语言的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。