@(實用技術)[資料庫]
SQL資料庫
什麼是SQL
SQL(structured query language):結構化查詢語言
SQL是一種對關系型資料庫中的資料進行定義和操作的語言
SQL語言簡潔,文法簡單,好學好用
常用關系型資料庫
PC端:Oracle、MySQL、SQL Server、Access、DB2、Sybase
嵌入式\移動用戶端:SQLite
術語
輕量級
完全配置時小于 400K,省略可選功能配置時小于 250K
- 字段(Col / Field):一個字段存儲一個值,可以存儲 INTEGER, REAL, TEXT, BLOB, NULL 五種類型的資料
- SQLite 在存儲時,本質上并不區分準确的資料類型
- 資料庫主要的目的是做資料的檢索,通常不會把無法檢索的二進制資料儲存在資料庫中
- 主鍵:Primary Key,唯一标示一條記錄的字段,具有以下特點:
- 名字:xxx_id
- 類型:Integer
- 自動增長
- 準确數值由資料庫決定,程式員不用關心
- 外鍵:Foreign Key,對應其他關系表的标示,利用外鍵 可以和另外一個表建立起"關系"
- 友善資料維護
- 節約存儲空間
字段類型
空(NULL):該值為空
整型(INTEGEER):有符号整數,按大小被存儲成1,2,3,4,6或8位元組。
實數(REAL):浮點數,以8位元組指數形式存儲。
文本(TEXT):字元串,以資料庫編碼方式存儲(UTF-8, UTF-16BE 或者 UTF-16-LE)。
BLOB:BLOB資料不做任何轉換,以輸入形式存儲。(二進制資料,比如檔案)
ps: 在關系資料庫中,CLOB和BLOB類型被用來存放大對象。BOLB表示二進制大對象,這種資料類型通過用來儲存圖檔,圖象,視訊等。CLOB表示字元大對象,能夠存放大量基于字元的資料。
SQL語句的特點
不區分大小寫(比如資料庫認為user和UsEr是一樣的)
每條語句都必須以分号 ; 結尾
SQL中的常用關鍵字有
select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等
資料庫中不可以使用關鍵字來命名表、字段
SQL語句的種類
- 資料定義語句(DDL:Data Definition Language)
- 包括
和create
等操作drop
- 在資料庫中建立新表或删除表(
或create table
)drop table
- 包括
- 資料操作語句(DML:Data Manipulation Language)
- 包括
、insert
、update
等操作delete
- 上面的3種操作分别用于添加、修改、删除表中的資料
- 包括
- 資料查詢語句(DQL:Data Query Language)
- 可以用于查詢獲得表中的資料
- 關鍵字
是DQL(也是所有SQL)用得最多的操作select
- 其他DQL常用的關鍵字有
,where
,order by
和group by
having
常用SQL語句
建立表(create)
//建立表如果不存在的話
create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …);
//建立表
create table t_student (id integer, name text, age inetger, score real);
//簡寫
create table t_student(name, age);
實際上SQLite是無類型的,是以可以簡寫 create table t_student(name, age);
為了保持良好的程式設計規範、友善程式員之間的交流,編寫建表語句的時候最好加上每個字段的具體類型
删除表(drop)
//删除表,如果存在的話
drop table if exists t_student
//删除表
drop table t_student;
插入資料(insert)
//insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
//删除表
insert into t_student (name, age) values ('lnj', 10);
注意:資料庫中的字元串内容應該用 單引号'
包覆
更新資料(update)
//update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
//更新資料
update t_student set name = ‘jack’, age = 20 ;
注意:上面的示例會将t_student表中 所有記錄
的name都改為jack,age都改為20
删除資料(delete)
//delete from 表名 ;
//删除表内容
delete from t_student ;
上面的示例會将t_student表中 所有記錄
都删掉
條件語句
如果隻想或
更新
某條固定的記錄,那麼就必須在DML語句後面加上一些條件
删除
where 字段 = 某個值 ; // 不能用兩個 =
where 字段 is 某個值 ; // is 相當于 =
where 字段 != 某個值 ;
where 字段 is not 某個值 ; // is not 相當于 !=
where 字段 > 某個值 ;
where 字段1 = 某個值 and 字段2 > 某個值 ; // and相當于C語言中的 &&
where 字段1 = 某個值 or 字段2 = 某個值 ; // or 相當于C語言中的 ||
示例
//将t_student表中年齡大于10 并且 姓名不等于jack的記錄,年齡都改為 5
update t_student set age = 5 where age > 10 and name != ‘jack’ ;
//删除t_student表中年齡小于等于10 或者 年齡大于30的記錄
delete from t_student where age <= 10 or age > 30 ;
//猜猜下面語句的作用
update t_student set score = age where name = ‘jack’ ;
//将t_student表中名字等于jack的記錄,score字段的值 都改為 age字段的值
查詢語句(select) *****
select 字段1, 字段2, … from 表名 ;
select * from 表名; // 查詢所有的字段
select name, age from t_student ;
select * from t_student ;
select * from t_student where age > 10 ; // 條件查詢
别名**
select 字段1 别名 , 字段2 别名 , … from 表名 别名 ;
select 字段1 别名, 字段2 as 别名, … from 表名 as 别名 ;
select 别名.字段1, 别名.字段2, … from 表名 别名 ;
字段和表都可以起别名
示例
select name myname, age myage from t_student ;
//給name起個叫做myname的别名,給age起個叫做myage的别名
select s.name, s.age from t_student s ;
//給t_student表起個别名叫做s,利用s來引用表中的字段
常用複合語句*****
計算記錄的數量**
select count (字段) from 表名 ;
select count ( * ) from 表名 ;
注意:該語句是複合語句
//計算age數量
select count (age) from t_student ;
//計算分數大于等于60的數量
select count ( * ) from t_student where score >= 60;
排序*****
查詢出來的結果可以用order by進行排序
select * from t_student order by 字段 ;
select * from t_student order by age ;
預設是按照升序排序(由小到大),也可以變為降序(由大到小)
select * from t_student order by age desc ; //降序
select * from t_student order by age asc ; // 升序(預設)
也可以用多個字段進行排序
select * from t_student order by age asc, height desc ;
//先按照年齡排序(升序),年齡相等就按照身高排序(降序)
limit限制搜尋範圍***
使用limit可以精确地控制查詢結果的數量,比如每次隻查詢10條資料
select * from 表名 limit 數值1, 數值2 ;
select * from t_student limit 4, 8 ;
//可以了解為:跳過最前面4條語句,然後取8條記錄
limit常用來做分頁查詢,比如每頁固定顯示5條資料,那麼應該這樣取資料
第1頁:limit 0, 5
第2頁:limit 5, 5
第3頁:limit 10, 5
…
第n頁:limit 5*(n-1), 5
select * from t_student limit 7 ;
//相當于select * from t_student limit 0, 7 ;
//表示取最前面的7條記錄
限制
簡單限制**
建表時可以給特定的字段設定一些限制條件,常見的限制有
- not null :規定字段的值不能為null
- unique :規定字段的值必須唯一
- default :指定字段的預設值
建議:盡量給字段設定嚴格的限制,以保證資料的規範性)
示例
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
//name字段不能為null,并且唯一
//age字段不能為null,并且預設為1
主鍵限制****
什麼是主鍵?
- 主鍵(Primary Key,簡稱PK)用來唯一地辨別某一條記錄
- 例如t_student可以增加一個id字段作為主鍵,相當于人的身份證
- 主鍵可以是一個字段或多個字段
良好的資料庫程式設計規範應該要保證每條記錄的唯一性,為此,增加了主鍵限制,也就是說,每張表都必須有一個主鍵,用來辨別記錄的唯一性
主鍵的設計原則
- 主鍵應當是對使用者沒有意義的
- 永遠也不要更新主鍵
- 主鍵不應包含動态變化的資料
- 主鍵應當由計算機自動生成
主鍵的聲明
- 在創表的時候用primary key聲明一個主鍵
create table t_student (id integer primary key, name text, age integer) ;
//integer類型的id作為t_student表的主鍵
- 主鍵的字段
- 隻要聲明為primary key,就說明是一個主鍵字段
- 主鍵字段預設就包含了not null 和 unique 兩個限制
- 如果想讓自增長,應該增加
autoincrement
create table t_student (id integer primary key autoincrement, name text, age integer) ;
外鍵限制****
什麼是外鍵限制?作用?
利用外鍵限制可以用來建立表與表之間的聯系
外鍵的一般情況是:一張表的某個字段,引用着另一張表的主鍵字段
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_t_student_class_id_t_class_id foreign key (class_id) references t_class (id)) ;
/-------------------------------------------------/
//t_student表中有一個叫做fk_t_student_class_id_t_class_id的外鍵
//這個外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段
表連接配接查詢
什麼是表連接配接查詢?
需要聯合多張表才能查到想要的資料。
表連接配接的類型
内連接配接:inner join 或者 join (顯示的是左右表都有完整字段值的記錄)
左外連接配接:left outer join (保證左表資料的完整性)
//示例
//查詢iOS11班級的所有學生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘iOS11’;
轉載于:https://www.cnblogs.com/MJchen/p/5916341.html