mysql命令行腳本執(zhí)行操作示例
本文實例講述了mysql命令行腳本執(zhí)行操作。分享給大家供大家參考,具體如下:
命令行連接
在工作中主要使用命令操作方式,要求熟練編寫
打開終端,運行命令
mysql -uroot -p
回車后輸入密碼,當前設(shè)置的密碼為mysql
連接成功后如下圖
退出登錄
quit 和 exit
或
ctrl+d
登錄成功后,輸入如下命令查看效果
查看版本:select version();
顯示當前時間:select now();
修改輸入提示符
prompt python> 1 \D 完整日期 \U 使用用戶
數(shù)據(jù)庫
查看所有數(shù)據(jù)庫
show databases;
使用數(shù)據(jù)庫
use 數(shù)據(jù)庫名;
查看當前使用的數(shù)據(jù)庫
select database();
創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名 charset=utf8;
例:
create database python charset=utf8;
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名;
例:
drop database python;
數(shù)據(jù)表
查看當前數(shù)據(jù)庫中所有表
show tables;
創(chuàng)建表
auto_increment表示自動增長
CREATE TABLE table_name( column1 datatype contrai, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY(one or more columns) );
例:創(chuàng)建班級表
create table classes( id int unsigned auto_increment primary key not null, name varchar(10) );
例:創(chuàng)建學(xué)生表
create table students( id int unsigned primary key auto_increment not null, name varchar(20) default '', age tinyint unsigned default 0, height decimal(5,2), gender enum('男','女','人妖','保密'), cls_id int unsigned default 0 )
修改表-添加字段
alter table 表名 add 列名 類型;
例:
alter table students add birthday datetime;
修改表-修改字段:重命名版
alter table 表名 change 原名 新名 類型及約束;
例:
alter table students change birthday birth datetime not null;
修改表-修改字段:不重命名版
alter table 表名 modify 列名 類型及約束;
例:
alter table students modify birth date not null;
修改表-刪除字段
alter table 表名 drop 列名;
例:
alter table students drop birthday;
刪除表
drop table 表名;
例:
drop table students;
查看表的創(chuàng)建語句
show create table 表名;
例:
show create table classes;
增刪改查(curd)
curd的解釋: 代表創(chuàng)建(Create)、更新(Update)、讀?。≧etrieve)和刪除(Delete)
查詢基本使用
查詢所有列
select * from 表名;
例:
select * from classes;
查詢指定列
可以使用as為列或表指定別名
select 列1,列2,... from 表名;
例:
select id,name from classes;
增加
格式:INSERT [INTO] tb_name [(col_name,…)] {VALUES | VALUE} ({expr | DEFAULT},…),(…),…
說明:主鍵列是自動增長,但是在全列插入時需要占位,通常使用0或者 default 或者 null 來占位,插入成功后以實際數(shù)據(jù)為準
全列插入:值的順序與表中字段的順序?qū)?yīng)
insert into 表名 values(...)
例:
insert into students values(0,'郭靖‘,1,'蒙古','2016-1-2');
部分列插入:值的順序與給出的列順序?qū)?yīng)
insert into 表名(列1,...) values(值1,...)
例:
insert into students(name,hometown,birthday) values('黃蓉','桃花島','2016-3-2');
上面的語句一次可以向表中插入一行數(shù)據(jù),還可以一次性插入多行數(shù)據(jù),這樣可以減少與數(shù)據(jù)庫的通信
全列多行插入:值的順序與給出的列順序?qū)?yīng)
insert into 表名 values(...),(...)...;
例:
insert into classes values(0,'python1'),(0,'python2');
insert into 表名(列1,...) values(值1,...),(值1,...)...;
例:
insert into students(name) values('楊康'),('楊過'),('小龍女');
修改
格式: UPDATE tbname SET col1={expr1|DEFAULT} [,col2={expr2|default}]…[where 條件判斷]
update 表名 set 列1=值1,列2=值2... where 條件
例:
update students set gender=0,hometown='北京' where id=5;
刪除
DELETE FROM tbname [where 條件判斷]
delete from 表名 where 條件
例:
delete from students where id=5;
邏輯刪除,本質(zhì)就是修改操作
update students set isdelete=1 where id=1;
備份
運行mysqldump
命令
mysqldump –uroot –p 數(shù)據(jù)庫名 > python.sql; # 按提示輸入mysql的密碼
恢復(fù)
連接mysql,創(chuàng)建新的數(shù)據(jù)庫
退出連接,執(zhí)行如下命令
mysql -uroot –p 新數(shù)據(jù)庫名 < python.sql # 根據(jù)提示輸入mysql密碼
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
相關(guān)文章
如何使用分區(qū)處理MySQL的億級數(shù)據(jù)優(yōu)化
mysql在查詢上千萬級數(shù)據(jù)的時候,通過索引可以解決大部分查詢優(yōu)化問題。但是在處理上億數(shù)據(jù)的時候,應(yīng)該怎么解決,本文就是用分區(qū)來優(yōu)化一下,感興趣的一起來了解一下2021-06-06如何把本地mysql遷移到服務(wù)器數(shù)據(jù)庫
這篇文章主要介紹了如何把本地mysql遷移到服務(wù)器數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11解決Navicat導(dǎo)入數(shù)據(jù)庫數(shù)據(jù)結(jié)構(gòu)sql報錯datetime(0)的問題
這篇文章主要介紹了解決Navicat導(dǎo)入數(shù)據(jù)庫數(shù)據(jù)結(jié)構(gòu)sql報錯datetime(0)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Mysql命令行導(dǎo)出SQL文件和導(dǎo)入文件詳細步驟
這篇文章主要給大家介紹了關(guān)于Mysql命令行導(dǎo)出SQL文件和導(dǎo)入文件的詳細步驟,在MySQL中,導(dǎo)入SQL文件是一個常見的操作,它可以幫助我們快速地恢復(fù)數(shù)據(jù)庫、遷移數(shù)據(jù)或者備份數(shù)據(jù),需要的朋友可以參考下2024-05-05