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