數(shù)據(jù)庫(kù)MySQL

數(shù)據(jù)庫(kù)分類

|-- windowMySQL的安裝和配置:

.msi微軟安裝

|--zip包壓縮

1、下載官網(wǎng)壓縮包

2、解壓壓縮包(不要C盤、不要有中文、不要有特殊字符)

3、復(fù)制一個(gè)xx.ini,修改為my.ini

4、修改my.ini

? ? ? ? ? ?datadir=mysql的主目錄

? ? ? ? ? ? basedir=mysql的主目錄/data

5、將bin目錄添加到path中去(此電腦右鍵屬性--高級(jí)系統(tǒng)設(shè)置--環(huán)境變量--path--新建)

6、以管理員的身份運(yùn)行cmd,將路徑切換到bin來(lái)(很重要)(搜索里寫(xiě)入cmd--命令提示符右鍵--以管理員身份運(yùn)行)

7、執(zhí)行mysqld install 命令,安裝注冊(cè)mysql服務(wù)

? ? ?cd ?D:\tools\mysql-5.5.41-winx64\bin

? ? ? d:

8、啟動(dòng)服務(wù)(net start[stop|restart] ?服務(wù)名稱(eg:mysql))

9、測(cè)試

?9、進(jìn)入數(shù)據(jù)庫(kù)

? ? ? ? ? ? ? ? ? ? mysql -u用戶名稱 -p用戶密碼

? ? ? ? ? ? ? ? ? ?如:mysql -uroot -proot

|--exe msi

? ? ? ? ? ? ? ? ? ? ?不教,下一步下一步

|-- Linux

|-- apt

? ? ? ? ? sudo apt [apt-get] install mysql-server mysql-client

? ? ? ? ? sudo apt [apt-get] install mysql-server

? ? ? ? ? sudo apt [apt-get] install mysql-client

? ? ? ? ? service mysql start[|stop|restart]

|-- zip壓縮包

1、下載mysql的壓縮包 .tar.gz

2、解壓壓縮包

tar -zxvf xxx.tar.gz

3、bin配置path :兩種方案,可以配置到系統(tǒng)配置文件中或者用戶配置文件

4、啟動(dòng)mysql

? ? ? ? mysqd install

? ? ? ? service mysql start/stop

msyql的登錄

? ? ? ? ? mysql -u(user) -p(password)-h(host主機(jī))-P(port端口(默認(rèn)端口是3306))

mysql的退出 :exit quit \q ctrl+c

常見(jiàn)的操作命令

? show databases; # 顯示當(dāng)前數(shù)據(jù)庫(kù)管理系統(tǒng)中的所有數(shù)據(jù)庫(kù)

? ? ? ? ? ?show databases like "%b%" # 模糊匹配符合規(guī)則的數(shù)據(jù)庫(kù)

? use dbName; # 進(jìn)入到某個(gè)數(shù)據(jù)庫(kù)(DBName就是這個(gè)數(shù)據(jù)庫(kù)名稱)中。

? show tables; # 查看當(dāng)前數(shù)據(jù)庫(kù)下所有表

? ? ? ? ? ? ?show tables like "%p%"; #模糊匹配符合規(guī)則的表

? desc(description) tableName; # 描述某張表的結(jié)構(gòu)

? show create table tableName; # 顯示創(chuàng)建表的sql

標(biāo)準(zhǔn)sql的學(xué)習(xí):

sql(structured? query language)結(jié)構(gòu)化查詢語(yǔ)言

在1986年 ANSI sql86

在1987年 ISO sql87

? 1989 ISO? sql89

dml(database Manipulation language): 數(shù)據(jù)庫(kù)操作語(yǔ)言

? ? ? ?show查 ???create增 ? drop刪 ??alter改

ddl(database difined language):數(shù)據(jù)定義語(yǔ)言

CRUD

insert

update

delete

dql(database query language) 數(shù)據(jù)庫(kù)查詢語(yǔ)言

dcl(database controller language):數(shù)據(jù)庫(kù)控制語(yǔ)言

? grant(權(quán)限)

invoke

dba(database administrator)數(shù)據(jù)庫(kù)管理員

三個(gè)創(chuàng)建:表 數(shù)據(jù)庫(kù) 視圖

四條語(yǔ)句:CRUD

五種約束:主鍵、外鍵、唯一、非空、默認(rèn)值

================================================================

DBMS ===> db ===> table、view、index……

如何創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)

create database dbName [charset=utf8 ENGINE=InnoDB]

# 創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),名稱是db_xx

create database db_py1803;

create database db_py1803 charset=utf8

刪除一個(gè)數(shù)據(jù)庫(kù)

drop database DBName (慎用)

drop database db_py1803;

如何創(chuàng)建一張表

作業(yè):請(qǐng)說(shuō)明如下四種類型的不同之處?

char nchar varchar nvarchar

create table tableName(

? ? ? ? ? ? ? ? ?id int, #

? ? ? ? ? ? ? ? name varchar(50), liujianhong

? ? ? ? ? ? ? ?name char(50),

? ? ? ? ? ? ? age int,

? ? ? ? ? ? ? nickname char(50)

)

drop table tableName;

# 創(chuàng)建一張表 t_user

create table t_user(

? ? ? ? ? ? ? id int primary key (主鍵不能為空,不能重復(fù))auto_increment, -- 主鍵 自動(dòng)增長(zhǎng)

? ? ? ? ? ? ? username varchar(50) not null,

? ? ? ? ? ? ? password varchar(100) not null,

? ? ? ? ? ? ?age int default 18,

? ? ? ? ? ? ?gender varchar(5) default (默認(rèn)值)'nan',

? ? ? ? ? ? ? nickname varchar(255) unique (無(wú)逗號(hào)) # unique 表示唯一約束

)

# CRUD 增刪改查

# c(create) r(Retrieve)u(update) d(delete)

# 插入數(shù)據(jù)

insert into tableName(字段1,字段2……,字段n) values(值1,值2……,值n)

# 插入一條數(shù)據(jù)到t_user表里面來(lái)

insert into t_user(id,username,password,age,gender,nickname)

values(1,"liushuaige",'123456',16,'nan',"shuaigeliu")

insert into t_user(id,username,password,age,gender,nickname)

values(1,"liushuaige",'123456',16,'nan',"shuaigeliu")

insert into t_user(id,username,password,age,gender,nickname)

values(null,"liushuaige",'123456',16,'nan',"shuaigege")

insert into t_user values(null,"liushuaiguo",'123456',16,'nan',"shuaige")

insert into t_user(id,password,username,nickname)

values(default,'123456',"liushuaige","ooo");

truncate? t_user;

# 簡(jiǎn)單的查詢某張表

select 字段1,字段2……,字段n from tableName;

select id,username,gender,age,password,nickname from t_user;

select * from t_user;

select id,username from t_user;

# 更新(修改)

update tableName set 字段1=新值[,字段2=新值……字段n=新值] where 條件

update t_user set username = 'liuouba';

update t_user set username = 'liushuaige' where id = 1;

update t_user set username = 'oubaliu' where id >3;

#刪除 delete

delete from tableName where condition

delete from t_user where id = 6






配置mysql的遠(yuǎn)程登錄:

1、為什么配置遠(yuǎn)程登錄?

2、如何配置遠(yuǎn)程登錄

1、到mysql數(shù)據(jù)庫(kù) use mysql

2、查詢user? ==== select host,user from user;

3、修改root用戶

update user set host='%' where user='root';(%表示通配所有,可以支持遠(yuǎn)程登錄)

4、修改/etc/mysql/mysql.conf.d/mysqld.cnf ?

?進(jìn)入vim中找到43行將bind-address 注釋掉

5、重啟服務(wù) sudo service mysql ?restart;

===============================================================

create table t_user(

? ? ? ? ? ? ? ?id int primary key auto_increment,

? ? ? ? ? ? ? ?name varchar(50) not null, # 不能為空

? ? ? ? ? ? ? ?age int default 18, # 如果填入的不寫(xiě),則默認(rèn)填寫(xiě)18歲

? ? ? ? ? ? ? ?gender varchar(10) default(默認(rèn)) 'nan',

? ? ? ? ? ? ? ?nickname varchar(50) unique, -- 不能重復(fù)

? ? ? ? ? ? ? address char(50)

)



mysql數(shù)據(jù)類型

整型 :int

浮點(diǎn)型:float , decimal(7,2)#7表示總長(zhǎng)度,2表示小數(shù)位數(shù)

字符串:name varchar(30)? liujianhong ?不超過(guò)30就占用本身的大小 (一般用于字符串長(zhǎng)度不定時(shí)可以剩內(nèi)存)

name char(30) ?字符串長(zhǎng)度不超過(guò)30時(shí),也占用30的位置 (一般用于長(zhǎng)度確定的時(shí)候)

文本類型:text


日期:data(只能表示年月日) time (只能表示時(shí)分秒)datatime(年月日時(shí)分秒)now()當(dāng)前日期?

create table t_user(

? ? ? ? ? ? ? ? ?id int primary key auto_increment,

? ? ? ? ? ? ? ? ?name varchar(50) not null,

? ? ? ? ? ? ? ? ?birthday datetime default '2018-06-22 00:00:00',

? ? ? ? ? ? ? ? ?intro text

);

====================================================================

grant語(yǔ)言的使用

grant all 權(quán)限?db_py05.* 這個(gè)庫(kù)的所有表 ljh表示用戶名稱localhost表示地址?identified by 'ljhljh'表示修改密碼為ljhljh 如果用戶存在則修改,不存在則創(chuàng)建ljh

grant all on db_py05.* to 'ljh'@'localhost' identified by 'ljhljh';

grant all on *.* to 'root'@'%' identified by '123456';第一個(gè)*表示所有的庫(kù),第二個(gè)*表示所有的表

====================================================================

CURD

查詢

select 字段1,字段2,……,字段n from tableName [where condition]條件

select ?* ?from ?tableName;

增加

insert into tabelName(字段1,字段2,……,字段n) values(值1,值2,……,值n);

insert into tabelName values(值1, 值2,……,值n); # 必須寫(xiě)全

更新

update tableName set 字段1=新值,字段2=新值,字段3=新值 [where condition]

?刪除

delete from tableName? [where condition](如果不帶條件,則表內(nèi)容清空)

# truncate 刪除 慎用

修改表的結(jié)構(gòu):alter

alter table t_user update/change/modify type text;修改字段type的類型

alter table t_user add type int;給表增加字段

alter table t_user drop type;刪除字段

===================================================================

CRUD

insert into tableName values();

update tableName set? where condition;

delete from tableName where condition;

select * from tableName;

select 字段1,字段2…… from tableName;

mysqldump


分頁(yè):

當(dāng)前頁(yè) pageNow 用戶決定

每頁(yè)顯示多少條 pageSize 程序員決定

總有多少頁(yè) pageCount=allCount/pageSize 計(jì)算得到

總有多少條記錄 allCount 查詢得到 select count(*) from tName;


# 查詢id為1的用戶

SELECT * FROM t_user WHERE id = 1;

# 查詢id大于1的用戶

select * FROM t_user WHERE id > 1;

select * FROM t_user WHERE id < 5;

select * FROM t_user WHERE id >= 3;

select * FROM t_user WHERE id != 3;

select * FROM t_user WHERE id <> 3;


-- 查詢年齡大于10并且姓名為王五的人的性別

SELECT gender FROM t_user where age > 10 AND username = '王五';

# 查詢年齡大于等于20 或者性別是男的人的詳情

SELECT * FROM t_user WHERE age >= 20 OR gender = '男';


# 查詢性別為空

# 注意:在sql中,判斷為空,或者判斷不為空,需要使用is,而不是等于符號(hào)

null空和空字符串的區(qū)別

SELECT * FROM t_user where gender is null;

SELECT * FROM t_user where gender is not null;包含非空和空字符串


# 查詢所有人的年齡的和

SELECT SUM(age) FROM t_user;

# 找出年齡最大的那個(gè)哥們

SELECT max(age) FROM t_user;

# 找出年齡最大的那個(gè)姐們

SELECT min(age) FROM t_user;

# 求當(dāng)前所有人的平均年齡

SELECT avg(age) FROM t_user;


# 共有多少人?

SELECT count(*) FROM t_user;

# 共有多少人?

SELECT count(*) as counts FROM t_user;

# 共有多少人?

SELECT count(*) counts FROM t_user;


# 年齡大于16 并且 年齡小于 20歲

SELECT * FROM t_user where age >= 16 AND age <= 20;

# 年齡大于16并且年齡小于 20歲

SELECT * FROM t_user where age BETWEEN 16 AND 20;

# 查詢年齡為5 8 16 20 30 50的人

SELECT * from t_user where age in (5,8,16,20,30,50);

# 查詢年齡為5 8 16 20 30 50的人

SELECT * from t_user where age not in (5,8,16,20,30,50);


# 模糊查詢? like

# 查詢姓王的人

SELECT * FROM t_user where username LIKE '王%'

# 查詢名稱中第二個(gè)字是建的

SELECT * FROM t_user where username LIKE '_建%'

SELECT * FROM t_user where username LIKE '%哥%'

SELECT * FROM t_user;


# 排序

-- 查詢所有用戶,默認(rèn)使用年齡升序排列

SELECT * FROM t_user order by age ;

SELECT * FROM t_user where age > 10 order by age asc;

SELECT * FROM t_user where age > 10 order by age desc;

SELECT * FROM t_user where age > 10 order by age desc, id asc;


# 分組

# 統(tǒng)計(jì)當(dāng)前男生有多少人,女生多少人

SELECT count(gender) FROM t_user;

SELECT gender,count(*) FROM t_user group by gender;

SELECT age FROM t_user GROUP BY age;

# 通過(guò)性別進(jìn)行分組,得到組員多余3人的

# having 必須是出現(xiàn)group by之后,它是對(duì)分組結(jié)果進(jìn)行篩選

SELECT gender,count(*) as counts FROM t_user group by gender having counts > 3;

SELECT * FROM t_user LIMIT 5;前五個(gè)值

SELECT * FROM t_user LIMIT 0,2;(0哪開(kāi)始,id=1開(kāi)始,2表示查兩個(gè))

SELECT * FROM t_user LIMIT 2,2;

SELECT * FROM t_user LIMIT 4,2;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容