sql 的分類: DDL: 數(shù)據(jù)定義語言 create drop alter DML: 數(shù)據(jù)操作語言 記錄 DQL:數(shù)據(jù)查詢語言 : 非官方的 查詢語句 DCL:數(shù)據(jù)控制語言 : 事物,管理net start MySQLmysql -u用戶名 -p密碼DDL: 數(shù)據(jù)庫定義語言 創(chuàng)建數(shù)據(jù)庫: create database 數(shù)據(jù)庫名稱; 刪除數(shù)據(jù)庫:drop database 數(shù)據(jù)庫名稱; 常用命令: 查看所有數(shù)據(jù)庫 : show databases; 切換或者使用數(shù)據(jù)庫: user 數(shù)據(jù)庫名稱; 操作表: 創(chuàng)建表: create table 表名(字段描述,字段名稱); 字段描述: 字段名稱,字段類型 [約束 ] create table user ( id Int PRimary key auto_increment , username varchar(20) ) 查看所有表: show tables; 查看表結(jié)構(gòu): desc 表名; 看看建表語句: show create table 表名; 修改表名: alter table 表名(舊表名) rename to 表名(新表名); 添加字段: alter table 表名 add [column] 字段描述; 例如: alter table user add passWord varchaer(20); 修改字段名稱: alter table 表名 change 字段名稱(舊字段) 字段名稱(新字段名稱) 字段描述 修改字段描述: alter table 表名 modify 字段名稱 字段類型 [約束] ; 刪除字段: alter table 表名 drop 字段名; 刪除表: drop table 表名;DML: 數(shù)據(jù)操作語言 關(guān)鍵字 insert update delete 注意: 若字段類型為數(shù)字,可以省略引號(hào),盡量別省略 插入: insert into 表名 values(字段值1,字段值2,字段值n);--默認(rèn)插入所有字段,必須保證value后的類型和順序要與鍵表結(jié)構(gòu)順序一致 insert into 表名 (字段名,字段名) values(字段值,字段值);--插入指定的值, 必須保證和創(chuàng)表的類型與順序一致 修改: update 表名 set 字段名=字段值, 字段名1=字段值 [條件查詢] ; 刪除: delete from 表名 【where】;DQL: 數(shù)據(jù)查詢語言 select * form 表名 where group by having order by ase| desc select * from 表名; select 字段名1, 字段名2 from 表名; select distincet 字段名 from 表名 ; // distincet 去重的意思 可以在查詢的結(jié)果上進(jìn)行運(yùn)算,但是不影響數(shù)據(jù)庫里面的值 select 字段名 + 10 from 表名; 起別名(給列或者字段別名) select 字段名 + 10 新別名 from 表名; select 字段名 + 10 ’新別名‘ from 表名; select 字段名 + 10 ·新別名· from 表名; 條件查詢 select *from 表名 where 字段名=value; select *form 表名 where 字段名 > 20; select * from 表名 where 字段名 like 匹配規(guī)則 匹配個(gè)數(shù), 匹配內(nèi)容 //模糊查詢 select * from 表名 where 字段名 like ”__“ 占2個(gè)位置 select * from 表名 where 字段名 like %龍 select * from 表名 where 字段名 like 龍% select * from 表名 where 字段名 like %龍% select *form 表名 where 字段名 = 20 or 字段名 = 30 or 字段名=40; select *form 表名 where 字段名 in(20,30,40); select *form 表名 where 字段名 between 20 and 40; select *from 表名 order by 字段名 (asc升序, desc 降序) select * from 表名 where 字段名 like ”“ order by 聚合函數(shù) (忽略null值) sum() avg() max() min() count() 獲得總和 select sum(字段名) from 表; 獲得平均數(shù) select avg(字段名) form 表名; round 函數(shù),四舍五入 select round(avg(字段名),2)from 表名; 計(jì)算 select count(*) form 表名 獲得一共多少條記錄 分組 在where 后面 order by前面 where 與 having區(qū)別 where 是分組前的數(shù)據(jù)進(jìn)行過濾,后面不能使用聚合函數(shù) having是分組后的數(shù)據(jù)進(jìn)行過濾, 可以使用聚合函數(shù) select 字段名, count(*) from 表名 group by 字段名 ; select 字段名, sum(字段名) from 表名 group by 字段名; select 字段名, sum(字段名) from 表名 group by 字段名 having > 200; 數(shù)據(jù)類型的問題 mysql java tinyInt byte smallInt short Int int bigInt long varchar varChar/char (varChar 可變長度 char不可變長度) int boolean float float double(5,2) double 注”:double(5,2) 改小數(shù)長度為5,小數(shù)占2位 date java.sql.Date time java.sql.Time timesTamp java.sql.timesTamp dateTime 日期加+時(shí)間 無 text (最大4g廠文件) java.sql.clob()存長文本 blob java.sql.blob() 存文二進(jìn)制文件(電影)約束: 作用:為了保證數(shù)據(jù)的有效性與完整性 mysql常用約束: primary key 主鍵約束 unique 唯一約束 not null 非空 foreign key 外鍵約束 主鍵約束: 修飾過過的字段唯一非空,一張表只能有一個(gè)主鍵,這個(gè)主鍵可以保函多個(gè)字段 方式1: 鍵表的同時(shí)添加約束 字段名稱 字段類型 primary key 方式2: 鍵表的同時(shí)在約束區(qū)域添加約束 primary key(字段1,字段2) creat table user ( id int, username varchar(20), primary key(id) ); 方式3: 添加完表之后,通過修改表結(jié)構(gòu),修改主鍵 alter table 表名 add primary key(字段名1,字段名2) (聯(lián)合主鍵) 唯一約束: 被修飾過的字段唯一, 對(duì)null值不起作用 方式1: 鍵表的同時(shí)添加約束 字段名稱 字段類型 unique 方式2: 鍵表的同時(shí)在約束區(qū)域添加約束 unique(字段1,字段2) creat table user ( id int, username varchar(20), unique(id) ); 方式3: 添加完表之后,通過修改表結(jié)構(gòu),修改主鍵 alter table 表名 add unique(字段名1,字段名2) (聯(lián)合主鍵) 非空約束: 被修飾的字段非空 creat table user ( id int not null, username varchar(20), ); truncate: 清空表 格式: truncate 表名; //清空表,重新創(chuàng)建一張新表 delete 屬于逐條刪除 auto_increment : 要求, 被修飾的字段要滿足自增,被修飾的字段必須是key,一般是primary key 多表操作 sql:舉例操作:網(wǎng)上商城網(wǎng)上商城的實(shí)體: 1 用戶 2 訂單 3 商品 4分類 常見關(guān)系: 一對(duì)多關(guān)系: 用戶和訂單 分類和商品 多對(duì)多關(guān)系: 訂單和商品 一對(duì)一關(guān)系: 丈夫和妻子 ER圖,可以描述實(shí)體與實(shí)體的關(guān)系 一對(duì)多關(guān)系: 一稱之為主表 多稱之為 從表 會(huì)在多表一份添加一個(gè)字段,字段名稱稱之為主表_id 與主表的主鍵類型保持一致,我們稱之為外鍵 用戶和訂單列表: 用戶表: create table user( id int primary key auto_increment, userName varchar(20) ) 訂單列表 create table orders( id int auto_increment, totlePrice double, user_id int, ) 添加外鍵約束:在多表的一方添加約束 alter table 多表名稱 add foreign key (外鍵名稱) reference 一表名稱(主鍵) alter table orders add foreight key(user_id) reference user(id) 添加了外鍵之后又如下特點(diǎn),主表中不能刪除從表中已經(jīng)引用的數(shù)據(jù) ,從表中不能添加主表 中不存在的數(shù)據(jù) 多對(duì)多關(guān)系:商品和訂單 create table orders( id int auto_increment, totlePrice double, user_id int, ) create table product( id int auto_increment, pname varchar(20) Price double, ) create table orderItem( oid int , pid int , ) 在開發(fā)中,我們一般引入中間表來表示他們的關(guān)系,在中間表中,存放2表的主鍵,然后一般還會(huì)將這個(gè)兩個(gè)主鍵設(shè)置成中間表的聯(lián)合主鍵,將多對(duì)多變?yōu)?個(gè)一對(duì)多,為了保證數(shù)據(jù)的一致性,在中間表上添加2個(gè)外鍵即可 alter table orderItem add foreign key(oid) reference orders(id)alter table orderItem add foreign key(pid) refence product(id)多表查詢笛卡爾迪: 多張表無條件的聯(lián)合查詢,沒有任何意義 select A表.* ,B表 from A表,B表;內(nèi)連接 : 格式1: 顯示的內(nèi)連接 select A表.* , B表.* from A[inner] join B on AB的鏈接條件 格式2: 隱示的內(nèi)連接 select A表.*, B表.* from A ,B where ab的鏈接條件 外鏈接 1 左外鏈接 select A表.*, B表.* from A表 left[outer] join B表 on 鏈接條件; 先展示join左邊的表的所有數(shù)據(jù),根據(jù)條件關(guān)聯(lián)查詢join 右邊的表(b),符合條件的展示出來 不符合的null展示 2 右外鏈接 select A表.*, B表.* from B表 right [outer] join A表 on 鏈接條件 先展示join右邊的數(shù)據(jù),根據(jù)條件關(guān)聯(lián)查詢join左邊的表的數(shù)據(jù),符合條件的則展示,不符合 條件則以null顯示 子查詢例如:查詢用戶的訂單,沒有訂單的用戶不顯示 隱示的內(nèi)連接: select user.*, order.* from user, orders where user.id=orders.user_id;顯示的內(nèi)連接:select user.*, order.* from user join orders on user.id=orders.use.id;查詢所有用戶的訂單詳情左外連接: select user.*, order.* from user left join orders on user.id=orders.use.id;右外鏈接: select user.*, orders.* form orders join user on user.id=orders.use.id;子查詢一個(gè)查詢依賴與另一個(gè)查詢 例如:查詢用戶為張三的詳情 select *from orders where user_id = (select id from user where username = '張三'); 查詢出訂單價(jià)格大于300的用戶信息 in 對(duì)應(yīng)多個(gè)信息 select * from user where user_id in (select user_id from orders where totleprice > 300); 查詢出訂單價(jià)格大于300的訂單信息及用戶信息 as 給表起別名 //內(nèi)鏈接 select user.*,order.* from user,orders where user.id = order.user_id and order.price > 300; //子查詢 select user.* , tamp.* from user, (select *from order where pice > 300) as tamp where user.id=tamp.user_id