1. 程式人生 > >Mysql淺析-基礎命令(一)

Mysql淺析-基礎命令(一)

dump 邏輯與 mysql基本 系統 https 使用 日期和時間 clu 常用

技術分享圖片

主要從以上篇幅來介紹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淺析-基礎命令(一)