數(shù)據(jù)庫簡介
什么是數(shù)據(jù)庫(DataBase,簡稱DB)
數(shù)據(jù)庫,又稱為數(shù)據(jù)管理系統(tǒng),簡而言之可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數(shù)據(jù)運行新增、截取、更新、刪除等操作[1]。 --《維基百科》
數(shù)據(jù)庫即存放數(shù)據(jù)的倉庫,只不過這個倉庫是在計算機存儲設(shè)備上,而且數(shù)據(jù)是按一定的格式存放的。
過去人們將數(shù)據(jù)存放在文件柜里,現(xiàn)在數(shù)據(jù)量龐大,已經(jīng)不再適用。
數(shù)據(jù)庫是長期存放在計算機內(nèi)、有組織、可共享的數(shù)據(jù)即可。
數(shù)據(jù)庫中的數(shù)據(jù)按一定的數(shù)據(jù)模型組織、描述和儲存,具有較小的冗余度、較高的數(shù)據(jù)獨立性和易擴展性,并可為各種 用戶共享。
本質(zhì)上數(shù)據(jù)庫就是存儲在電腦上的一個文件夾,而數(shù)據(jù)庫表就是文件夾下的文件。
什么是數(shù)據(jù)庫管理系統(tǒng)(DataBase Management System 簡稱DBMS)
用于管理數(shù)據(jù)庫的軟件系統(tǒng)。DBMS解決了如何科學地組織和存儲數(shù)據(jù),如何高效獲取和維護數(shù)據(jù)的問題。
常見的DBMS
MYSQL就是一個數(shù)據(jù)庫管理軟件。又叫數(shù)據(jù)管理系統(tǒng),即DBMS(dababase management system)。大型數(shù)據(jù)庫通常運行于一個獨立的數(shù)據(jù)庫服務(wù)器上,客戶端或者其他服務(wù)器通過網(wǎng)絡(luò)訪問數(shù)據(jù)庫。這就用到了數(shù)據(jù)庫管理系統(tǒng)。MYSQL就是數(shù)據(jù)庫管理軟件,而非數(shù)據(jù)庫。但通常我們所談?wù)摰臄?shù)據(jù)庫,其實是在談?wù)揇BMS。
關(guān)系型數(shù)據(jù)庫(管理系統(tǒng))
- MySQL數(shù)據(jù)庫
- Microsoft SQL Server數(shù)據(jù)庫
- Oracle數(shù)據(jù)庫
- Postgresql數(shù)據(jù)庫
- Sybase數(shù)據(jù)庫
- DB2數(shù)據(jù)庫
- Access數(shù)據(jù)庫
非關(guān)系型數(shù)據(jù)庫
- Mongodb數(shù)據(jù)庫
- Redis數(shù)據(jù)庫
- Hbase數(shù)據(jù)庫
- Memcaced數(shù)據(jù)庫
- BigTable數(shù)據(jù)庫
mysql是目前各大企業(yè)使用的主流數(shù)據(jù)庫。它主要的優(yōu)勢就是開放源代碼,因為開放源代碼這個數(shù)據(jù)庫是免費的,現(xiàn)在是甲骨文公司的產(chǎn)品。
oracle主要用于銀行、鐵路、飛機場等。該數(shù)據(jù)庫功能強大,軟件費用高。也是甲骨文公司的產(chǎn)品。
sql server是微軟公司的產(chǎn)品,主要應(yīng)用于大中型企業(yè),如聯(lián)想、方正等。
數(shù)據(jù)庫管理工具
- navicat
- mysql workbench
理解數(shù)據(jù)庫服務(wù)器、數(shù)據(jù)管理系統(tǒng)、數(shù)據(jù)庫、表與記錄的關(guān)系
記錄(record):1 小明 男 二班 9歲 (多個字段的信息組成一條記錄,即數(shù)據(jù)庫表文件中的一行內(nèi)容)
表(table):student,class,school(即文件)
數(shù)據(jù)庫(database):oldboy_stu(即文件夾)
數(shù)據(jù)庫管理系統(tǒng)(DBMS):如mysql(是一個軟件)
數(shù)據(jù)庫服務(wù)器:一臺計算機(對內(nèi)存性能要求比較高)
總結(jié):
數(shù)據(jù)庫服務(wù)器-:運行數(shù)據(jù)庫管理軟件
數(shù)據(jù)庫管理軟件:管理-數(shù)據(jù)庫
數(shù)據(jù)庫:即文件夾,用來組織文件/表
表:即文件,用來存放多行內(nèi)容/多條記錄
MySQL簡介
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),目前屬于 Oracle 旗下公司。MySQL 是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),在 WEB 應(yīng)用方面MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應(yīng)用軟件之一。
mysql就是一個基于socket編寫的C/S架構(gòu)的軟件
客戶端軟件
mysql自帶:如mysql命令,mysqldump命令等
python模塊:如pymysql
MYSQL安裝與管理
linux下安裝
#二進制rpm包安裝
yum -y install mysql-server mysql
macOS下安裝
brew install mysql
查看mysql版本
mysql --version
啟動mysql
mysql.server start
設(shè)置mysql密碼
mysqladmin -u root password "123456"
修改mysql密碼
mysqladmin -u root -p12345678(新密碼) password 123456(舊密碼)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'
You must reset your password using ALTER USER statement before executing this statement.
安裝完mysql 之后,登陸以后,不管運行任何命令,總是提示這個
mac mysql error You must reset your password using ALTER USER statement before executing this statement.
解決方法:
step 1: SET PASSWORD = PASSWORD('your new password');
step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
step 3: flush privileges;
登錄設(shè)置密碼
初始狀態(tài)下,管理員root,密碼為空,默認只允許從本機登錄localhost
設(shè)置密碼
# mysqladmin -uroot password "123" 設(shè)置初始密碼 由于原密碼為空,因此-p可以不用
# mysqladmin -uroot -p"123" password "456" 修改mysql密碼,因為已經(jīng)有密碼了,所以必須輸入原密碼才能設(shè)置新密碼
命令格式:
# mysql -h172.31.0.2 -uroot -p456
# mysql -uroot -p
# mysql 以root用戶登錄本機,密碼為空
linux下破解密碼
linux平臺下,破解密碼的兩種方式
方法一:刪除授權(quán)庫mysql,重新初始化
# rm -rf /var/lib/mysql/mysql #所有授權(quán)信息全部丟失?。?!
# systemctl restart mariadb
# mysql
方法二:啟動時,跳過授權(quán)庫
# vim /etc/my.cnf #mysql主配置文件
[mysqld]
skip-grant-table
# systemctl restart mariadb
# mysql
MariaDB [(none)]> update mysql.user set password=password("123") where user="root" and host="localhost";
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> \q
# #打開/etc/my.cnf去掉skip-grant-table,然后重啟
# systemctl restart mariadb
# mysql -u root -p123 #以新密碼登錄
SQL 語句
mysql服務(wù)端軟件即mysqld幫我們管理好文件夾以及文件,前提是作為使用者的我們,需要下載mysql的客戶端,或者其他模塊來連接到mysqld,然后使用mysql軟件規(guī)定的語法格式去提交自己命令,實現(xiàn)對文件夾或文件的管理。該語法即sql(Structured Query Language 即結(jié)構(gòu)化查詢語言)
SQL語言主要用于存取數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和管理關(guān)系數(shù)據(jù)庫系統(tǒng),SQL語言由IBM開發(fā)。SQL語言分為3種類型:
1、DDL語句 數(shù)據(jù)庫定義語言: 數(shù)據(jù)庫、表、視圖、索引、存儲過程,例如CREATE DROP ALTER
2、DML語句 數(shù)據(jù)庫操縱語言: 插入數(shù)據(jù)INSERT、刪除數(shù)據(jù)DELETE、更新數(shù)據(jù)UPDATE、查詢數(shù)據(jù)SELECT
3、DCL語句 數(shù)據(jù)庫控制語言: 例如控制用戶的訪問權(quán)限GRANT、REVOKE
初識SQL語句
#1\. 操作數(shù)據(jù)庫
增:create database db1 charset utf8;
查:show databases;
改:alter database db1 charset latin1;
刪除: drop database db1;
#2\. 操作庫表
先切換到文件夾下:use db1
增:create table t1(id int,name char);
查:show tables
改:alter table t1 modify name char(3);
alter table t1 change name name1 char(2);
刪:drop table t1;
#3\. 操作表中的記錄
增:insert into t1 values(1,'egon1'),(2,'egon2'),(3,'egon3');
查:select * from t1;
改:update t1 set name='sb' where id=2;
刪:delete from t1 where id=1;
數(shù)據(jù)庫的操作
數(shù)據(jù)庫命名規(guī)則
可以由字母、數(shù)字、下劃線、@、#、$
區(qū)分大小寫
唯一性
不能使用關(guān)鍵字如 create select
不能單獨使用數(shù)字
最長128位
數(shù)據(jù)庫CRUD
# charset可省略
CREATE DATABASE 數(shù)據(jù)庫名 charset utf8;
# 查看數(shù)據(jù)庫
show databases;
# 查看或創(chuàng)建(如果沒有)數(shù)據(jù)庫
show create database db1;
# 查看當前數(shù)據(jù)庫
select database();
選擇數(shù)據(jù)庫
# 進入數(shù)據(jù)庫
USE 數(shù)據(jù)庫名
# 刪除數(shù)據(jù)庫
DROP DATABASE 數(shù)據(jù)庫名;
# 修改數(shù)據(jù)庫
alter database db1 charset utf8;
MySQL使用存儲引擎
方法1:建表時指定
> create table innodb_t1(id int,name char)engine=innodb;
> create table innodb_t2(id int)engine=innodb;
> show create table innodb_t1;
> show create table innodb_t2;
方法2:在配置文件中指定默認的存儲引擎
/etc/my.cnf
[mysqld]
default-storage-engine=INNODB
innodb_file_per_table=1
練習:創(chuàng)建四個表,分別使用innodb,myisam,memory,blackhole存儲引擎
> create table t1(id int)engine=innodb;
> create table t2(id int)engine=myisam;
> create table t3(id int)engine=memory;
> create table t4(id int)engine=blackhole;
> quit
> ls /var/lib/mysql/db1/ #發(fā)現(xiàn)后兩種存儲引擎只有表結(jié)構(gòu),無數(shù)據(jù)
db.opt t1.frm t1.ibd t2.MYD t2.MYI t2.frm t3.frm t4.frm
#memory,在重啟mysql或者重啟機器后,表內(nèi)數(shù)據(jù)清空
#blackhole,往表內(nèi)插入任何數(shù)據(jù),都相當于丟入黑洞,表內(nèi)永遠不存記錄
表的操作
創(chuàng)建表
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件],
字段名3 類型[(寬度) 約束條件]
);
#注意:
1\. 在同一張表中,字段名是不能相同
2\. 寬度和約束條件可選
3\. 字段名和類型是必須的
例:
create table article(
id int(10) not null auto_increment,
img text default null comment '縮略圖',
title varchar(80) default null comment '文章標題',
summary varchar(300) default null comment '文章簡介',
content text default null comment '文章內(nèi)容',
createTime timestamp default null comment '發(fā)布時間',
primary key(id)
)engine=InnoDB AUTO_INCREMENT=1 comment '文章表';
mysql> create table student(
-> id int not null auto_increment,
-> name text default null comment '名字',
-> gender enum('male', 'female') default 'male' comment '性別',
-> primary key(id)
-> ) engine=InnoDB AUTO_INCREMENT=1 comment '學生表';
show tables; #查看數(shù)據(jù)庫下所有表名
查看表結(jié)構(gòu)
desc 表名;
查看表詳細結(jié)構(gòu),可加\G
show create table t1\G; #查看表詳細結(jié)構(gòu),可加\G
例:
desc student; # 等價于 describe student;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
查數(shù)據(jù)
select id,name,sex,age from student;
select * from student;
插入數(shù)據(jù)
# 只插入id
insert into article(id) values(7);
# 插入一條空記錄
insert into article() value();
# 插入記錄時可以省略into
Insert article(id, title) value(10, '操作系統(tǒng)');
# 插入多條記錄
insert into student(name, gender) values('mark', 'male'), ('jenny', 'female');
# 插入兩條記錄
insert into student values
-> (1,'jack',18,'male'),
-> (2,'mark',81,'female')
-> ;
# 插入兩條只有id的記錄
insert into t1(id) values
-> (3),
-> (4);
查看表結(jié)構(gòu)
> describe student; #查看表結(jié)構(gòu),可簡寫為desc 表名
> show create table student\G; #查看表詳細結(jié)構(gòu),可加\G
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
修改表結(jié)構(gòu)
# 修改表名 student表修改成person表
mysql> alter table student rename person;
# 添加字段 給peron表添加address字段
mysql> alter table person add address varchar(100);
# 刪除字段 刪除person表的address字段
mysql> alter table person drop address;
# 修改字段類型 用 modify
mysql> alter table person modify name char(10);
# 修改字段名 用change
mysql> alter table person change gender sex enum('male', 'female');
# 建表之后設(shè)置 primary key
mysql> alter table employee modify id int primary key;
語法
1\. 修改表名
ALTER TABLE 表名
RENAME 新表名;
2\. 增加字段
ALTER TABLE 表名
ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…],
ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…];
ALTER TABLE 表名
ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…] FIRST;
ALTER TABLE 表名
ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…] AFTER 字段名;
3\. 刪除字段
ALTER TABLE 表名
DROP 字段名;
4\. 修改字段
ALTER TABLE 表名
MODIFY 字段名 數(shù)據(jù)類型 [完整性約束條件…];
ALTER TABLE 表名
CHANGE 舊字段名 新字段名 舊數(shù)據(jù)類型 [完整性約束條件…];
ALTER TABLE 表名
CHANGE 舊字段名 新字段名 新數(shù)據(jù)類型 [完整性約束條件…];
示例:
1\. 修改存儲引擎
mysql> alter table service
-> engine=innodb;
2\. 添加字段
mysql> alter table student10
-> add name varchar(20) not null,
-> add age int(3) not null default 22;
mysql> alter table student10
-> add stu_num varchar(10) not null after name; //添加name字段之后
mysql> alter table student10
-> add sex enum('male','female') default 'male' first; //添加到最前面
3\. 刪除字段
mysql> alter table student10
-> drop sex;
mysql> alter table service
-> drop mac;
4\. 修改字段類型modify
mysql> alter table student10
-> modify age int(3);
mysql> alter table student10
-> modify id int(11) not null primary key auto_increment; //修改為主鍵
5\. 增加約束(針對已有的主鍵增加auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined
mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
6\. 對已經(jīng)存在的表增加復合主鍵
mysql> alter table service2
-> add primary key(host_ip,port);
7\. 增加主鍵
mysql> alter table student1
-> modify name varchar(10) not null primary key;
8\. 增加主鍵和自動增長
mysql> alter table student1
-> modify id int not null primary key auto_increment;
9\. 刪除主鍵
a. 刪除自增約束
mysql> alter table student10 modify id int(11) not null;
b. 刪除主鍵
mysql> alter table student10
-> drop primary key;
復制表
復制表結(jié)構(gòu)+記錄 (key不會復制: 主鍵、外鍵和索引)
mysql> create table new_service select * from service;
只復制表結(jié)構(gòu)
mysql> select * from service where 1=2; //條件為假,查不到任何記錄
Empty set (0.00 sec)
mysql> create table new1_service select * from service where 1=2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create table t4 like employees;
刪除表
DROP TABLE 表名;