溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何執(zhí)行mysql命令行腳本

發(fā)布時間:2021-06-04 16:39:02 來源:億速云 閱讀:1266 作者:Leah 欄目:MySQL數(shù)據(jù)庫

這篇文章給大家介紹如何執(zhí)行mysql命令行腳本,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

命令行連接

在工作中主要使用命令操作方式,要求熟練編寫

打開終端,運行命令

mysql -uroot -p

回車后輸入密碼,當前設(shè)置的密碼為mysql

連接成功后如下圖

如何執(zhí)行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)于如何執(zhí)行mysql命令行腳本就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI