SQL基礎(chǔ)01

什么是SQL

     SQL(structured query language):結(jié)構(gòu)化查詢語(yǔ)言
    SQL是一種對(duì)關(guān)系型數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行定義和操作的語(yǔ)言
    SQL語(yǔ)言簡(jiǎn)潔,語(yǔ)法簡(jiǎn)單,好學(xué)好用
    在程序運(yùn)行過(guò)程中,要想操作(增刪改查,CRUD)數(shù)據(jù)庫(kù)中的數(shù)據(jù),必須使用SQL語(yǔ)句
    Create , Retrive, Update, Delete

SQL中常用的關(guān)鍵字

    select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等

SQL中的語(yǔ)句的種類

    數(shù)據(jù)定義語(yǔ)句(DDL:Data Definition Language)
    數(shù)據(jù)操作語(yǔ)句(DML:Data Manipulation Language)
    數(shù)據(jù)查詢語(yǔ)句(DQL:Data Query Language)
    其他DQL常用的關(guān)鍵字有where,order by,group by和having

SQL中約束

簡(jiǎn)單約束:

     ①不能為空  not  null
     ②不能重復(fù)   unique
     ③默認(rèn)值  default

示例:

create table t_dog (id integer, name test not null unique,age integer not null default 60);

主鍵:

Primary Key,簡(jiǎn)稱PK 用來(lái)唯一地標(biāo)示某一條記錄,可以有多個(gè)字段或一個(gè)字段
列如t_dog 可以添加一個(gè)id作為主鍵,相當(dāng)于狗的身份證

添加主鍵約束原因:

  為了保持每條記錄的唯一性,增加了主鍵約束

主鍵設(shè)計(jì)原則:

        ①對(duì)用戶沒(méi)有意義
        ②永遠(yuǎn)不要更新
        ③不應(yīng)包含動(dòng)態(tài)變化的數(shù)據(jù)
        ④應(yīng)當(dāng)由計(jì)算機(jī)自動(dòng)生成

主鍵的聲明:

    在創(chuàng)表的時(shí)候用Primary Key 聲明一個(gè)主鍵
    例如:create table t_dog (id integer primary key,name text, age integer);integer類型的id作為t_dog表的主鍵
    如果想要主鍵自動(dòng)增長(zhǎng)(必須為integer類型),應(yīng)當(dāng)增加autoincrement
    例如:create table t_dog(id integer primary key autoincrement,name text,age integer);

DDL語(yǔ)句

創(chuàng)表

格式:create table 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
示例:create table t_dog (id integer, name text, age inetger, score real) ;
經(jīng)驗(yàn)語(yǔ)句優(yōu)化:
創(chuàng)建表格時(shí), 最好加個(gè)表格是否已經(jīng)存在的判斷, 這個(gè)防止語(yǔ)句多次執(zhí)行時(shí)發(fā)生錯(cuò)誤
create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;

DML語(yǔ)句

插入數(shù)據(jù)(insert)

  格式:insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
  示例:insert into t_student (name, age) values (‘zs’, 10) ;
  注意:數(shù)據(jù)庫(kù)中的字符串內(nèi)容應(yīng)該用單引號(hào) ’ 括住

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

    格式:update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
    示例:update t_student set name = ‘zs’, age = 20 ;
    注意:上面的示例會(huì)將t_student表中所有記錄的name都改為zs,age都改為20

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

    格式:delete from 表名 ;
    示例:delete from t_dog ;
    注意:上面的示例會(huì)將t_student表中所有記錄都刪掉

DQL語(yǔ)句

格式:

    select 字段1, 字段2, … from 表名 ;
    select * from 表名;   //  查詢所有的字段

示例:

    select name, age from t_dog ;
    select * from t_dog ;
    select * from t_dog where age > 10 ;  //  條件查詢

條件語(yǔ)句

    作用:如果只想更新或者刪除某些固定的記錄,那就必須在DML語(yǔ)句后加上一些條件

條件語(yǔ)句的常見格式

    where 字段 = 某個(gè)值 ;   // 不能用兩個(gè) =
    where 字段 is 某個(gè)值 ;   // is 相當(dāng)于 =
    where 字段 != 某個(gè)值 ;
    where 字段 is not 某個(gè)值 ;   // is not 相當(dāng)于 !=
    where 字段 > 某個(gè)值 ;
    where 字段1 = 某個(gè)值 and 字段2 > 某個(gè)值 ;  // and相當(dāng)于C語(yǔ)言中的 &&
    where 字段1 = 某個(gè)值 or 字段2 = 某個(gè)值 ;  //  or 相當(dāng)于C語(yǔ)言中的 ||

條件語(yǔ)句練習(xí)

示例:
 ①將t_dog表中年齡大于10 并且 姓名不等于zs的記錄,年齡都改為 5
  update t_dog set age = 5 where age > 10 and name != ‘zs’ ;
  ②將t_dog表中名字等于li的記錄,score字段的值 都改為 age字段的值
  update t_dog set score = age where name = ‘li’ ;

查詢相關(guān)語(yǔ)句

統(tǒng)計(jì)

  count(X)
  select count(*) from t_student
  計(jì)算所有記錄個(gè)數(shù)

  select count(age) from t_student
  計(jì)算age有值的記錄個(gè)數(shù)(Null不計(jì)算在內(nèi))

  avg(X):計(jì)算某個(gè)字段的平均值
  sum(X):計(jì)算某個(gè)字段的總和
  max(X):計(jì)算某個(gè)字段的最大值
  min(X):計(jì)算某個(gè)字段的最小值

排序:

    查詢出來(lái)的結(jié)果可以用order by進(jìn)行排序
    select 字段1, 字段2 from 表名 order by 字段 ;
    select * from t_student order by age ;

    默認(rèn)是按照升序排序(由小到大),也可以變?yōu)榻敌颍ㄓ纱蟮叫。?    select * from t_student order by age desc ;  //降序
    select * from t_student order by age asc ;   // 升序(默認(rèn))

    也可以用多個(gè)字段進(jìn)行排序
    select * from t_student order by age asc, height desc ;
    先按照年齡排序(升序),年齡相等就按照身高排序(降序)

limit分頁(yè)

  使用limit可以精確地控制查詢結(jié)果的數(shù)量,比如每次只查詢10條數(shù)據(jù)
  select * from 表名 limit 數(shù)值1, 數(shù)值2 ;
  示例:select * from t_student limit 4, 8 ;
  可以理解為:跳過(guò)最前面4條語(yǔ)句,然后取8條記錄

分頁(yè)

    limit常用來(lái)做分頁(yè)查詢,比如每頁(yè)固定顯示5條數(shù)據(jù),那么應(yīng)該這樣取數(shù)據(jù)
    第1頁(yè):limit 0, 5
    第2頁(yè):limit 5, 5
    第3頁(yè):limit 10, 5
    第n頁(yè):limit 5*(n-1), 5

    特殊案例:select * from t_student limit 7 ;
    相當(dāng)于select * from t_student limit 0, 7 ;
    表示取最前面的7條記錄
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容