標籤:主鍵 模糊查詢 email 存在 多列 mil 根據 約束 屬性
前幾天學習了MySQL資料庫的一些基礎知識,瞭解了後台資料庫的一些基礎的資料操作:增刪改查,對於項目前背景資料傳遞有了些概念,總結了一些MySQL的基礎文法。
一、資料存放區形式發展和資料庫作用
根據時間先後:層次模型,網狀模型,傳統層次模型,和關聯式模式。
資料庫作用:1.儲存大量資料,訪問和檢索資料;2.保證資料的完整性;3.安全與共用;4.資料可以進行組合,產生新的資料
二、關聯式模式
關聯式模式是實體,和聯絡組成的關係網,所謂實體就是指現實世界中客觀存在並可相互區別的實物。實體具有屬性,屬性用於描述實體。以關聯式模式建立的資料庫稱為關係型資料庫(relational database)。表是關係型資料庫的核心單元,他是資料存放區的地方。
三、結構化查詢語言 (SQL)SQL
SQL是一種用於管理關聯性型資料庫,與資料庫中的資料進行通訊的電腦標準語言
分為:1.資料定義語言 (Data Definition Language)(DDL)
2.資料查詢語言(DQL)
3.資料操作語言(DML)
4.資料控制語言(DCL)
四、增加
建立資料庫:
create database 資料庫名
使用資料庫
use 資料庫名
刪除資料庫
DROP DATABASE 資料庫名
建立表格
create table student(表名){stuNum int primary key auto_increment,-- 資料類型,主鍵,自增stuName varchar(20), --資料類型sex char(2) default “男”, --資料類型,預設值card char(18) unique not null, --資料類型,唯一,不為空白s_c_id int references t_class(c_id), --資料類型,外鍵constraint foreign key(s_c_id) references t_class(c_id) --外鍵約束}
添加表格列
alter table 表名 add 列名 列名資料類型 列約束
刪除表格列
alter table 表名 drop column 列名
五、DML語句更改資料
添加資料
insert into 表名(欄位) values(欄位對應的值)
修改資料
update 表名 set 列=新值 where 條件
六、DQL語句查詢資料‘
’查詢
select * from 表名 where 條件
去重
select distinct 列名 from 表名 where 條件
模糊查詢
select * from 表名 where name like ‘李%’ //0個或多個字元
select * from 表名 where name in(‘’啊’,方,’哈’’)
Select * from 表名 where email is null //查詢是否為null
排序
Select 列名 from 表名 where 條件 order by 列名 ASC// asc 升序 desc 降序
分組
Select 欄欄位 from 表名 group by 欄欄位
七、子查詢
select子查詢:要求返回單行 和單列 相關
select * (select name1 from goods2 where name1=goods.name)as name1 from 表名
from 子查詢:返回多行多列 表結構 非相關
Select * from (select * from goods limit 10)as t where 條件
where 子查詢: 返回多行單列
Select * from student where ID in (select gid from ggg where name in (‘1’“2”))
八、表串連
1.Select * from 表1 join 表2 on 表1.ID=表2.ID 內串連:兩個表都存在的資料2.Select * from 表1,表2 where 表1.ID=表2.ID 內串連另一種寫法3.Select * from 表1 left outer join 表2 on 表1.ID=表2.ID 左外連結4.Select * from 表1 right outer join 表2 on 表1.ID=表2.ID 右外連結5.Select * from 表1 as 1 join 表1 as 2 on 條件 自串連
MySQL增刪改查基礎知識