標籤:exit 用戶端 rom .sql 執行個體 備份 使用 not primary
Mysql資料庫的基本使用
服務端
1.安裝:sudo apt-get install mysql-service
2.啟動:sudo service mysql start
3.查看進程中是否存在mysql服務:ps ajx|grep mysql (/usr/sbin/mysqld)
4.停止服務:sudo service mysql stop
5.重啟服務:sudo service mysql restart
6.查看設定檔:設定檔目錄為/etc/mysql/mysql.conf.d/mysqld.cnf
用戶端
1.安裝:sudo apt-get install mysql-client
2.串連伺服器:mysql -u root(使用者名稱) -p mysql(密碼)
3.退出:ctrl+d 或 exit 或 quit
4.修改輸入提示符:prompt 提示符 (\D完整日期,\d資料庫名稱,\u使用者,\h主機)
5.清屏:ctrl+shift+l
資料庫操作
1.查看所有資料庫: show databases;
2.建立資料庫:create database 資料庫名 charset = utf-8;
3.使用資料庫:use 資料庫名;
4.查看當前使用的資料庫:select database();
5.刪除資料庫:drop database 資料庫名;
資料表操作
1.查看所有資料表: show tables;
2.建立表:
create table table_name(
column1 datatype constraint,
column2 datatype,
...
columeN datatype,
PRIMARY KEY(one or more columns)
);
執行個體:
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) not null,
age tinyint unsigned default 0,
height decimal(5,2),
gender enum("男","女","保密") default "保密"
);
3.查看錶的建立語句: show create table 表名;
4.查看錶結構: desc 表名;
5.修改表結構
5.1.添加欄位:alter table 表名 add 欄位名 類型;
alter table students add birthday datetime;
5.2.修改欄位類型: alter table 表明 modify 欄位名 新類型及約束;
alter table students modify birthday date not null;
5.3.欄位重新命名: alter table 表名 change 舊名 新名 類型及約束;
alter table students change birthday birth date not null;
6.刪除表: droptable 表名;
資料操作
1.基本查詢
1.1.查詢表中所有資料: select * from 表名;
select * from students;
1.2.查詢指定列的資料: select column1, column2,... from 表名;
select name, height from students;
2.添加資料
2.1.全列插入:insert into 表名 values(...)[,(...)...];
insert into students values(0,"悟空",40,178.50,"男","1980-10-01");
insert into students values(0,"悟飯",20,180.00,"男","2000-10-2"), (0,"比克",default,190,default,"1950-1-1");
2.2.部分插入:insert into 表名 (列1,列2,...) values(值1,值2,...)[,(值1,值2,...)];
insert into students (name,age,birth) values("貝吉塔",60,"1970-1-1");
insert into students (name,birth) values("特蘭克斯","2008-12-1"),("悟天","2009-12-2");
3.修改資料
update 表名 set 列1=值1,列2=值2...where 條件;
update students set age=12 where name="特蘭克斯";
4.刪除資料
4.1.物理刪除
delete from 表名 where 條件;
delete from students where name="悟天";
4.2.邏輯刪除(設定isdelete欄位,類型為bit,預設值為0,需要刪除資料時修改isdelete=1)
(alter table students add isdelete bit default 0 not null;)
update students set isdelete=1 where 條件;
Database Backup
1.備份:運行mysqldump命令
mysqldump -u root -p 資料庫名 > python.sql(儲存到*.sql檔案)
# 按提示輸入密碼
2.恢複
2.1.串連mysql伺服器,建立新的資料庫,退出登入
2.2.mysql -u root -p 新資料庫名 < python.sql
# 按提示輸入密碼
資料完整性
一個資料庫就是一個完整的業務單元,可以包含多張表,資料被儲存在表中。為了在表中更加準確的儲存資料,保證資料的完整有效,
可以在建立表的時候為表添加一些強制性的驗證,包括資料欄位的類型和約束。
1.資料類型
使用資料類型的原則是:夠用就行,盡量使用取值範圍小的,而不用大的,這樣可以更多的節省儲存空間。
常用資料類型如下:
整數:int,bit
小數:decimal
字串:varchar,char
日期時間: date, time, datetime
枚舉類型(enum)
特別說明的類型如下:
decimal 表示浮點數,如decimal(5,2)表示共存5位元,小數佔2位
char 表示固定長度的字串,如char(3),如果填充‘ab‘時會補一個空格為‘ab ‘
varchar 表示可變長度的字串,如varchar(3),填充‘ab‘時就會儲存‘ab‘
字串 text 表示儲存大文本,當字元大於4000時推薦使用
對於圖片、音頻、視頻等檔案,不儲存在資料庫中,而是上傳到某個伺服器上,然後在表中儲存這個檔案的儲存路徑
2.約束
主鍵 primary key:物理上儲存的順序
非空 not null:此欄位不允許填寫空值
惟一 unique :此欄位的值不允許重複
預設 default:當不填寫此值時會使用預設值,如果填寫時以填寫為準
外鍵 foreign key:對關係欄位進行約束,當為關係欄位填寫值時,會到關聯的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗並拋出異常
說明:雖然外鍵約束可以保證資料的有效性,但是在進行資料的crud(增加、修改、刪除、查詢)時,
都會降低資料庫的效能,所以不推薦使用,那麼資料的有效性怎麼保證呢?答:可以在邏輯層進行控制
mysql安裝與基本使用