一、數據維護
1、備份數據
# mysqldump命令是在dos或bash窗口運行的命令
# mysqldump -u 用戶名 -p 密碼 備份數據的數據庫名>要存放到指定位置的絕對路徑
mysqldump -u root -p 123456 test_db>/usr/local/test_db.sql
2、導入數據
# 導入數據不包括創建數據庫,需要先創建好數據庫
# mysql -u 用戶名 -p 密碼 要導入數據的數據庫名<存放數據文件的絕對路徑
mysql -u root -p 123456 test_db</usr/local/test_db.sql
二、常用命令
1、數據庫
# 1.查看已有數據庫
show databases;
# 2.創建數據庫
# create database 數據庫名;
create database test_db;
# 3.刪除數據庫
# drop database 數據庫名;
drop database test_db;
# 4.使用數據庫
# use 數據庫名;
use test_db;
2、表
注意:操作表需要先進入對應的數據庫中,使用 use 數據庫名 進入數據庫。
# 1.查看已有表
show tables;
# 2.創建表 括號裏邊是設置字段,多個字段以英文半角逗號分隔
# create table 表名(字段名 字段類型(長度),...);
create table test_table(id int(16),name varchar(32),age int(16));
# 3.刪除表
# drop table 表名;
drop table test_table;
三、CRUD基本命令
1、新增數據C(Create)
# 新增數據到test_table表中,values中爲每個字段對應的數據
# insert into 表名 values(字段id的值,字段name的值,字段age的值);
insert into test_table values(1,'porty',18);
2、查詢數據R(Retrieve)
# 查詢test_table表裏邊的所有記錄,*號代表所有字段
# select 字段名(多個字段名以英文半角逗號分隔,查詢所有字段可直接使用*號) from 表名;
select * from test_table;
3、更新數據U(Update)
# 更新test_table表的數據
# update 表名 set 要設置的字段='值';
update test_table set name='happy';
4、刪除數據D(Delete)
# 刪除test_table表裏的所有數據
# delete from 表名;
delete from test_table;
四、where條件
說明:where條件可以使操作數據具有選擇性
1、where條件的簡單使用
# 1.查詢語句加上where條件可查出滿足條件的數據
# 例如 查詢出test_table表中名字爲porty的數據
select * from test_table where name='porty';
# 2.更新語句加上where條件可更新滿足條件的數據
# 例如 將test_table表中名字爲porty的數據更新成名字爲happy
update test_table set name='happy' where name='porty';
# 3.刪除語句加上where條件可刪除滿足條件的數據
# 例如 刪除test_table表中名字爲porty的數據
delete from test_table where name='porty';
持續更新中…