SQL語言

一、使用SQL對數(shù)據(jù)庫操作

1.1 連接數(shù)據(jù)庫

  • 打開cmd窗口,使用命令,連接mysql數(shù)據(jù)庫
  • 命令:mysql -u root -p 密碼

1.2 創(chuàng)建數(shù)據(jù)庫

  • 語句:create database 數(shù)據(jù)庫的名稱;
  • 示例:create database testdb1;

1.3 查看所有的數(shù)據(jù)庫

  • 語句:show databases;

1.4 刪除數(shù)據(jù)庫

  • 語句:drop database 要?jiǎng)h除的數(shù)據(jù)庫的名稱;
  • 示例:drop database testdb1;

1.5 切換數(shù)據(jù)庫

  • 如果想要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫表,這個(gè)表要在一個(gè)數(shù)據(jù)庫里面,所以需要切換到數(shù)據(jù)庫
  • 語句:use 要切換的數(shù)據(jù)庫的名稱;
  • 示例:use testdb2;

1.6 查看當(dāng)前使用的數(shù)據(jù)庫

  • 語句:select database()

二、使用SQL對數(shù)據(jù)庫表操作

2.1 創(chuàng)建數(shù)據(jù)庫表

語句:
create table 表名稱 (
字段 類型(長度),
字段 類型(長度)
);

//示例:創(chuàng)建表 user,字段  id  username  password  sex
create table user (
 id int,
 username varchar(40),
 password varchar(40),
 sex varchar(30)
)

創(chuàng)建帶約束的表

create table person (
id int primary key ,
username varchar(40) not null,
sex varchar(20)
)

2.2 查看表結(jié)構(gòu)

desc 表名稱;

2.3 刪除表

drop table 要?jiǎng)h除的表名稱;

2.4 查看當(dāng)前的數(shù)據(jù)庫的表

show tables;(查詢數(shù)據(jù)庫中一共有多少個(gè)表)

三、操作表中的記錄

3.1 添加記錄 insert

語句:insert into 要添加的表名稱 values(要添加的值);
示例:insert into user values(1,'aaa','123456','nan');
注意:當(dāng)添加的字段的數(shù)據(jù)類型是int類型,直接寫值,如果添加的字段的類型是varchar類型和日期類型,使用單引號把值包起來

3.2 修改記錄 update

語句:update 表名稱 set 要修改的字段的名稱1=修改的值2,要修改的字段的名稱2=修改的值2 where 條件
示例:update user set username='aaa',password='999' where id=1;

3.3 刪除記錄 delete

語句:delete from 表名稱 where 條件
示例:delete from user where id=1;
注意:不添加where條件,把表里面的所有的記錄都刪除

3.4 查詢記錄 select

語句:select 要查詢的字段的名稱 (*) from 表名稱 where 條件
示例:select * from user;(查詢表里的數(shù)據(jù))
select username,chinese from user;(可以查詢多個(gè)字段)
select * from user where id=2;
注意:as關(guān)鍵字可以給字段起一個(gè)別名。
select username as u1,chinese as c1 from user;

3.5 去除重復(fù)記錄 distinct

語句 select distinct * from 表名;

3.6 運(yùn)算符 < > >= <=

select * from student where english > 60;
in:在范圍內(nèi)
練習(xí):查詢表里面英語成績是80-100的學(xué)生信息
select * from student where english in (80,90);(寫誰查誰)
and:在where里面如果有多個(gè)條件,表示多個(gè)條件同時(shí)滿足
練習(xí):查詢表里面英語成績是70,并且數(shù)學(xué)成績是80的學(xué)生信息
select * from student where english=70 and math=80;
得到區(qū)間范圍的值
練習(xí):查詢表里面數(shù)學(xué)成績在80-100之間的值(兩種寫法)
寫法一,select * from student where math >=80 and math <=100;
寫法二,select * from student where math between 80 and 100;
like:模糊查詢
練習(xí):查詢表里面name包含a的學(xué)生信息
select * from student where name like '%a%';
對查詢記錄排序 order by存儲(chǔ)
order by寫在select語句的最后
第一,升序 order by 要排序字段 asc(asc可以省略,默認(rèn)的情況下就是升序)

  • 練習(xí):對student表里面查詢的數(shù)據(jù),根據(jù)語文成績進(jìn)行升序排列
    select * from student order by english asc;
    第二,降序 order by 要排序字段 desc
    練習(xí):對student表里面的英語成績進(jìn)行降序排列
    select * from student order by english desc;

3.7 查看當(dāng)前運(yùn)行的數(shù)據(jù)庫

select database();

3.8 聚集函數(shù)

使用提供的一些函數(shù),直接實(shí)現(xiàn)某些功能。
count()函數(shù)

  • 根據(jù)查詢的結(jié)果,統(tǒng)計(jì)記錄數(shù)
    *代表所有字段
  • 寫法 select count(*) from ...where....
  • 查詢student表里面有多少條記錄
    select count(*) from student;
  • 查詢student表里面英語成績大于90的學(xué)生有多少
    select count(*) from student where english>90;
    sum()函數(shù)
  • 求和的函數(shù)
  • 寫法 select sum(要進(jìn)行求和字段) from ...where....
    **得到student表里面的英語的總成績
    select sum(english) from student;
  • *得到student表里面英語總成績,數(shù)學(xué)的總成績
    select sum(english),sum(math) from student;
    *得到student表里面語文成績的平均分(總的成績/總的人數(shù))
    select sum(english)/count(
    ) from student;
    avg()函數(shù)
  • 計(jì)算的平均數(shù)的函數(shù)
  • 寫法 select avg(要計(jì)算平均數(shù)的字段名稱) from ...
  • 練習(xí):得到student表里面數(shù)學(xué)成績的平均分
    select avg(math) from student;
    max()函數(shù)
  • 計(jì)算最大值
  • 寫法 select max(字段) from...
  • 練習(xí):得到英語成績的最大值
    select max(english) from student;
    min()函數(shù)
  • 計(jì)算最小值
  • 寫法 select min(字段) from...
  • 練習(xí):得到user表里面英語成績的最小值
    select min(english) from student;
    分組操作
    分組使用 group by 根據(jù)分組的字段
    語法:group by + 要分組的字段
    在分組的基礎(chǔ)之上再進(jìn)行條件的判斷 hav4ing,后面可以寫聚集函數(shù)。

四、mysql關(guān)鍵字limit

(1)limit關(guān)鍵字查詢表中的某幾條記錄
(2)limit關(guān)鍵字不是標(biāo)準(zhǔn)sql的關(guān)鍵字,只能在mysql數(shù)據(jù)庫里面使用,實(shí)現(xiàn)分頁的功能。

  • 在oracle里面特有關(guān)鍵字 rownum
  • 在sqlserver里面特有關(guān)鍵字 top
    (3)使用limit查詢前幾條記錄
  • 寫法: limit 前幾條記錄 limit 3
    查詢orders表里面前三條記錄
    select * from orders limit 3;
    select * from orders limit 0,3;

(4)使用limit查詢第幾條到第幾條記錄

  • 寫法: limit 第一個(gè)參數(shù),第二個(gè)參數(shù)(不包括第一個(gè)參數(shù))
    ** 第一個(gè)參數(shù)開始的記錄數(shù)的位置,從0開始的
    ** 第二個(gè)參數(shù)從開始的位置向后獲取幾條記錄
  • 練習(xí):查詢orders表里面第二條到第四條記錄
    select * from orders limit 1,3;

五、mysql的數(shù)據(jù)類型

5.1 字符串型

VARCHAR、CHAR
當(dāng)創(chuàng)建表時(shí)候,使用字符串類型,name varchar(40),指定數(shù)據(jù)的長度
varchar和char的區(qū)別:
varchar的長度是可變的,比如 name varchar(5),存值 a ,直接把a(bǔ)存進(jìn)去
char的長度是固定的,比如name char(5),存值 b,把b存進(jìn)去,后面加很多空格

5.2 大數(shù)據(jù)類型

BLOB(存音頻,視頻,圖片等)、TEXT(存字符類型,文本文件)

  • 使用這個(gè)類型可以存儲(chǔ)文件,一般開發(fā),不會(huì)直接把文件存到數(shù)據(jù)庫里面,存文件的路徑

5.3 數(shù)值型

mysql中 TINYINT SMALLINT INT BIGINT FLOAT DOUBLE
Java中 byte short int long float double

5.4 邏輯性

BIT 占1位,1字節(jié)占8位

  • 類似java里面的boolean

5.5 日期型

DATE:用于表示日期 1945-08-15
TIME:用于表示時(shí)間 19:10:40
下面的兩個(gè)類型可以表示日期和時(shí)間
DATETIME:手動(dòng)添加時(shí)間到數(shù)據(jù)表里面
TIMESTAMP:自動(dòng)把時(shí)間添加到表里面

六、MySQL的約束

6.1 非空約束 not null

  • 表示數(shù)據(jù)不能為空

6.2 唯一性約束 unique

  • 表中的記錄不能重復(fù)的

6.3 主鍵約束 primary key

  • 表示非空,唯一性
  • 后面自動(dòng)增長 auto_increment
    一旦字段設(shè)置為主鍵,那么該字段就是非空并且唯一
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 50個(gè)常用的sql語句Student(S#,Sname,Sage,Ssex) 學(xué)生表Course(C#,Cname...
    哈哈海閱讀 1,327評論 0 7
  • SQL語言基礎(chǔ) 本章,我們將會(huì)重點(diǎn)探討SQL語言基礎(chǔ),學(xué)習(xí)用SQL進(jìn)行數(shù)據(jù)庫的基本數(shù)據(jù)查詢操作。另外請注意本章的S...
    厲鉚兄閱讀 5,456評論 2 46
  • 50個(gè)常用的sql語句 Student(S#,Sname,Sage,Ssex) 學(xué)生表 Course(C#,Cna...
    最美的太陽WW閱讀 3,421評論 0 23
  • 什么是SQL數(shù)據(jù)庫: SQL是Structured Query Language(結(jié)構(gòu)化查詢語言)的縮寫。SQL是...
    西貝巴巴閱讀 1,990評論 0 10
  • 很多人忽視了HTML標(biāo)簽META的強(qiáng)大功效,一個(gè)好的META標(biāo)簽設(shè)計(jì)可以大大提高你的個(gè)人網(wǎng)站被搜索到的可能性,有興...
    燃燒吧_小宇宙閱讀 1,404評論 0 0

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