免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
sql語(yǔ)句

一.客戶端命令介紹

mysql

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

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

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

3)接收用戶的SQL語(yǔ)句

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

mysqldump

  • 1、備份數(shù)據(jù)庫(kù)和表的內(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語(yǔ)句則稱(chēng)為:
#1.腳本文件
#2.批處理文件
mysql> SOURCE /data/mysql/world.sql
#或者使用非交互式
mysql</data/mysql/world.sql

mysqladmin命令的使用

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

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

二.接收用戶的SQL語(yǔ)句

  • 1.什么是SQL

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

  • 2.SQL的種類(lèi)

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

庫(kù)對(duì)象:庫(kù)名字、庫(kù)屬性
開(kāi)發(fā)規(guī)范:庫(kù)名小寫(xiě)

比較危險(xiǎn),刪庫(kù)是默認(rèn)刪除小寫(xiě),一般不設(shè)置

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

#創(chuàng)建oldboy數(shù)據(jù)庫(kù)
mysql> create database oldboy;
#創(chuàng)建OLDBOY數(shù)據(jù)庫(kù)
mysql> create database OLDBOY;
#查看數(shù)據(jù)庫(kù)
mysql> show databases;
#查看oldboy的創(chuàng)建語(yǔ)句(DQL)
mysql> show create database oldboy;
#查看創(chuàng)建數(shù)據(jù)庫(kù)語(yǔ)句幫助
mysql> help create database
#創(chuàng)建oldboy數(shù)據(jù)庫(kù)添加屬性
mysql> create database  if not exists testa collate utf8_general_ci charset utf8;
#查詢字符集,校驗(yàn)規(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)

刪庫(kù):drop database

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

修改定義庫(kù):alter database

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

表對(duì)象:列名、列屬性、約束

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

#查看創(chuàng)建表語(yǔ)句幫助
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ù)類(lèi)型
int: 整數(shù) -2^31 ~ 2^31 -1 (zerofill用0自動(dòng)補(bǔ)全)
varchar:字符類(lèi)型 (變長(zhǎng))
char: 字符類(lèi)型 (定長(zhǎng))
tinyint: 整數(shù) -128 ~ 128
enum: 枚舉類(lèi)型
datetime: 時(shí)間類(lèi)型 年月日時(shí)分秒 8字節(jié)

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

#創(chuàng)建表加其他屬性
mysql> create table student(
sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '學(xué)號(hào)’,
sname VARCHAR(20) NOT NULL COMMENT '學(xué)生姓名’,
sage TINYINT UNSIGNED COMMENT '學(xué)生年齡’,
sgender ENUM('m','f')  NOT NULL DEFAULT 'm’ COMMENT '學(xué)生性別’,
cometime DATETIME NOT NULL COMMENT '入學(xué)時(shí)間’)chatset utf8 engine innodb;
#查看建表語(yǔ)句
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 '學(xué)號(hào)',  sname varchar(10) not null comment '學(xué)生姓 名',  sage tinyint unsigned comment '學(xué)生年齡',  sgender enum('m','f') not null default 'm' comment '學(xué)生性別', datetime not null default now() comment '入學(xué)時(shí)間');


約束:

not null: 非空

auto_increment: 自增

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

刪除表

#刪除表
mysql> drop table student;

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

#修改表名
mysql> alter table student rename stu;
#添加列和列定義
mysql> alter table stu add age int;
#添加多個(gè)列
mysql> alter table stu add test varchar(20),add qq int;
#指定位置進(jìn)行添加列(表首)
mysql> alter table stu add classid varchar(20) first;
#指定位置進(jìn)行添加列(指定列)
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ù)控制語(yǔ)言

針對(duì)權(quán)限進(jì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)一個(gè)炒雞管理員呢?(priviliges)
mysql> grant all on *.* to root@'10.0.0.51' identified by 'oldboy123' with grant option;
#其他參數(shù)(擴(kuò)展)
max_queries_per_hour:一個(gè)用戶每小時(shí)可發(fā)出的查詢數(shù)量
max_updates_per_hour:一個(gè)用戶每小時(shí)可發(fā)出的更新數(shù)量
max_connetions_per_hour:一個(gè)用戶每小時(shí)可連接到服務(wù)器的次數(shù)
max_user_connetions:允許同時(shí)連接數(shù)量

revoke

#查看庫(kù)權(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ù)操作語(yǔ)言

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

insert

#基礎(chǔ)用法,插入數(shù)據(jù),前面不寫(xiě),所有值一一對(duì)應(yīng),沒(méi)有給null
mysql> insert into stu values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#規(guī)范用法,插入數(shù)據(jù) (只需跟前面的key值相對(duì)應(yīng))
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后面先敲出來(lái)
mysql> update student set sgender='f' where 1=1;

delete

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

  • 1、使用偽刪除

使用update代替delete

1)額外添加一個(gè)狀態(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)應(yīng)用查詢存在的數(shù)據(jù)后面接條件

mysql> select * from student where status=1;

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

trigger

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

select:基礎(chǔ)用法

#常用用法
mysql> select countrycode,district from city;
#查詢單列
mysql> select countrycode from city;
#行級(jí)查詢
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語(yǔ)句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范圍查詢IN語(yǔ)句
mysql> select * from city where countrycode in ('CHN','USA');

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

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

#password()
mysqladmin -uroot -p123   password  1

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

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

#統(tǒng)計(jì)世界上每個(gè)國(guó)家的總?cè)丝跀?shù)
select countrycode,sum(population) from city group by countrycode;
#統(tǒng)計(jì)中國(guó)各個(gè)省的人口數(shù)量(練習(xí))
不加別名:
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)每個(gè)國(guó)家的城市數(shù)量(練習(xí))
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)

字符集:是一個(gè)系統(tǒng)支持的所有抽象字符的集合。字符是各種文字和符號(hào)的總稱(chēng),包括各國(guó)家文字、標(biāo)點(diǎn)符號(hào)、圖形符號(hào)、數(shù)字等。

?

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

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

  • 3.MySQL中常見(jiàn)的字符集

1)UTF8

2)LATIN1
3)GBK

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

四.字符集設(shè)置

#查詢字符集,校驗(yàn)規(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)級(jí)別
[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)客戶端級(jí)別(SSH)
  • 3.MySQL實(shí)例級(jí)別

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

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

方法2:在配置文件中設(shè)置字符集

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

臨時(shí)
mysql>set character_set_server=utf8

  • 4.建庫(kù)級(jí)別
mysql> create database oldboy charset utf8 default collate = utf8_general_ci;

  • 5.建表級(jí)別

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

    alter database lq charset utf8;
    
    

    修改表的字符集

    alter table student charset utf8
    
    

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

    # 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;


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

?
?


五.select的高級(jí)用法(擴(kuò)展)

  • 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)生一致性問(wèn)題,把一個(gè)表作為一個(gè)原子,把一張表拆到不能再拆為止。(開(kāi)發(fā)階段設(shè)計(jì)規(guī)范)

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

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

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

#世界上小于100人的人口城市是哪個(gè)國(guó)家的?
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的城市所在的國(guó)家,他們都說(shuō)什么語(yǔ)言?
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的城市所在的國(guó)家,他們都說(shuō)什么語(yǔ)言? (自連接)
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的城市在哪個(gè)國(guó)家,城市和國(guó)家人口數(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的城市在哪個(gè)國(guó)家,說(shuō)的什么語(yǔ)言?
·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 城市名稱(chēng),country.code as 國(guó)家代碼,country.name as 國(guó)家名稱(chēng)  from city left join country  on city.countrycodde=country.code  and  city.population<100 limit 10;

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

1.5 UNION(合并查詢)

#范圍查詢OR語(yǔ)句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范圍查詢IN語(yǔ)句
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:去重復(fù)合并
union all :不去重復(fù)
使用情況:union<union all

補(bǔ)充:清除多余的加密密碼

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;

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
MySQL從入門(mén)到入魔(01)
第一天
Mysql數(shù)據(jù)庫(kù)入門(mén)-基本操作
mysql
MySQL數(shù)據(jù)庫(kù)基礎(chǔ)學(xué)習(xí)筆記(一)
MySQL的基本操作CRUD(增刪改查)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服