日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL命令学习

發(fā)布時(shí)間:2023/11/29 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL命令学习 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上面兩篇博客講了MySQL的安裝、登錄,密碼重置,為接下來的MySQL命令學(xué)習(xí)做好了準(zhǔn)備,現(xiàn)在開啟MySQL命令學(xué)習(xí)之旅吧。

首先打開CMD,輸入命令:mysql -u root -p? 登錄MySQL。

注意:MySQL命令終止符為分號 (;)?

1.show databases;顯示當(dāng)前用戶的數(shù)據(jù)庫

2.use 數(shù)據(jù)庫名;? ?選擇數(shù)據(jù)庫

3.create database 數(shù)據(jù)庫名; 創(chuàng)建數(shù)據(jù)庫

4. drop database? 數(shù)據(jù)庫名 ;刪除數(shù)據(jù)庫?

5.create table if not exists `表名`()engine= InnoDB default charset=utf8;創(chuàng)建數(shù)據(jù)表?

例:

create table if not exists `test_user`(
`test_id` int unsigned auto_increment,
`test_title` varchar(100) not null,
?`test_name` varchar(100) not null,
`test_createDate` date, primary key (`test_id`)
)engine= InnoDB default charset=utf8;

6. drop??table 表名;?刪除數(shù)據(jù)表?

7.show tables; 顯示當(dāng)前數(shù)據(jù)庫的表

8.insert? into?table_name?(?字段1,字段2,...字段N?)?values???(?value1,?value2,...valueN?);為表插入數(shù)據(jù)

9. where是查詢條件語句與select,update,delete一起用的

10.select 查詢命令,select 讀取一條或者多條記錄

? 10.1? select?*?from?表名;? ?查看表里的所有數(shù)據(jù) ,顯示所有字段??

? 10.2? select?column_name,column_name?from?表名?where?條件?limit?n?offset?m ;查詢滿足條件的數(shù)據(jù),只顯示字段column_name,column_name

  • 查詢語句中可以使用一個(gè)或者多個(gè)表,表之間使用逗號(,)分割
  • 用星號(*)來代替其他字段,select語句會(huì)返回表的所有字段數(shù)據(jù)
  • 使用 where語句寫查詢條件。
  • 使用limit 屬性來設(shè)定返回的記錄數(shù)。
  • offset指定select語句查詢數(shù)據(jù)的偏移量。默認(rèn)偏移量為0。
例1:
select test_title from test_user where test_id>2 limit 3 offset 3;
查詢結(jié)果只顯示了字段:test_title
test_id>2的記錄有3,4,5,6,7,8,9,10,11,然后offset為3,所以查詢結(jié)果偏移3后,最后的數(shù)據(jù)為:6,7,8 例2: select test_title from test_user where test_id>7 limit 3 offset 3;
查詢結(jié)果只顯示了字段:test_title test_id>7的記錄有8,9,10,11,然后offset為3,所以查詢結(jié)果偏移3后,最后的數(shù)據(jù)為:11

11.update 表名 set field1=new-value1, field2=new-value2 where 條件; 更新滿足條件的記錄

? ? 例:update test_user set test_createDate="2018-11-02" where test_id<5;

12.delete from 表名 where 條件; 刪除表里滿足條件的記錄?
?
?? ?delete from test_user where id>6 && id<8; 刪除第7條數(shù)據(jù)

13.like,like與where聯(lián)合使用,表示包含的情況,其中l(wèi)ike語句的%表示任意字符,如果like語句中沒有用%,那么它就與等號沒有差別

like "%my":表示以my結(jié)尾 like "%my%":表示中間或開始有my like "my%":表示以my開始
like "my" :表示等于my

14.order by 字段??ASC |DESC ;這個(gè)命令一般跟select聯(lián)合使用, 根據(jù)什么字段排序,默認(rèn)情況下是升序

? ? ? ? asc:升序

? ? ? ? desc:降序? ??

? ? ? ? 按拼音來排序:

? ? ? 14.1 如果字符集采用的是 gbk(漢字編碼字符集),直接在查詢語句后邊添加 order by:

? ? ? ? ? ? ?select * from 表名 order by 字段??ASC ;

? ? ? 14.2 如果字符集采用的是 utf8(萬國碼),需要先對字段進(jìn)行轉(zhuǎn)碼然后排序:

select * from 表名 order by convert(runoob_title using gbk);?

15.?show columns from?表名;?查看表的字段定義格式

?例:show columns from test_user;

16.alter?

? ? 16.1 alter table?表名?add?新字段?數(shù)據(jù)類型;為數(shù)據(jù)表添加新的字段(為已經(jīng)建好的表增加一列)

? ? ??? ?例:

? ? ? ? ? ? ??alter table?test_user?add?score int; 為表test_user,添加類型為int的新字段score;

? ? ? ? ?? ? ?alter table?test_user?add?score int first;為表test_user,添加類型為int的新字段score,并且放在第一位

? ? ? ? ? ? ?alter table?test_user?add?score int? after? name;? ? ??添加類型為int的新字段score,并且放在字段name的后面

?

? ?16.2 alter table?表名?drop?字段;刪除數(shù)據(jù)表的字段(為已經(jīng)建好的表刪除一列)

? 例:alter table test_user drop score;

? 16.3 alter table?表名?modify?字段??新類型;修改已有字段的類型

? ? ?例:alter table?test_user?modify?test_title??varchar(200) not null default? "hello";

? ? ? ? ? ? 修改表test_user的字段test_title?的類型為varchar(200) ,并且不能為null,默認(rèn)值為hello

? 16.4?alter table?表名?change 字段??新字段 新字段數(shù)據(jù)類型;更改舊字段的名稱及類型

例:alter table test_user change score middle_score int;

? 16.5?alter table 表名 engine=myisam;修改存儲(chǔ)引擎為myisam

? 16.6?alter table 表名 drop foreign key 鍵名;刪除外鍵約束

? 16.7?alter table 表名 modify 字段1? 數(shù)據(jù) first|after 字段2;

? ? ?修改字段的相對位置,字段1為想要修改的字段,類型為該字段原來類型,first和after二選一,first放在第一位,after放在字段2后面

? 16.8?alter table?表名?alter?字段?set default?;為數(shù)據(jù)表字段設(shè)置默認(rèn)值;

例:alter table test_user alter score set default 0;表test_user的score字段 設(shè)置默認(rèn)值0;

? 16.9?alter table?表名?rename to 新的表名;更改數(shù)據(jù)表的名字

?

? 1. show databases; 顯示當(dāng)前用戶的數(shù)據(jù)庫

2.? use 數(shù)據(jù)庫名;? ?選擇數(shù)據(jù)庫

3. create database 數(shù)據(jù)庫名; 創(chuàng)建數(shù)據(jù)庫

?

4. drop database 數(shù)據(jù)庫名 ;刪除數(shù)據(jù)庫

?5.create table if not exists `test_user`()engine= InnoDB default charset=utf8;創(chuàng)建數(shù)據(jù)表

create table if not exists `test_user`(
`test_id` int unsigned auto_increment,
`test_title` varchar(100) not null,
?`test_name` varchar(100) not null,
`test_createDate` date, primary key (`test_id`)
)engine= InnoDB default charset=utf8;

6. drop??table 表名;?刪除數(shù)據(jù)表

7.show tables; 顯示當(dāng)前數(shù)據(jù)庫的表

8.insert? into?table_name ( field1, field2,...fieldN )?values???( value1, value2,...valueN );為表插入數(shù)據(jù)

?

9. where是查詢條件語句與select,update,delete一起用的

10.select 查詢命令,select 讀取一條或者多條記錄

? ?select?*?from?表名;? ?查看表里的所有數(shù)據(jù) ,顯示所有字段??

?select column_name,column_name from 表名 where 條件 limit n offset m ;查詢滿足條件的數(shù)據(jù),只顯示字段column_name,column_name

  • 查詢語句中可以使用一個(gè)或者多個(gè)表,表之間使用逗號(,)分割
  • 用星號(*)來代替其他字段,select語句會(huì)返回表的所有字段數(shù)據(jù)
  • 使用 where語句寫查詢條件。
  • 使用limit 屬性來設(shè)定返回的記錄數(shù)。
  • offset指定select語句查詢數(shù)據(jù)的偏移量。默認(rèn)偏移量為0。
select test_title from test_user where test_id>2 limit 3 offset 3;
查詢結(jié)果只顯示了字段:
test_title
test_id>2的記錄有3,4,5,6,7,8,9,10,11,然后offset為3,所以查詢結(jié)果偏移3后,最后的數(shù)據(jù)為:6,7,8

select test_title from test_user where test_id>7 limit 3 offset 3;
查詢結(jié)果只顯示了字段:test_title
test_id>7的記錄有8,9,10,11,然后offset為3,所以查詢結(jié)果偏移3后,最后的數(shù)據(jù)為:11

11.update
表名 set field1=new-value1, field2=new-value2 where 條件; 更新滿足條件的記錄

?update test_user set test_createDate="2018-11-02" where test_id<5;



12.delete from 表名 where 條件; 刪除表里滿足條件的記錄?

?
delete from test_user where id>6 && id<8; 刪除第7條數(shù)據(jù)

?

13.like,like與where聯(lián)合使用,表示包含的情況,其中l(wèi)ike語句的%表示任意字符,如果like語句中沒有用%,那么它就與等號沒有差別

like "%my":表示以my結(jié)尾
like "%my%":表示中間或開始有my like "my%":表示以my開始
like "my" :表示等于my
查詢結(jié)果如下:

?14.order by 字段??ASC |DESC ;這個(gè)命令一般跟select聯(lián)合使用, 根據(jù)什么字段排序,默認(rèn)情況下是升序

? ? ? ? asc:升序

? ? ? ? desc:降序? ??

? ? ? ? 按拼音來排序:

? ? ? 14.1 如果字符集采用的是 gbk(漢字編碼字符集),直接在查詢語句后邊添加 order by:

? ? ? ? ? ? ?select * from 表名 order by 字段??ASC ;

? ? ? 14.2 如果字符集采用的是 utf8(萬國碼),需要先對字段進(jìn)行轉(zhuǎn)碼然后排序:

select * from 表名 order by convert(runoob_title using gbk);?

? ? ? ? ? ?

?

15. alter table 表名 add 新字段 數(shù)據(jù)類型;為數(shù)據(jù)表添加新的字段(為已經(jīng)建好的表增加一列)

? ? ? ???alter table test_user add score int;

?

16.alter table?表名 drop 字段;刪除數(shù)據(jù)表的字段(為已經(jīng)建好的表刪除一列)

?alter table test_user drop score;

?

?

17. alter table 表名 modify 字段? 新類型;修改已有字段的類型

?alter table test_user modify test_title? varchar(200);

?

18.?show columns from 表名; 查看表的字段定義格式

?show columns from test_user;

?

?

?

總結(jié)

以上是生活随笔為你收集整理的MySQL命令学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。