建設(shè)銀行網(wǎng)站功能介紹百度的seo關(guān)鍵詞優(yōu)化怎么弄
DDL語法
DDL(Data Definition Language) 數(shù)據(jù)定義語言,該語言部分包括以下內(nèi)容。
對數(shù)據(jù)庫的常用操作
對表結(jié)構(gòu)的常用操作
修改表結(jié)構(gòu)
對數(shù)據(jù)庫的常用操作
1: 查看當(dāng)前所有的數(shù)據(jù)庫
show databases;
2:創(chuàng)建數(shù)據(jù)庫
create database if not exists 數(shù)據(jù)庫名稱;
create database 數(shù)據(jù)庫名稱;
3:選擇使用哪一個(gè)數(shù)據(jù)庫
use 數(shù)據(jù)庫名稱;
4:刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名稱;
drop database if exists 數(shù)據(jù)庫名稱;
5:修改數(shù)據(jù)庫編碼
alter database school character set utf8;
對表結(jié)構(gòu)的常用操作-創(chuàng)建表
1:創(chuàng)建表格式
create table [if not exists] 表名(
? 字段1 類型[(寬度)] [約束條件] [comment '字段說明'],
? 字段2 類型[(寬度)] [約束條件] [comment '字段說明'],
? 字段3 類型[(寬度)] [約束條件] [comment '字段說明'],
)[表的一些設(shè)置];
創(chuàng)建表是構(gòu)建一張空表,指定這個(gè)表的名字,這個(gè)表有幾列,每一列叫什么名字,以及每一列存儲(chǔ)的數(shù)據(jù)類型。
2:數(shù)據(jù)類型
數(shù)據(jù)類型是指在創(chuàng)建表的時(shí)候?yàn)楸碇凶侄沃付〝?shù)據(jù)類型的,只有數(shù)據(jù)符合類型要求才能存儲(chǔ)起來,使用數(shù)據(jù)類型的原則是:夠用就行,盡量使用取值范圍小的,而不用大的,這樣可以更多的節(jié)省存儲(chǔ)空間。
數(shù)值類型
日期和時(shí)間類型
字符串類型
3:數(shù)值類型
create table if not exists student(
? ?-- 無符號(hào) 沒有負(fù)數(shù)
?? sid int unsigned,
?? name varchar(20),
?? gender varchar(20),
?? age int,
?? birthday date,
?? address varchar(20)
);
-
字符串類型
-
日期類型
對表結(jié)構(gòu)的其它操作
1:查看當(dāng)前數(shù)據(jù)庫的所有表
show tables ;
2:查看數(shù)據(jù)表的創(chuàng)建語句
show create table 數(shù)據(jù)表名;
3:查看表結(jié)構(gòu)
desc 數(shù)據(jù)表名;
4:刪除表
drop table 數(shù)據(jù)表表名;
5:修改表結(jié)構(gòu)格式
語法格式
alter ?table 表名 add? 列名? 類型(長度) 【約束】;
例子:
為student表添加一個(gè)字段為:系別dept類型為department
alter table student add column dept varchar(20);
6:修改列名和類型
語法格式
修改列名和表名,alter table 表名 change 舊列名 新列名 類型(長度) [約束]
為student表中的dept字段更名為department varchar(30)
alter table student change dept department varchar(30);
7:修改表刪除列
語法格式:
alter table 表名 drop 列名;
例如:
# 刪除student表中department這列
alter table student drop department;
8:修改表名
語法格式:
rename table 表名 to 新表名;
例如:
# 將表student改名為stu
rename table student to stu
DML語法基本介紹
DML是指數(shù)據(jù)操作語言,英文全稱是Data Manipulation Language,用來對數(shù)據(jù)庫中標(biāo)的數(shù)據(jù)幾列進(jìn)行更新。
關(guān)鍵字:
-
插入Insert
-
刪除delete
-
更新update
數(shù)據(jù)插入
-
語法格式
insert into 表(列名1,列表2,列表3...) values(值1,值2,值3...);// 向表中插入某些
insert into 表 values(值1,值2,值3...);// 向表中插入所有列
例子:
向表中插入所有列
insert into student(sid, name, gender, age, birthday, address,department)
values (1,'tom','男',18,'2000-01-01','鄭州','銷售部');
?insert into student values (2,'jerry','女',28,'2001-01-01','北京','研發(fā)部');一次性插入多條數(shù)據(jù)
insert into student(sid, name, gender, age, birthday, address,department)
values (3,'tom1','男',18,'2000-01-01','鄭州','銷售部'),?????????? (4,'corky1','女',18,'2001-01-01','北京','法務(wù)部');
? ? ?
insert into student values (5,'jerry','女',28,'2001-01-01','北京','研發(fā)部'),???????????????????????????????????????? (6,'yi','男',39,'2002-01-01','上海','研發(fā)部');
數(shù)據(jù)修改
- 語法格式
update 表名 set 字段名=值,字段名=值...;
update 表名 set 字段名=值,字段名=值... where 條件;
例子:
將所有學(xué)生的地址修改為河南
update student ?set address='河南';
?
將id為1的學(xué)生的地址修改為河南
update student set address='河南' where id=1;
?
將id為2的學(xué)生的地址修改為北京,成績修成績修改為100
update student set address='北京',score=100 where id=2
數(shù)據(jù)刪除
- 語法格式:
delete from 表名[where 條件];
truncate table 表名 或者truncate 表名
例子:
1:刪除sid為3的學(xué)生數(shù)據(jù)
delete from student where sid=3;
2: 刪除表所有數(shù)據(jù)
delete from student;
3:清空表數(shù)據(jù)
truncate table student;
truncate student;
注意:delete和truncate原理不同,delete只刪除內(nèi)容,而truncate類似于drop table,可以理解為是將整個(gè)表刪除,然后再創(chuàng)建該表。
實(shí)例代碼
?
DQL語法概述
-
概念
-
數(shù)據(jù)庫管理系統(tǒng)一個(gè)重要功能就是數(shù)據(jù)查詢,數(shù)據(jù)查詢不應(yīng)只是簡單返回?cái)?shù)據(jù)庫中存儲(chǔ)的數(shù)據(jù),還應(yīng)該根據(jù)需要對數(shù)據(jù)庫進(jìn)行篩選以及確定數(shù)據(jù)以什么樣的格式顯示。
-
MySQL提供了功能強(qiáng)大,靈活的語句來實(shí)現(xiàn)這些操作。
-
MySQL數(shù)據(jù)庫使用select語句來查詢數(shù)據(jù)。
-
-
應(yīng)用?
基本查詢
-
語法格式
select
[all|distinct]
<目標(biāo)列的表達(dá)式1> [別名],
<目標(biāo)列的表達(dá)式2> [別名]...
from <表名或視圖名> [列名],<表名或視圖名> [別名]...
[where<條件表達(dá)式>]
[group by<列名>
[having<條件表達(dá)式>]]
[order by<列名>[asc|desc]]
[limit<數(shù)字或者列表>];
-
簡化版語法
select *| 列名 from 表 where 條件
-
數(shù)據(jù)準(zhǔn)備
創(chuàng)建數(shù)據(jù)庫和表
-- 創(chuàng)建數(shù)據(jù)庫
create database if not exist mydb2;
use mydb2;
-- 創(chuàng)建商品表:
create table product(
pid int primary key auto_increment, -- 商品編號(hào)
pname varchar(20) not null , -- 商品名字
price double, ?-- 商品價(jià)格
category_id varchar(20) -- 商品所屬分類
);
-
添加數(shù)據(jù)
insert into product values(null,'海爾洗衣機(jī)',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空調(diào)',5000,'c001');
insert into product values(null,'九陽電飯煲',200,'c001');
insert into product values(null,'啄木鳥襯衣',300,'c002');
insert into product values(null,'恒源祥西褲',800,'c002');
insert into product values(null,'花花公子夾克',440,'c002');
insert into product values(null,'勁霸休閑褲',266,'c002');
insert into product values(null,'海瀾之家衛(wèi)衣',180,'c002');
insert into product values(null,'杰克瓊斯運(yùn)動(dòng)褲',430,'c002');
insert into product values(null,'蘭蔻面霜',300,'c003');
insert into product values(null,'雅詩蘭黛精華水',200,'c003');
insert into product values(null,'香奈兒香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'資生堂粉底液',180,'c003');
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品鋪?zhàn)雍Ыz',17,'c004');
insert into product values(null,'三只松鼠堅(jiān)果',88,null);
數(shù)據(jù)準(zhǔn)備
1:創(chuàng)建數(shù)據(jù)庫
create database if not exists mydb2;
use mydb2;
2:創(chuàng)建商品表
create table if not exists product(
?? pid int primary key auto_increment,-- 商品編號(hào)
?? pname varchar(20) ,-- 商品名稱
?? price double, -- 商品價(jià)格
?? category_id int -- 商品所屬分類
);?alter table product modify category_id varchar(20);
3:添加數(shù)據(jù)
insert into product values(null,'海爾洗衣機(jī)',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空調(diào)',5000,'c001');
insert into product values(null,'九陽電飯煲',200,'c001');
insert into product values(null,'啄木鳥襯衣',300,'c002');
insert into product values(null,'恒源祥西褲',800,'c002');
insert into product values(null,'花花公子夾克',440,'c002');
insert into product values(null,'勁霸休閑褲',266,'c002');
insert into product values(null,'海瀾之家衛(wèi)衣',180,'c002');
insert into product values(null,'杰克瓊斯運(yùn)動(dòng)褲',430,'c002');
insert into product values(null,'蘭蔻面霜',300,'c003');
insert into product values(null,'雅詩蘭黛精華水',200,'c003');
insert into product values(null,'香奈兒香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'資生堂粉底液',180,'c003');
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品鋪?zhàn)雍Ыz',17,'c004');
insert into product values(null,'三只松鼠堅(jiān)果',88,null);
-
簡單查詢操作
查詢所有的商品
select * from product;
select pid,pname,price,category_id from product;
查詢商品名和商品價(jià)格
select? price,pname from product;
別名查詢,使用的關(guān)鍵字是as,(as可以省略的)
表別名
? ?select * from product as p;
? ?select * from product p;
? ?select p.id,u.id from product p,user u;列表名
? ?select pname as 商品名,price as 商品價(jià)格 from product;
去掉重復(fù)值
select distinct price from product;
每一行和每一行都不一樣
select distinct * from product;
查詢結(jié)果是表達(dá)式(運(yùn)算查詢):將所有商品加價(jià)10元進(jìn)行展示
select pname,price+10 as 新價(jià)格,price from product;
運(yùn)算符
-
算數(shù)運(yùn)算符
-
比較運(yùn)算符
-
邏輯運(yùn)算符
-
位運(yùn)算符
位運(yùn)算符是在二進(jìn)制數(shù)上進(jìn)行計(jì)算的運(yùn)算符。位運(yùn)算會(huì)先將操作數(shù)變成二進(jìn)制數(shù),進(jìn)行位運(yùn)算。然后再將計(jì)算結(jié)果從二進(jìn)制數(shù)變回十進(jìn)制數(shù)。
①:算數(shù)運(yùn)算符
算數(shù)運(yùn)算符
select 6+2;
select 6-2;
select 6*2;
select 6/2;
select 6 div 2;
select 6 mod 2;將所有商品價(jià)格+10元
select price+10 as 價(jià)格 from product;
?
將所有商品的價(jià)格上調(diào)10%
select price*(1+0.1) as 價(jià)格 from product;
條件查詢
算數(shù)運(yùn)算符
?
代碼實(shí)現(xiàn):
查詢商品名稱為“海爾洗衣機(jī)”的商品所有信息
select * from product where pname='海爾洗衣機(jī)';
?
查詢價(jià)格為800商品
select * from product where price=800;查詢價(jià)格不是800商品
select * from product where price <>800;
select * from product where price!=800;
select * from product where not (price=800);查詢商品價(jià)格大于等于60元的所有商品信息
select * from product where price >=60;查詢商品價(jià)格在200到1000之間所有商品
select * from product where price between 200 and 1000;
select * from product where price >= 200 and price<= 1000;
select * from product where price >= 200 && price<= 1000;查詢商品價(jià)格在200或800的所有商品
select * from product where price=200 or price=800;
select * from product where price=200 || price=800;
select * from product where price in(200,800);查詢含有'褲'字的所有商品
select * from product where pname like '%褲%'; -- %任意字符查詢以‘?!_頭的所有商品
select * from product where pname like '海%';
?
查詢第二個(gè)字為'蔻'的所有商品
select * from product where pname like '_蔻%';
?
查詢category_id為null的商品
select * from product where category_id is null;
?
查詢category_id不為null的商品
select * from product where category_id is not null;使用Least求取最小值
select LEAST(10,20,30) as small_number;
select least(10,null,30); -- 如果求最小值有一個(gè)null值,不會(huì)比較直接null使用greatest求最大值
select greatest(10,20,30) as big_number ;
select greatest(10,null,30) as big_number ;
如果求最大值有一個(gè)null值,不會(huì)比較直接null
排序查詢
聚合查詢
?
?
?
聚合函數(shù)--null值得處理
分組查詢
?