數(shù)據(jù)庫操作
創(chuàng)建數(shù)據(jù)庫
create database ceshi_01 charset=utf8;
進入指定數(shù)據(jù)庫
use ceshi_01;
刪除數(shù)據(jù)庫
drop database ceshi_02;
查看數(shù)據(jù)庫
show database ceshi_01;
select database();
表操作
查看數(shù)據(jù)庫中的所有表
show tables;
創(chuàng)建表
auto_increment表示自動增長
create table 表明(列及類型)幔欧;
例子 create table list(id int auto_incremant primary key, name varchar(10) not null);
修改表
alter table 表名 add | change | drop 列名 類型揪惦;
例子 alter table list add birthday datetime;
刪除表
drop table 表名;
查看表結構
desc 表名满俗;
更改表名稱
rename table 原表名 to 新表名菩鲜;
查看表的創(chuàng)建語句
show create table “表名”;
數(shù)據(jù)操作
查詢
select * from 表名
增加?
全列插入:insert into 表名 values()
缺省插入:insert into 表名(列1胳螟,……) values(值1昔馋,……)
同時插入多條數(shù)據(jù):insert into 表名 values(……)(……)…;
主鍵列是自動增長糖耸,但是在全列插入時需要占位秘遏,通常使用0,插入成功后以實際數(shù)據(jù)為準
修改
update 表名 set 列1=值1嘉竟,…where 條件
刪除?
delete from 表名 where 條件
邏輯刪除邦危,本質就是修改操作update
alter table list add isdelete bit default 0;
如果需要刪除則
update list isdelete=1 where …周拐;
備份與恢復
數(shù)據(jù)備份
進入超級管理員
sudo -s?
進入MySQL庫目錄
cd /var/lib/mysql
運行mysqldump命令
MySQL dump -uroot -p 數(shù)據(jù)庫名稱 > ~/Desktop/備份文件.sql;
按提示輸入MySQL的密碼
數(shù)據(jù)恢復
連接MySQL铡俐,創(chuàng)建數(shù)據(jù)庫
退出連接,執(zhí)行如下命令
MySQL -uroot -p 數(shù)據(jù)庫名? < ~/Desktop/備份文件.sql
根據(jù)提示輸入MySQL密碼