SQL學(xué)習(xí)

1、SQL簡介

SQL:Structured Query Language的縮寫(結(jié)構(gòu)化查詢語言)是一種定義、操作、管理關(guān)系數(shù)據(jù)庫的句法

  • 結(jié)構(gòu)化查詢語言的工業(yè)標(biāo)準(zhǔn)由ANSI(美國國家標(biāo)準(zhǔn)學(xué)會(huì),ISO的成員之一)維護(hù)。

    • DQL:數(shù)據(jù)查詢語言
    • DML:數(shù)據(jù)操作語言
    • DDL:數(shù)據(jù)定義語言
    • DCL:數(shù)據(jù)控制語言
    • TPL:事務(wù)處理語言
    • CCL:指針控制語言
  • 優(yōu)點(diǎn):最大的優(yōu)點(diǎn)共享數(shù)據(jù)、安全性有很大的保障,但是也不是絕對(duì)的安全(黑客)、操作數(shù)據(jù)很容易

2、常用數(shù)據(jù)庫

Oracle
DB2
Informix
Sybase
SQL Server
ProstgreSQL面向?qū)ο髷?shù)據(jù)庫
MySQL
Access
SQLite等

3、數(shù)據(jù)庫服務(wù)器、數(shù)據(jù)庫和表的關(guān)系

  • 數(shù)據(jù)庫服務(wù)器、數(shù)據(jù)庫和表的關(guān)系如圖


    image.png

    * 表的一行稱之為一條記錄,表中一條記錄對(duì)應(yīng)一個(gè)java對(duì)象的數(shù)據(jù)

image.png

4、DDL數(shù)據(jù)定義語言

數(shù)據(jù)庫的操作

  • 創(chuàng)建數(shù)據(jù)庫 create database mydb ;
  • 查看創(chuàng)建數(shù)據(jù)庫的語句 show create database mydb ;
  • 改變當(dāng)前的數(shù)據(jù)庫 use mydb ;
  • 刪除數(shù)據(jù)庫 drop database mydb ;
  • 查看所有的數(shù)據(jù)庫 show databases ;
  • 修改數(shù)據(jù)庫mydb1的字符集為utf8 alter database mydb1 character set utf8;
  • 創(chuàng)建數(shù)據(jù)庫mydb1,字符集用gbk create database mydb1 character set gbk ;
  • 查看數(shù)據(jù)庫中所有的校對(duì)規(guī)則 show collation ;
  • 查看中文的校驗(yàn)規(guī)則 show collation like '%gb%' ;
  • 創(chuàng)建數(shù)據(jù)庫mydb2,字符集用gbk,校驗(yàn)規(guī)則用gbk_bin create database mydb2 character set gbk collate gbk_bin ;

針對(duì)表的操作

  • 創(chuàng)建表t create table t( id int , name varchar(30) ) ;
  • 查看創(chuàng)建表的源碼 show create table t ;
  • 創(chuàng)建表t1,使用字符集gbk create table t1( id int , name varchar(30) )character set gbk ;
  • 創(chuàng)建表t4 create table t4 ( id int , name varchar(30), optime timestamp ) ;

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

  • 設(shè)置客戶端的字符集為gbk set character_set_client=gbk;
  • 設(shè)置結(jié)果集的字符集為gbk set character_set_results=gbk ; insert into t4(id,name) values(1,'張無忌') ; insert t4(id,name) values(2,'喬峰') ; into 可以省略
  • 省略字段,意味著所有的字段都必須給值(自增例外) insert t4 values(3,'楊過','2014-4-3') ;

更新

  • 將表t4的第三條記錄姓名字段改為楊康update t4 set name='楊康' where id = 3 ;
  • 將所有記錄的名字都改為東方不敗 update t4 set name = '東方不敗' ;
  • 修改多個(gè)字段 update t4 set id=6,name='蕭峰' where id = 2 ;

刪除

  • delete from t4 where id = 4 ;
  • 刪除所有的記錄 delete from t4 ;
  • 刪除所有的記錄 truncate table t4 ;
  • 給表t4增加一個(gè)字段address alter table t4 add address varchar(100) ;
  • 刪除字段address alter table t4 drop column address ;
  • 查看表的結(jié)構(gòu) desc t4 ;

創(chuàng)建一個(gè)學(xué)生表

  • create table stu ( id int primary key, // 主鍵約束 name varchar(30) unique, // 唯一約束 sex char(2) not null, //非空約束 age int check (age > 0 and age < 100),// 檢查約束 address varchar(50) default '北京' //默認(rèn)約束 ) ;

  • insert into stu values(1,'張無忌','男',20,'北京') ; insert into stu values(2,'小龍女','女',18,'古墓') ; insert into stu values(3,'黃蓉','女',15,'桃花島') ; insert into stu values(4,'韋小寶','男',24,'揚(yáng)州') ; insert into stu values(5,'喬峰','男',34,'雁門關(guān)') ; insert into stu values(6,'張果老','男',30,'雁門關(guān)') ; insert into stu values(7,'老張','男',38,'黒木崖') ; insert into stu values(8,'張','男',34,'桃花島') ; insert into stu values(9,'韋小寶','女',24,'新東方') ; insert into stu(id,name,sex,age) values(10,'令狐沖','男',27) ;

  • 查看所有數(shù)據(jù) select * from stu ;

  • 查看小龍女的信息 select * from stu where id = 2 ; select * from stu where name='小龍女' ;

  • 查看年齡在20~30之間的人 select * from stu where age >=20 and age <=30 ; select * from stu where age between 20 and 30 ; # 包括20和30 #查看所有的的姓名 select name from stu ;

  • 查看所有的的姓名,年齡,性別 select name,age,sex from stu ;

模糊查詢

  • select * from 表名 where 字段名 like 字段表達(dá)式 % 表示任意字符數(shù) _ 表示任意的一個(gè)字符 [] 表示在某個(gè)區(qū)間
  • 查詢所有以張開頭的人 select * from stu where name like '張%' ;
  • 查詢姓名中含有張這個(gè)字的人 select * from stu where name like '%張%' ;
  • 查詢姓名中含有張這個(gè)字的人并且姓名的長度是3個(gè)字的人select * from stu where name like '張__' or name like '_張_' or name like '__張' ;
  • 查詢表中有幾種性別 select distinct sex from stu ;
  • 查找姓名和性別整體都不同的記錄 select distinct name,sex from stu ;

創(chuàng)建新表分?jǐn)?shù)表

create table score ( id int primary key, sid int , china int, english int , history int, constraint sid_FK foreign key(sid) references stu(id) ) ;
 insert into score values(1,1,68,54,81) ;
 insert into score values(2,3,89,98,90) ; 
insert into score values(3,4,25,60,38) ; 
insert into score values(4,6,70,75,59) ;
 insert into score values(5,8,60,65,80) ;
  • 字段可以有表達(dá)式 select id,china+10,english,history from score ;
  • 給字段起別名select id as 編號(hào),china as 語文,english as 英語,history as 歷史 from score ; select id 編號(hào),china 語文,english 英語,history 歷史 from score ;
  • 查看所有人考試的總分是多少 select id,china + english + history 總分 from score ;
  • 查看總分大于200的人 select * from score where china + english + history > 200 ;
  • 查詢家在桃花島或者黒木崖的人
    select * from stu where address = '桃花島' or address = '黒木崖' ;
    select * from stu where address in('桃花島','黒木崖') ;
  • 查詢沒有參加考試的人 select id ,name from stu where id not in(select sid from score) ;
  • 查詢沒有地址的人 select * from stu where address = null ; #錯(cuò)誤的 select * from stu where address is null ;
  • 查詢有地址的人 select * from stu where address is not null ;

排序(order by )

  • 對(duì)考試的人的語文升序asc排列 select * from score order by china asc;

  • 對(duì)考試的人的歷史降序desc排列 select * from score order by history desc;

  • 根據(jù)多個(gè)字段進(jìn)行排序(語文升序,對(duì)語文成績一樣的人再進(jìn)行歷史降序類排)select * from score order by china asc,history desc;

  • 根據(jù)考試總分降序進(jìn)行排序 select *,china + english + history 總分 from score order by china + english + history desc ;主鍵:唯一的去區(qū)分每一條記錄的一列或者多列的值. 特點(diǎn):唯一,非空
    Order by指定排序的列,排序的列即可是表中的列名,也可以是select 語句后指定的列名。
    Asc升序、Desc降序
    ORDER BY 子句應(yīng)位于SELECT語句的結(jié)尾。

創(chuàng)建引用約束

alter table score add constraint stu_score_FK foreign key(sid) references stu(id) ;

  • 刪除約束 alter table score drop foreign key stu_score_FK ;
  • 引用約束 注意:
      1. 添加記錄時(shí)必須先添加主表中的記錄,再添加字表中的記錄
      1. 不能更改主表中具有外鍵約束的記錄的主鍵
      1. 刪除記錄的時(shí)候不允許刪除具有外鍵關(guān)系的主表中的記錄(刪除的順序應(yīng)當(dāng)是先刪除字表中的記錄,然后刪除主表中的記錄)

自動(dòng)增長 create table t5 ( id int primary key auto_increment, name varchar(20) ) ;

  • 多表查詢

  • 交叉查詢

  • 查詢每個(gè)人的考試成績 select * from stu s cross join score c on s.id = c.sid ;

    image.png

  • 查詢參加考試的人的成績 select name,china,english,history,china+english+history 總分 from stu s inner join score c on s.id = c.sid ;

  • 查詢所有人的成績 select name,china,english,history,china+english+history 總分 from stu s left out join score c on s.id = c.sid ;

  • 查詢沒有參加考試的人select * from stu where id not in(select sid from score) ;

  • 查詢參加考試的人的成績select name,china,english,history,china+english+history 總分 from stu s,score c where s.id = c.sid ;

  • 聚合函數(shù) sum max,min avg ,count

    image.png

  • 分組函數(shù)select count(*) 數(shù)量,sex,name from stu group by sex,name ;

  • 根據(jù)多個(gè)字段進(jìn)行分組

  • 分組條件select count(*),sex from stu where age >=16 group by sex having count(*) >1 ;

5、在where子句中經(jīng)常使用的運(yùn)算符

運(yùn)算符
Like語句中,% 代表零個(gè)或多個(gè)任意字符,_ 代表一個(gè)字符,例first_name like ‘_a%’

image.png

6、數(shù)據(jù)完整性

  • 數(shù)據(jù)完整性是為了保證插入到數(shù)據(jù)中的數(shù)據(jù)是正確的,它防止了用戶可能的輸入錯(cuò)誤。
  • 數(shù)據(jù)完整性主要分為以下三類:
    • 實(shí)體完整性:
      規(guī)定表的一行(即每一條記錄)在表中是唯一的實(shí)體。實(shí)體完整性通過表的主鍵來實(shí)現(xiàn)。主鍵:唯一的去區(qū)分每一條記錄的一列或者多列的值. 特點(diǎn):唯一,非空
    • 域完整性:
      指數(shù)據(jù)庫表的列(即字段)必須符合某種特定的數(shù)據(jù)類型或約束。比如NOT NULL。主鍵primary key 唯一約束unique 檢查約束 MySQL不支持哦int check (age > 0 and age < 100) 非空約束not null
      • create table stu ( id int primary key, // 主鍵約束 name varchar(30) unique, // 唯一約束 sex char(2) not null, //非空約束 age int check (age > 0 and age < 100),// 檢查約束 address varchar(50) default '北京' //默認(rèn)約束 ) ;
    • 參照完整性:
      保證一個(gè)表的外鍵和另一個(gè)表的主鍵對(duì)應(yīng)。

7、查詢

  • 連接查詢

    • 交叉連接(cross join):不帶on子句,返回連接表中所有數(shù)據(jù)行的笛卡兒積。
    • 內(nèi)連接(inner join):返回連接表中符合連接條件及查詢條件的數(shù)據(jù)行。
    • 外連接:分為左外連接(left out join)、右外連接(right outer join)。與內(nèi)連接不同的是,外連接不僅返回連接表中符合連接條件及查詢條件的數(shù)據(jù)行,也返回左表(左外連接時(shí))或右表(右外連接時(shí))中僅符合查詢條件但不符合連接條件的數(shù)據(jù)行。
  • 子查詢

    • 子查詢也叫嵌套查詢,是指在select子句或者where子句中又嵌入select查詢語句 查詢“陳冠?!钡乃杏唵涡畔?br> SELECT * FROM orders WHERE customer_id=(SELECT id FROM customer WHERE name LIKE '%陳冠希%');
  • 聯(lián)合查詢

    • 聯(lián)合查詢能夠合并兩條查詢語句的查詢結(jié)果,去掉其中的重復(fù)數(shù)據(jù)行,然后返回沒有重復(fù)數(shù)據(jù)行的查詢結(jié)果。聯(lián)合查詢使用union關(guān)鍵字
      SELECT * FROM orders WHERE price>200 UNION SELECT * FROM orders WHERE customer_id=1;
  • 報(bào)表查詢

    • 報(bào)表查詢對(duì)數(shù)據(jù)行進(jìn)行分組統(tǒng)計(jì),其語法格式為:
      [select …] from … [where…] [ group by … [having… ]] [ order by … ]
      其中g(shù)roup by 子句指定按照哪些字段分組,having子句設(shè)定分組查詢條件。在報(bào)表查詢中可以使用SQL函數(shù)。

8、 MySQL常用數(shù)據(jù)類型

  • 數(shù)值類型

    • BIT(M) 位類型。M指定位數(shù),默認(rèn)值1,范圍1-64

    • TINYINT [UNSIGNED] [ZEROFILL] 帶符號(hào)的范圍是-128到127。無符號(hào)0到255。

    • BOOL,BOOLEAN 使用0或1表示真或假

    • SMALLINT [UNSIGNED] [ZEROFILL] 2的16次方

    • INT [UNSIGNED] [ZEROFILL] 2的32次方

    • BIGINT [UNSIGNED] [ZEROFILL] 2的64次方

    • FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] M指定顯示長度,d指定小數(shù)位數(shù)

    • DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 表示比float精度更大的小數(shù)

  • 文本、二進(jìn)制類型

    • CHAR(size) char(20) 固定長度字符串
    • VARCHAR(size) varchar(20)可變長度字符串(但是不可以超過255, 但是在Oracle中是4000)
    • BLOB LONGBLOB 二進(jìn)制數(shù)據(jù)( 用在音頻或者是視屏)
    • TEXT(clob) LONGTEXT(longclob)大文本(小說)
  • 時(shí)間日期

    • DATE/DATETIME/TimeStamp
    • 日期類型(YYYY-MM-DD) (YYYY-MM-DD HH:MM:SS),TimeStamp表 示時(shí)間戳,它可用于自動(dòng)記錄insert、update操作的時(shí)間

9、 相關(guān)函數(shù)

  • 時(shí)間日期相關(guān)函數(shù)

    image.png

    select addtime(‘02:30:30’,‘01:01:01’); 注意:字符串、時(shí)間日期的引號(hào)問題
    select date_add(entry_date,INTERVAL 2 year) from student;//增加兩年
    select addtime(time,‘1 1-1 10:09:09’) from student;//時(shí)間戳上增加,注意年后沒有

  • 字符串相關(guān)函數(shù)


    image.png
  • 數(shù)學(xué)相關(guān)函數(shù)


    image.png
  • 數(shù)據(jù)庫備份:
    mysqldump -u root -psorry test>D:\test.sql
  • 數(shù)據(jù)庫恢復(fù):
    • 創(chuàng)建數(shù)據(jù)庫并選擇該數(shù)據(jù)庫
    • SOURCE 數(shù)據(jù)庫文件
    • 或者:
      mysql -u root -psorry test<test.sql
最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 1 安裝MySQL 進(jìn)入MySQL官網(wǎng),登錄 如何在Macbook上安裝MySQL_百度經(jīng)驗(yàn) 我之后改了個(gè),怕忘記...
    喬大葉_803e閱讀 769評(píng)論 0 0
  • 查詢數(shù)據(jù) 基本查詢 SELECT * FROM <表名> :查詢一個(gè)表的所有數(shù)據(jù) SELECT * FROM <...
    凌星月月閱讀 533評(píng)論 0 0
  • 表 存儲(chǔ)在表中的數(shù)據(jù)是同一種類型的數(shù)據(jù)或清單。 數(shù)據(jù)庫中的表有為一個(gè)名字來標(biāo)識(shí)自己。 表具有一些特性,這些特性定義...
    蛐蛐囍閱讀 1,470評(píng)論 0 7
  • 1: 常用的sql分類: DDL:數(shù)據(jù)定義語言DCL:數(shù)據(jù)控制語言DML: 數(shù)據(jù)的查詢語句DQL:數(shù)據(jù)的查詢語句 ...
    醉舞經(jīng)閣半卷書A閱讀 387評(píng)論 0 0
  • SQL學(xué)習(xí) 法則1:col table表/columns列/rows行 問題:movies表有100萬數(shù)據(jù)? 法則...
    jessica涯閱讀 585評(píng)論 0 1

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