Mysql淺析-基礎命令(一)

來源:互聯網
上載者:User

標籤:大小   生活感悟   ODB   dsd   顯示   modify   mem   tno   drop   

主要從以上篇幅來介紹mysql的一些知識點

一.Mysql簡介

MySQL是一個關係型資料庫管理系統由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下產品。MySQL 是最流行的關係型資料庫管理系統之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關聯式資料庫管理系統) 應用軟體。

二.邏輯架構

三.Mysql基本命令I.庫1. 建立資料庫

文法 :create database 資料庫名

#建立資料庫ab

create database ab;

2. 查看資料庫

#顯示所有的資料庫

show databases;

#以行顯示所有資料庫

show databases \G

3.刪除資料庫

文法 :drop database 資料庫名

刪除資料庫ab

drop database ab;

II.表1. 建立表

文法 :create table 表名 (欄位名,類型,欄位名,類型,欄位名,類型);

create table book(idint(10),namechar(40),ageint);

2.查看錶結構

desclist;

explain food.list;

show columns from food .list;

show columns from food. list like‘%id‘;

#查看錶的建立過程,指定儲存引擎,字元集

show create table list;

3.mysql儲存引擎

mysql的儲存引擎包括:MyISAM、InnoDB、BDB、MEMORY、MERGE、EXAMPLE、NDBCluster、ARCHIVE、CSV、BLACKHOLE、FEDERATED

4. 刪除表

文法:drop table 表名

drop table list;

5.修改表名

文法:altertable 表名 rename 新表名;

altertable list rename lists;

6. 修改表中的欄位類型

文法:altertable 表名 modify 要修改的欄位名 欄位名的新欄位類型

altertable lists modifyid char(40);

7.修改表中欄位名稱和類型

文法:altertable 表名 change 原欄位名 新欄位名 新欄位類型

altertable lists change id ids int(40);

8.表中添加欄位 1.表中添加欄位

文法:altertable 表名 add 欄位名 欄位類型

altertable lists add sum int(50);

2.表第一行添加欄位

文法:alter table 表名 add 欄位名 欄位類型 first

#第一行添加欄位

alter table lists add sum int(50)first;

3.在欄位後添加欄位

文法:alter table 表名 add 欄位名 欄位類型 after su

#欄位su後添加欄位

alter table lists add so char(30)after su;

9.刪除表中欄位

文法:alter table 表名 drop 欄位名

alter table lists drop so;

III.記錄1.欄位中插入記錄

文法:insert into 表名 values(1,’zhangshan‘,2)

#後面記錄指定為空白

insert into lists values(1,2,‘shanshi’,null,null);

#插入多條記錄中間用分號隔開

insert into lists valus (1,2,‘lisi’,null,null),(2,3,‘siji’,1,1);

#指定欄位插入

insert into lists (su,ids)values(1,1);

2.查詢表中記錄

文法:select * from 表名

#*表示所有記錄

select * from lists;

#查詢ids中記錄

select ids from lists;

#查詢ids,su中記錄

select ids,su from lists;

#查看指定資料庫中表內容

select * from food.lists; `

3.刪除表中記錄

文法:delete from表名 where 欄位名=xx

delete from lists where ids=2;

#刪除欄位name記錄為空白的行

delete from lists where name is null;

4.更新記錄

文法:update 表名 set 欄位名1=xx where 欄位名2=xx

update lists set ids=1 where name=null;

#所有都變成2

update lists set ids=2

#同時更新多個欄位用分號隔開

update lists set ids=3,name=‘lisi’ where su=1;

四.SQL基本語句查詢1. 多欄位查詢

文法:select 欄位1,欄位2 from 表名

select ids,name from lists;

2. 去重複查詢

文法:select distinct 欄位1,欄位2 from 表名

select distinct ids,name from lists;

3.使用and和or多條件查詢

文法:select 欄位1,欄位2 from 表名 where 欄位1>3 and 欄位2<5

select ids,name from lists where ids>3 and name <5;

select ids,name from lists where ids>3 or name <5;

#and與or同時存在時,先算and左右兩邊的,邏輯與先執行

select * from lists where ids=3 and(su=1or name =5);

4.mysql區分大小寫查詢

文法:select * from 表名 where binary 欄位1=‘xxx’

binary區分大小寫

select *from lists where binary name=‘LK’

5.排序查詢

文法:select distinct 欄位1,欄位2 from 表名 orderby 欄位名

#預設是升序排列

select distinct ids,su from lists orderby idsasc;

#降序排列

select distinct ids,su from lists orderby idsdesc;

6.查詢引用別名

文法:select * from 舊錶名 新表名

select * from lists s;

文法:select 舊欄位名 as 新欄位名 from 表名

#指定欄位別名

select ids as s from lists;

7.like查詢

文法:select 欄位名1 欄位名2 ... from 表名 where 欄位名1 like ‘%abc‘ or 欄位名2 like ‘%ABC‘

select abc ABC from abc1 where abc like ‘%abc‘ or ABC like ‘%ABC‘

五.常用select查詢

#列印當前的日期和時間

selectnow();

#列印當前的日期

selectcurdate();

#列印當前的時間

selectcurtime()

#列印當前資料庫

selectdatabase();

#列印資料庫版本

selectversion();

#列印目前使用者

selectuser();

六.匯入匯出資料庫1.匯入資料庫方法一

建立資料庫 :mysql -e ‘create database book’ -uroot -p123456

匯入資料庫 :mysql -uroot -p123456 book

方法二

建立資料庫 :mysql -e ‘create database book’ -uroot -p123456

匯入資料庫 :source /root/book.sql ** // 資料庫所在路徑**

2.匯出資料庫

mysqldump -uroot -p123456 資料庫名>資料庫檔案名

mysqldump -uroot -p123456 book>book.sql

#匯出包含建庫語句

mysqldump -uroot -p123456 -B book>book.sql

#匯出所有資料庫

mysqldump -uroot -p123456 -A book>book.sql

#匯出資料庫到檔案

select * from lists outfile ‘/tmp/123.txt‘ ;

七.思考與總結

到此主要介紹,mysql基礎命令,包括庫,表,記錄,sql查詢,資料匯入匯出。mysql在關係型資料庫中算是比較強大的一款資料庫,還有後面的分享將會陸續推出,敬請期待!

我是MIkel Pan,雲端運算愛好者,定期更新生活感悟,心靈進化者就在MIkel Pan,喜歡我就來找我吧!

部落格園地址:http://www.cnblogs.com/plyx/

簡書地址:http://www.cnblogs.com/plyx/

Mysql淺析-基礎命令(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.