Mysql 常見命令語句

来源:https://www.cnblogs.com/luym/archive/2018/09/01/9569574.html
-Advertisement-
Play Games

# # cmd命令行連接MySql cd C:\Program Files\MySQL\MySQL Server 5.5\bin # 啟動mysql伺服器net start mysql # 關閉mysql伺服器net stop mysql # 進入mysql命令行 mysql -h localhos ...


#---------------------------
#----cmd命令行連接MySql---------

 

cd  C:\Program Files\MySQL\MySQL Server 5.5\bin

 

# 啟動mysql伺服器
net  start  mysql

 

# 關閉mysql伺服器
net  stop  mysql

 

# 進入mysql命令行 

mysql  -h  localhost  -u  root  -p

 #---------------------------

#----MySql用戶管理---------

#修改密碼:首先在DOS 下進入mysql安裝路徑的bin目錄下,然後鍵入以下命令:

mysqladmin -uroot -p123 password 456;

#增加用戶

#格式:grant 許可權 on 資料庫.* to 用戶名@登錄主機 identified by '密碼'

/*

如,增加一個用戶user1密碼為password1,讓其可以在本機上登錄, 並對所有資料庫有查詢、插入、修改、刪除的許可權。首先用以root用戶連入mysql,然後鍵入以下命令:

grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";

如果希望該用戶能夠在任何機器上登陸mysql,則將localhost改為"%"。

如果你不想user1有密碼,可以再打一個命令將密碼去掉。

grant select,insert,update,delete on mydb.* to user1@localhost identified by "";

*/

grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有許可權

 

#----------------------------

#-----MySql資料庫操作基礎-----

 

# 創建資料庫

create  database   namage  default  character  set  utf8  collate  utf8_general_ci;


# 如果資料庫存在刪除

drop  database  if  exists  manage;

 

# 進入資料庫

use  manage;


# 刪除資料庫

drop  manage;


# 查看表的結構

desc  class;


# 查看表內數據

select  *   from  class;

 


# 創建班級表並添加欄位:
create  table  class(
id  int(10)  not  null  auto_increment,
name  varchar(30)  not  null  default  " noname",
add_time  datetime  no t null,
primary  key(id)
)
ENGINE = INNODB  charset=utf8;

 

# 1、向表內添加2條數據:如果 add_time 欄位為datetime
insert into class(name,add_time) values ("一年級","2018-08-31 15:33");
insert into class(name,add_time) values ("二年級","2018-08-31 15:33");

 

# 2、向表內添加2條數據:如果 add_time 欄位為timestamp

insert into class(name) values ("一年級");

insert into class(name) values ("二年級");

 

# 創建學生表並添加欄位:
CREATE table student(
id  int(10)  not  null  primary  key  auto_increment  unique,    #  unique唯一性,不可重覆
name  varchar(30)  not  null  default  "noname "   comment   "名稱",
age  int(10)  not  null  default  0  comment   "年齡",
birthday  datetime  not  null  comment  "生日",
class_id  int(10) ,
foreign  key(class_id)  references  class(id)
);

# 向表內添加4條數據:
insert into student(name,age,birthday,class_id) values ("盧宇蒙",23,"1996-07-11",1);
insert into student(name,age,birthday,class_id) values ("王志敏",23,"1996-08-12",1);
insert into student(name,age,birthday,class_id) values ("趙廣正",23,"1996-09-13",2);
insert into student(name,age,birthday,class_id) values ("古川",23,"1996-10-14",2);

 

# 創建分數表並添加欄位: decimal(5,2) 5是有效長度,2是小數點後2位
create  table  course(
id  int (10)  not  null  primary  key  auto_increment,
name  varchar(30)  not  null ,
score  DECIMAL(5,2)  not  null,
class_id  int(10)  not  null,
stu_id  int (10)  not  null,
foreign  key(class_id)  references  class(id),
foreign  key (stu_id)  references  student(id)
);

# 向表內添加5條數據:
insert into course(name,score,class_id,stu_id) values ("數學",90.6,1,1);
insert into course(name,score,class_id,stu_id) values ("語文","135.44",1,5);
insert into course(name,score,class_id,stu_id) values ("英語","100",2,3);
insert into course(name,score,class_id,stu_id) values ("政治","98",1,2);
insert into course(name,score,class_id,stu_id) values ("歷史","89.92",2,4);

完成後如圖所示:

 

 

 

# 查找三張表裡所有的數據:
SELECT * FROM student;
SELECT * FROM class;
SELECT * FROM course;

 

# 查詢student表中id=1的name名

select  name  from  student  where  id=1;


# 查詢student表中name=“王志敏”的數據

select * from student where name = "王志敏";


# 查詢student表中年齡大於15的數據

select id,name from student where age>"15";


and且;
# 查詢student表中年齡大於15並且小於30的數據

select * from student where age>"15" and age<"30";


or 或;
# 查詢student表中年齡大於15或小於30的數據

select * from student where age>"15" and age<"30";


between 之間;
# 查詢student表中年齡在15和30之間的數據

select   *  from  student  where  age > "15"  between  age > "30";


in 包含
# 查詢指定集合內的數據

select  *  from  student  where  id  in  (1,3,5);


排序
id升序 :  select  *  from  student  order  by  id  asc;
id降序 :  select  *  from  student  order  by  id  desc;

id 最大值: select  max(id)  from  student;
生日最小值:select   min(birth)  from  student;
id平均值: select  avg(id)  as  '求平均'  from  student;
統計數據:   select  count(*)  from  student
名字統計:   select  count(name)  from  student;(如果為空不統計)
id的和:       select  sum(id)  from  student
查詢第 i 條以後的 j 條數據(不包括第i條):select  *  from  student  limit  2,5; #從第3條開始的5條數據(3-8)

 


# 修改id=2的age為66

update student set age=66 where id=2;


# 修改id=2的name和age

update student set name = "haha", birth = "1999-01-01" where id=2;

 

# 修改表名

alter table student rename to stu;

 

# 修改某個欄位的名字

alter table stu change name names varchar(30);(修改欄位name名為names)


# 修改表的預設值

alter table stu alter text set default 'system';


# 刪除預設值

alter table stu alter text drop default;


# 增加列

alter table stu add (text char(10));


# 刪除主鍵

alter table stu drop primary key;

 

# 刪除表內所有數據但是不刪除表

delete * from student;


# 刪除一列

alter table student drop column birth;


# 添加一列

alter table student add column haha varchar(30);


# 刪除一行

delete from student where id=6;


# 添加一行

insert into student(name,age,birth,class_id) values ("薑奕迪",18,"2000-11-11")

#---------------------------
#----cmd命令行連接MySql---------

 

cd C:\mysql\bin

 

# 啟動mysql伺服器
net start mysql

 

# 關閉mysql伺服器
net stop mysql

 

# 進入mysql命令行
mysql -h localhost -u root -p

#---------------------------
#----MySql用戶管理---------

#修改密碼:首先在DOS 下進入mysql安裝路徑的bin目錄下,然後鍵入以下命令:
mysqladmin -uroot -p123 password 456;

#增加用戶
#格式:grant 許可權 on 資料庫.* to 用戶名@登錄主機 identified by '密碼'
/*
如,增加一個用戶user1密碼為password1,讓其可以在本機上登錄, 並對所有資料庫有查詢、插入、修改、刪除的許可權。首先用以root用戶連入mysql,然後鍵入以下命令:
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";
如果希望該用戶能夠在任何機器上登陸mysql,則將localhost改為"%"。
如果你不想user1有密碼,可以再打一個命令將密碼去掉。
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";
*/
grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有許可權

 

#----------------------------
#-----MySql資料庫操作基礎-----

 

# 創建資料庫
create database namage default character set utf8 collate utf8_general_ci;


# 如果資料庫存在刪除
drop database if exists manage;

 

# 進入資料庫
use manage;


# 刪除資料庫
drop manage;


# 查看表的結構
desc class;


# 查看表內數據
select * from class;

 


# 創建班級表並添加欄位:
create table class(
id int(10) not null auto_increment,
name varchar(30) not null default " noname",
add_time datetime no t null,
primary key(id)
)
ENGINE = INNODB charset=utf8;

 

# 1、向表內添加2條數據:如果 add_time 欄位為datetime
insert into class(name,add_time) values ("一年級","2018-08-31 15:33");
insert into class(name,add_time) values ("二年級","2018-08-31 15:33");

 

# 2、向表內添加2條數據:如果 add_time 欄位為timestamp
insert into class(name) values ("一年級");
insert into class(name) values ("二年級");

 

# 創建學生表並添加欄位:
CREATE table student(
id int(10) not null primary key auto_increment unique, # unique唯一性,不可重覆
name varchar(30) not null default "noname " comment "名稱",
age int(10) not null default 0 comment "年齡",
birthday datetime not null comment "生日",
class_id int(10) ,
foreign key(class_id) references class(id)
);

# 向表內添加4條數據:
insert into student(name,age,birthday,class_id) values ("盧宇蒙",23,"1996-07-11",1);
insert into student(name,age,birthday,class_id) values ("王志敏",23,"1996-08-12",1);
insert into student(name,age,birthday,class_id) values ("趙廣正",23,"1996-09-13",2);
insert into student(name,age,birthday,class_id) values ("古川",23,"1996-10-14",2);

 

# 創建分數表並添加欄位: decimal(5,2) 5是有效長度,2是小數點後2位
create table course(
id int (10) not null primary key auto_increment,
name varchar(30) not null ,
score DECIMAL(5,2) not null,
class_id int(10) not null,
stu_id int (10) not null,
foreign key(class_id) references class(id),
foreign key (stu_id) references student(id)
);

# 向表內添加5條數據:
insert into course(name,score,class_id,stu_id) values ("數學",90.6,1,1);
insert into course(name,score,class_id,stu_id) values ("語文","135.44",1,5);
insert into course(name,score,class_id,stu_id) values ("英語","100",2,3);
insert into course(name,score,class_id,stu_id) values ("政治","98",1,2);
insert into course(name,score,class_id,stu_id) values ("歷史","89.92",2,4);

完成後如圖所示:

class表:

student表:

course表:

 

 

# 查找三張表裡所有的數據:
SELECT * FROM student;
SELECT * FROM class;
SELECT * FROM course;

 

# 查詢student表中id=1的name名
select name from student where id=1;


# 查詢student表中name=“王志敏”的數據
select * from student where name = "王志敏";


# 查詢student表中年齡大於15的數據
select id,name from student where age>"15";


and且;
# 查詢student表中年齡大於15並且小於30的數據
select * from student where age>"15" and age<"30";


or 或;
# 查詢student表中年齡大於15或小於30的數據
select * from student where age>"15" and age<"30";


between 之間;
# 查詢student表中年齡在15和30之間的數據
select * from student where age > "15" between age > "30";


in 包含
# 查詢指定集合內的數據
select * from student where id in (1,3,5);


排序
id升序 :  select * from student order by id asc;
id降序 :  select * from student order by id desc;
id 最大值: select max(id) from student;
生日最小值:select min(birth) from student;
id平均值: select avg(id) as '求平均' from student;
統計數據: select count(*) from student
名字統計: select count(name) from student;(如果為空不統計)
id的和: select sum(id) from student
查詢第 i 條以後的 j 條數據(不包括第i條)
select * from student limit 2,5; #從第3條開始的5條數據(3-8)

 


# 修改id=2的age為66
update student set age=66 where id=2;

# 修改id=2的name和age
update student set name = "haha", birth = "1999-01-01" where id=2;

# 修改表名
alter table student rename to stu;

# 修改某個欄位的名字
alter table stu change name names varchar(30);(修改欄位name名為names);

# 修改表的預設值
alter table stu alter text set default 'system';

# 刪除預設值
alter table stu alter text drop default;

# 增加列
alter table stu add (text char(10));

# 刪除主鍵
alter table stu drop primary key;

# 刪除表內所有數據但是不刪除表
delete * from student;

# 刪除一列
alter table student drop column birth;

# 添加一列
alter table student add column haha varchar(30);

# 刪除一行
delete from student where id=6;  如果被關聯,需要把關聯的數據也刪除

# 添加一行
insert into student(name,age,birth,class_id) values ("薑奕迪",18,"2000-11-11")

 

 

#---------------------------
#----資料庫備份---------

 

# 導出數據

# 進入資料庫存放位置  預設位置是C:\Program Files\MySQL\MySQL Server 5.5\bin  位置更改自行查找

cd C:\Users\memgmeng\Documents\Navicat\MySQL\servers\mysql1602A

 

#  輸入mysqldump -u root -p manager > D:\manager.sql    manager是資料庫名,> 後是備份的位置,然後輸入密碼,成功後如圖:

 

# 導入數據

# 輸入mysql -u root -p 和密碼進入mysql

# 新建一個空的資料庫,如mana

# 進入資料庫:use mana;

# 導入文件:source D:/manager.sql;  D:/與導入的文件之間不能有空格

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 常見的linux指令 1、ls ll 查看文件信息 2、cd 切換工作目錄 cd 或 cd ~ 切換到/home/用戶目錄 cd. 切換到當前目錄 cd.. 切換到上級目錄 cd- 切換入上次所在的目錄 3、clear 或 ctrl + l 清屏 4、pwd 顯示當前路徑 5、mkdir 創建目錄 ...
  • 初學mysql時,可能不太明白delimiter的真正用途,delimiter在mysql很多地方出現,比如存儲過程、觸發器、函數等。 學過oracle的人,再來學mysql就會感到很奇怪,百思不得其解。 其實就是告訴mysql解釋器,該段命令是否已經結束了,mysql是否可以執行了。預設情況下,d ...
  • 當MySQL單表記錄數過大時,增刪改查性能都會急劇下降,可以參考以下步驟來優化:除非單表數據未來會一直不斷上漲,否則不要一開始就考慮拆分,拆分會帶來邏輯、部署、運維的各種複雜度,一般以整型值為主的表在千萬級以下,字元串為主的表在五百萬以下是沒有太大問題的。而事實上很多時候MySQL單表的性能依然有不... ...
  • FOREIGN KEY約束添加規則 1、外鍵約束並不僅僅可以與另一表的主鍵約束相鏈接,它還可以定義為引用另一個表中 UNIQUE 約束的列。 2、如果在 FOREIGN KEY 約束的列中輸入非 NULL 值,則此值必須在被引用列中存在;否則,將返回違反外鍵約束的錯誤信息。 若要確保驗證了組合外鍵約 ...
  • 問題: 解決方案: 在PHP 代碼中 輸入 ; 完美解決: ...
  • 問題:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which ...
  • 一.概述 在前面講過"sql server 備份與恢復系列"都是集中在用戶資料庫上。sql server還維護著一組系統資料庫,這些系統資料庫對於伺服器實例的運行至關重要。在每次進行系統更新後必須備份多個系統資料庫。必須備份的系統資料庫包括:msdb,master,model。如果使用了複製,還要備 ...
  • 最近很多人問小編現在學習大數據這麼多,他們都是如何學習的呢。很多初學者在萌生向大數據方向發展的想法之後,不免產生一些疑問,應該怎樣入門?應該學習哪些技術?學習路線又是什麼?今天小編特意為大家整理了一份大數據從入門到精通的學習路線。並且附帶學習資料和視頻。希望能夠幫助到大家。大數據學習資料分享群:11 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...