日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

sql語句

 行者花雕 2022-05-03 發(fā)布于北京

一.客戶端命令介紹

mysql

  • 1、用于數(shù)據(jù)庫的連接管理

1) 連接(略)
2) 管理:

#MySQL接口自帶的命令
\h 或 help 或?      查看幫助
\G                  格式化查看數(shù)據(jù)(key:value)
\T 或 tee            記錄日志(臨時)  永久有效加入客戶端配置文件中,不用重啟
\c(5.7可以ctrl+c)   結(jié)束命令 5.6可以直接退出數(shù)據(jù)庫
\s 或 status         查看狀態(tài)信息
\. 或 source         導入SQL數(shù)據(jù)
\u或 use             使用數(shù)據(jù)庫
\q 或 exit 或 quit   退出
system 或\!           敲命令

3)接收用戶的SQL語句

  • 2、將用戶的SQL語句發(fā)送到服務器

mysqldump

  • 1、備份數(shù)據(jù)庫和表的內(nèi)容

help命令的使用

mysql> help
mysql> help contents
mysql> help select
mysql> help create
mysql> help create user
mysql> help status
mysql> help show

source命令的使用

#在MySQL中處理輸入文件:
#如果這些文件包含SQL語句則稱為:
#1.腳本文件
#2.批處理文件
mysql> SOURCE /data/mysql/world.sql
#或者使用非交互式
mysql</data/mysql/world.sql

mysqladmin命令的使用

01)“強制回應 (Ping)”服務器。
02)關(guān)閉服務器。
03)創(chuàng)建和刪除數(shù)據(jù)庫。
04)顯示服務器和版本信息。
05)顯示或重置服務器狀態(tài)變量。
06)設置口令。
07)重新刷新授權(quán)表。
08)刷新日志文件和高速緩存。
09)啟動和停止復制。
10)顯示客戶機信息。

#查看MySQL存活狀態(tài)
#前提在配置文件中配置成可客戶端
[client]
mysqladmin   ping #判斷mysql命令是否存活,腳本中寫絕對路徑,否則不識別
[root@db01 ~]# mysqladmin -uroot -p123 ping
#查看MySQL狀態(tài)信息
[root@db01 ~]# mysqladmin -uroot -p123 status
#關(guān)閉MySQL進程
[root@db01 ~]# mysqladmin -uroot -p123 shutdown
#查看MySQL參數(shù)
[root@db01 ~]# mysqladmin -uroot -p123 variables
#刪除數(shù)據(jù)庫
[root@db01 ~]# mysqladmin -uroot -p123 drop DATABASE
#創(chuàng)建數(shù)據(jù)庫
[root@db01 ~]# mysqladmin -uroot -p123 create DATABASE
#重載授權(quán)表
[root@db01 ~]# mysqladmin -uroot -p123 reload
#刷新binlog日志
[root@db01 ~]# mysqladmin -uroot -p123 flush-log
#刷新緩存主機
[root@db01 ~]# mysqladmin -uroot -p123 reload
#修改口令
[root@db01 ~]# mysqladmin -uroot -p123 password

二.接收用戶的SQL語句

  • 1.什么是SQL

結(jié)構(gòu)化的查詢語句

  • 2.SQL的種類

DDL:數(shù)據(jù)定義語言

庫對象:庫名字、庫屬性
開發(fā)規(guī)范:庫名小寫

比較危險,刪庫是默認刪除小寫,一般不設置

創(chuàng)建庫:create database|schema

#創(chuàng)建oldboy數(shù)據(jù)庫
mysql> create database oldboy;
#創(chuàng)建OLDBOY數(shù)據(jù)庫
mysql> create database OLDBOY;
#查看數(shù)據(jù)庫
mysql> show databases;
#查看oldboy的創(chuàng)建語句(DQL)
mysql> show create database oldboy;
#查看創(chuàng)建數(shù)據(jù)庫語句幫助
mysql> help create database
#創(chuàng)建oldboy數(shù)據(jù)庫添加屬性
mysql> create database  if not exists testa collate utf8_general_ci charset utf8;
#查詢字符集,校驗規(guī)則
mysql> show global variables like '%server';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| character_set_server | utf8            |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
2 rows in set (0.00 sec)

刪庫:drop database

#刪除oldboy數(shù)據(jù)庫
mysql> drop database oldboy;

修改定義庫:alter database

#修改oldboy數(shù)據(jù)庫屬性
mysql> alter database oldboy charset gbk;
#查看oldboy的創(chuàng)建語句(DQL)
mysql> show create database oldboy;
#修改校驗規(guī)則
mysql>alter database oldboy collate utf8_bin

表對象:列名、列屬性、約束

創(chuàng)建表:create table (開發(fā)做)

#查看創(chuàng)建表語句幫助
mysql> help create table
#創(chuàng)建表
mysql> create table student(
sid INT,
sname VARCHAR(20),
sage TINYINT,
sgender ENUM('m','f'),
cometime DATETIME);

數(shù)據(jù)類型
int: 整數(shù) -2^31 ~ 2^31 -1 (zerofill用0自動補全)
varchar:字符類型 (變長)
char: 字符類型 (定長)
tinyint: 整數(shù) -128 ~ 128
enum: 枚舉類型
datetime: 時間類型 年月日時分秒 8字節(jié)

timestamp 4字節(jié) ,2038年過期

#創(chuàng)建表加其他屬性
mysql> create table student(
sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '學號’,
sname VARCHAR(20) NOT NULL COMMENT '學生姓名’,
sage TINYINT UNSIGNED COMMENT '學生年齡’,
sgender ENUM('m','f')  NOT NULL DEFAULT 'm’ COMMENT '學生性別’,
cometime DATETIME NOT NULL COMMENT '入學時間’)chatset utf8 engine innodb;
#查看建表語句
mysql> show create table student;
#查看表
mysql> show tables;
#查看表中列的定義信息
mysql> desc student;#查看表
mysql> show create  table student;#查看注釋

#正規(guī)建表
create table student2( sid int not null primary key auto_increment comment '學號',  sname varchar(10) not null comment '學生姓 名',  sage tinyint unsigned comment '學生年齡',  sgender enum('m','f') not null default 'm' comment '學生性別', datetime not null default now() comment '入學時間');


約束:

not null: 非空

auto_increment: 自增

primary key: 主鍵 (唯一,切非空)
unique key: 單獨的唯一的(可以為空)
pk=uk+not null
default: 默認值
unsigned: 非負數(shù)
comment: 注釋

刪除表

#刪除表
mysql> drop table student;

修改表定義:alter table (開發(fā)做)

#修改表名
mysql> alter table student rename stu;
#添加列和列定義
mysql> alter table stu add age int;
#添加多個列
mysql> alter table stu add test varchar(20),add qq int;
#指定位置進行添加列(表首)
mysql> alter table stu add classid varchar(20) first;
#指定位置進行添加列(指定列)
mysql> alter table stu add phone int after age;#注意前面加入的,
#刪除指定的列及定義
mysql> alter table stu drop qq;
#修改列及定義(列屬性)
mysql> alter table stu modify sid varchar(20);
#修改列及定義(列名及屬性)
mysql> alter table stu change phone telphone char(20);
#mysql  5.7版本改密碼
alter user root@'localhost' identified by '123';

DCL:數(shù)據(jù)控制語言

針對權(quán)限進行控制

grant

#授權(quán)root@10.0.0.51用戶所有權(quán)限(非炒雞管理員)
mysql> grant all on *.* to root@'10.0.0.51' identified by 'oldboy123';
#怎么去授權(quán)一個炒雞管理員呢?(priviliges)
mysql> grant all on *.* to root@'10.0.0.51' identified by 'oldboy123' with grant option;
#其他參數(shù)(擴展)
max_queries_per_hour:一個用戶每小時可發(fā)出的查詢數(shù)量
max_updates_per_hour:一個用戶每小時可發(fā)出的更新數(shù)量
max_connetions_per_hour:一個用戶每小時可連接到服務器的次數(shù)
max_user_connetions:允許同時連接數(shù)量

revoke

#查看庫權(quán)限的
show grants for pril@'%';
#收回select權(quán)限
mysql> revoke select on *.* from root@'10.0.0.51';
#查看權(quán)限
mysql> show grants for root@'10.0.0.51';

DML:數(shù)據(jù)操作語言

操作表的數(shù)據(jù)行信息

insert

#基礎(chǔ)用法,插入數(shù)據(jù),前面不寫,所有值一一對應,沒有給null
mysql> insert into stu values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#規(guī)范用法,插入數(shù)據(jù) (只需跟前面的key值相對應)
mysql> insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#插入多條數(shù)據(jù)
mysql> insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456),
('linux02',2,NOW(),'zhangsi',21,'f',NOW(),111,1234567);

update

#不規(guī)范
mysql> update student set sgender='f';
#規(guī)范update修改
mysql> update student set sgender='f' where sid=1;
#如果非要全表修改,where后面先敲出來
mysql> update student set sgender='f' where 1=1;

delete

#不規(guī)范
mysql> delete from student;
#規(guī)范刪除(危險),刪除表里面的內(nèi)容,drop刪除表,必須接條件
mysql> delete from student where sid=3;#范圍  sid>2 and sid<10
#DDL刪除表,一下刪除
mysql> truncate table student;

  • 1、使用偽刪除

使用update代替delete

1)額外添加一個狀態(tài)列

mysql> alter table student add status enum('1','0') default 1;#添加字段 status

2)使用update

mysql> update student set status='0' where sid=1;

3)應用查詢存在的數(shù)據(jù)后面接條件

mysql> select * from student where status=1;

  • 2、使用觸發(fā)器(了解)

trigger

DQL:數(shù)據(jù)查詢語言

select:基礎(chǔ)用法

#常用用法
mysql> select countrycode,district from city;
#查詢單列
mysql> select countrycode from city;
#行級查詢
mysql> select countrycode,district from city limit 2;
mysql> select id,countrycode,district from city limit 2,2;
#條件查詢
mysql> select name,population from city where countrycode='CHN';
#多條件查詢
mysql> select name,population from city where countrycode='CHN' and district='heilongjiang';
#模糊查詢
mysql> select name,population,countrycode from city where countrycode like '%H%' limit 10;
mysql> select * from city where countrycode like 'H%';
mysql> select * from city where countrycode like '%H';
#排序查詢(順序)
mysql> select id,name,population,countrycode from city order by countrycode limit 10;
#排序查詢(倒敘)
mysql> select id,name,population,countrycode from city order by countrycode desc limit 10;
#范圍查詢(>,<,>=,<=,<>(!=))
mysql> select * from city where population>=1410000;
#范圍查詢OR語句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范圍查詢IN語句
mysql> select * from city where countrycode in ('CHN','USA');

#去重
mysql> select count(distinct(sgender)) from student2;

#group by + 聚合函數(shù)
#聚合函數(shù)種類:
#max()
#min()
#avg()
#sum()
#count()
#distinct()

#password()
mysqladmin -uroot -p123   password  1

#now()
#database()
+------------+
| database() |
+------------+
| world      |
+------------+
#此時此刻,我想吟詩一首
1.遇到統(tǒng)計想函數(shù)
2.形容詞前groupby
3.函數(shù)中央是名詞
4.列名select后添加

#方便查看相關(guān)聯(lián)的字段
mysql> select * from world.city limit 1\G

#統(tǒng)計世界上每個國家的總?cè)丝跀?shù)
select countrycode,sum(population) from city group by countrycode;
#統(tǒng)計中國各個省的人口數(shù)量(練習)
不加別名:
mysql> select District,sum(population) from city where countrycode='CHN' group by District order by sum(population);

別名:
mysql> select District as 省,sum(population) as 人口 from city where countrycode='CHN' group by 省 order by 人口;

#統(tǒng)每個國家的城市數(shù)量(練習)
select countrycode,count(name) from city group by countrycode order by count(name);

mysql> select countrycode,count(name) from city where countrycode='chn' group by countrycode order by count(name);

#and
mysql> select * from city where countrycode='CHN' and id>500;

#or
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#in
mysql> select * from city where countrycode in ('CHN','USA');

####### 聯(lián)合查詢 效率比in和or高
mysql> select * from city where countrycode='CHN' union all select * from city where countrycode='USA';

三.字符集定義

  • 1.什么是字符集(Charset)

字符集:是一個系統(tǒng)支持的所有抽象字符的集合。字符是各種文字和符號的總稱,包括各國家文字、標點符號、圖形符號、數(shù)字等。

img?

  • 2.MySQL數(shù)據(jù)庫的字符集

1)字符集(CHARACTER)
2)校對規(guī)則(COLLATION)

  • 3.MySQL中常見的字符集

1)UTF8

2)LATIN1
3)GBK

  • 4.常見校對規(guī)則31)ci:大小寫不敏感
    2)cs或bin:大小寫敏感
  • 5.我們可以使用以下命令查看
mysql> show charset;
mysql> show collation;

四.字符集設置

#查詢字符集,校驗規(guī)則
mysql> show global variables like '%server';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| character_set_server | utf8            |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
2 rows in set (0.00 sec)

mysql> show global variables like '%server';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| character_set_server | utf8            |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
2 rows in set (0.00 sec)


mysql> mysql> select * from schemata;
+--------------+--------------------+----------------------------+------------------------+---
| CATALOG_NAME | SCHEMA_NAME        | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQ
+--------------+--------------------+----------------------------+------------------------+---
| def          | information_schema | utf8                       | utf8_general_ci        | NU
| def          | linux50            | utf8                       | utf8_general_ci        | NU
| def          | lq                 | utf8                       | utf8_general_ci        | NU
| def          | mysql              | utf8                       | utf8_general_ci        | NU
| def          | performance_schema | utf8                       | utf8_general_ci        | NU
| def          | world              | utf8                       | utf8_general_ci        | NU
+--------------+--------------------+----------------------------+------------------------+---
6 rows in set (0.00 sec)


  • 1.操作系統(tǒng)級別
[root@db01 ~]# source /etc/sysconfig/i18n
[root@db01 ~]# echo $LANG
zh_CN.UTF-8

vim /etc/locale.conf
LANG="en_US.UTF-8"

  • 2.操作系統(tǒng)客戶端級別(SSH)
  • 3.MySQL實例級別

方法1:在編譯安裝時候就指定如下服務器端字符集。

cmake . 
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all 

方法2:在配置文件中設置字符集

修改配置文件
[mysqld]
character-set-server=utf8

臨時
mysql>set character_set_server=utf8

  • 4.建庫級別
mysql> create database oldboy charset utf8 default collate = utf8_general_ci;

  • 5.建表級別

    修改數(shù)據(jù)庫的字符集

    alter database lq charset utf8;
    
    

    修改表的字符集

    alter table student charset utf8
    
    

    企業(yè)中修改某個庫中的所有表字符集

    # mysqldump -uroot -p123 -B xx > /tmp/xx.sql
    # vim /tmp/xx.sql
    # :%s#gbk#utf8#g
    # mysql -uroot -p123 < /tmp/xx.sql
    
    
mysql>  CREATE TABLE `test` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;


思考問題:如果在生產(chǎn)環(huán)境中,字符集不夠用或者字符集不合適該怎么處理?

img?
img?


五.select的高級用法(擴展)

  • 1.多表連接查詢(連表查詢)

集合:
[zhang3,li4,wang5]

[50,70,80]

t1:
sid 1 2 3
sname zhang3 li4 wang5

t2:
sid 1 2 3
mark 50 70 80

范式: 減少數(shù)據(jù)冗余,防止產(chǎn)生一致性問題,把一個表作為一個原子,把一張表拆到不能再拆為止。(開發(fā)階段設計規(guī)范)

例:根據(jù)兩張表的內(nèi)容查出張三的成績

select t1.sname,t2.mark from t1,t2 where t1.sid=t2.sid and t1.sname=’zhang3’;

1.1傳統(tǒng)連接(只能內(nèi)連接,只能取交集)

#世界上小于100人的人口城市是哪個國家的?
select city.name,city.countrycode,country.name 
from city,country 
where city.countrycode=country.code 
and city.population<100;

1.2 NATURAL JOIN(自連接的表要有共同的列名字)

# 人口數(shù)量大于1000000的城市所在的國家,他們都說什么語言?
city.population,city.name,city.countrycode,countrylanguage.language

select city.population,city.name,city.countrycode,countrylanguage.language
from city,countrylanguage
where city.countrycode=countrylanguage.countrycode
and city.population > 1000000;

# 人口數(shù)量大于1000000的城市所在的國家,他們都說什么語言? (自連接)
select city.population,city.name,city.countrycode,countrylanguage.language
from city natural join countrylanguage
where city.population > 1000000;

前提條件:一定要有相同的列名字,并且列中的數(shù)據(jù)一致

1.3企業(yè)中多表連接查詢(內(nèi)連接)

#查世界上人口數(shù)量小于100的城市在哪個國家,城市和國家人口數(shù)量分別是多少?
select city.name,city.population,country.name,country.population
from city,country
where city.countrycode=country.code
and city.population<100;

select city.name,city.population,country.name,country.population
from city join country
on city.countrycode=country.code
where city.population<100;

#世界上人口數(shù)量小于100的城市在哪個國家,說的什么語言?
·A join B on 1 join C on 2 join D on 3·

select city.population,city.name,country.name,countrylanguage.language
from city,country,countrylanguage
where city.countrycode=country.code and countrylanguage.countrycode=country.code
and city.population < 100;

select city.population,city.name,country.name,countrylanguage.language
from city 
join country 
on city.countrycode=country.code
join countrylanguage
on countrylanguage.countrycode=country.code
where city.population < 100;

建議:小表在前,大表在后

1.4外連接(可以加判斷)

#左外連接
mysql> select city.name as 城市名稱,country.code as 國家代碼,country.name as 國家名稱  from city left join country  on city.countrycodde=country.code  and  city.population<100 limit 10;

#右外連接
mysql> select city.name as 城市名稱,country.code as 國家代碼,country.name as 國家名稱  from city right join country  on city.countrycodde=country.code  and  city.population<100 limit 10;

1.5 UNION(合并查詢)

#范圍查詢OR語句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范圍查詢IN語句
mysql> select * from city where countrycode in ('CHN','USA');
替換為: 
mysql> select * from city where countrycode='CHN' 
union  all
select * from city where countrycode='USA' limit 10

union:去重復合并
union all :不去重復
使用情況:union<union all

補充:清除多余的加密密碼

mysql> use mysql

mysql> select password from user;
+-------------------------------------------+
| password                                  |
+-------------------------------------------+
| *E6CC90B878B948C35E92B003C792C46C58C4AF40 |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+


#修改為空
mysql> update user set password=null where password='加密';
Query OK, 4 rows affected, 4 warnings (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 4
#刷新授權(quán)表
mysql> flush privileges;

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多