mySQL
Navicat for mySQL
關(guān)系型數(shù)據(jù)庫:用表傳數(shù)據(jù)
如何建表:查詢→新建查詢
注釋: -- 這是一個注釋? ?或者/*這是一條注釋*/???
建表時需要指定:1.表名;2.表中字段(列)名;3.字段的數(shù)據(jù)類型
數(shù)值型
-- 【int(6)--整數(shù) -999999到999999 n代表位數(shù) 最多11位,decimal(7,2) 最大長度七位,小數(shù)點后保留兩位 整數(shù)位最多五位】
字符型
-- varchar(200) 最多保存100個漢字或200個英文或數(shù)字
、日期類型
-- DATETIME (年月日時分秒)
超級字符串 ·***·(1左面那個鍵)(標準是要寫的 實際上寫不寫都行)
CREATE TABLE students(
`stuid` INT(9),
`stuname` VARCHAR(100),
`height` DECIMAL(3,2)
)
-- 向表中插入數(shù)據(jù)
INSERT INTO students(stuid,stuname) VALUES(9527,'華安');
INSERT INTO students(stuid,stuname,height,birthday) VALUES(9528,'華安他哥',1.73,'2018-11-11')
引號必須加
在不添加括號的時候是按順序添加 不能缺項
-- 簡單查詢
-- 1.查什么
-- 2.從哪查
SELECT cname,id,teacher
from course
-- *代表全部列
SELECT *
from course
-- 條件查詢
-- 查詢身高1.5以上的所有人數(shù)據(jù)
-- 數(shù)值類型的條件
-- > < <= >=? 不等于!=
SELECT *
FROM students
WHERE height > 1.5
-- 查詢姓名是華安的學員信息
SELECT *
FROM students
WHERE stuname = '華安'
引號!!
單引號?。。。?!
-- 查找2010年之前出生的學員信息
SELECT *
FROM students
WHERE birthday < '2010-1-1'
還是引號?。?!
-- 兩個條件時用and連接
SELECT *
from students
WHERE stuname = '華安' AND height > 1.6
再重復(fù) 還是引號 必須寫
-- 兩個非并列條件 用or連接
SELECT *
from students
WHERE stuname = '華安' OR height > 1.6
-- 查詢每個人姓名 工資 年薪 查詢后面加空格打字 是給查詢結(jié)果列起名字
select stuname 姓名,salary 工資,salary*12 年薪
from students
-- 特殊比較運算符
-- 查詢所有人工資大于等于1500,小于等于3500的所有人的信息
SELECT *
FROM students
WHERE salary >= 1500 and salary <= 3500
SELECT *
FROM students
WHERE salary BETWEEN 1500 and 3500
-- 查詢在2000年至2017年之間出生的所有人信息
SELECT *
from students
WHERE birthday BETWEEN '2000-1-1' and '2017-12-31'
-- in(參數(shù)......)等于任意一個參數(shù)
-- 查詢身高為2.23和3.23的所有人信息
select *
from students
where height = 2.23 or height = 3.23
select *
from students
where height in (2.23,3.23)
-- 查詢姓名是華安,華安他姐,華安他二姐的人的信息
SELECT *
from students
WHERE stuname in ('華安','華安他姐','華安他二姐')
-- LIKE 模糊查詢
-- 通配符:%
-- 查詢姓名是以’華‘開頭人的信息
select *
from students
where stuname like '華%'
-- 查詢姓名是以’華‘結(jié)尾人的信息
select *
from students
where stuname like '%華'
-- 查詢姓名中包含'華’人的信息
select *
from students
where stuname like '%華%'
-- 查詢姓名是以王或小開頭的人的信息
select *
from students
where stuname like '王%' or stuname like '小%'
-- 查詢2013年出生的人的信息
select *
from students
where birthday between '2013-1-1' and '2013-12-31'
-- like不要用在查詢時間上 有風險
原因是因為數(shù)據(jù)庫不一樣時間的表達方式不一樣
-- 建兩張相關(guān)表
CREATE TABLE class(
`id` int(9),
`cname` VARCHAR(100),
`croom` VARCHAR(100)
)
CREATE TABLE stu(
`id` int(9),
`name` varchar(100),
`birthday` date,
`cno` int(9)
)
-- 查詢學生編號,姓名,所在班級編號
SELECT stu.id,name,cno,croom,class.id
FROM stu,class
WHERE cno = class.id