MySQL常用操作命令

來源:互聯網
上載者:User

標籤:des   style   http   io   color   ar   使用   sp   strong   

安裝:rpm

wget http://cdn.mysql.com/Downloads/MySQL-5.5/MySQL-server-5.5.27-1.linux2.6.i386.rpm

wget http://cdn.mysql.com/Downloads/MySQL-5.5/MySQL-client-5.5.27-1.linux2.6.i386.rpm

rpm –ivh  MySQL-server-5.5.27-1.linux2.6.i386.rpm

rpm –ivh  MySQL-client-5.5.27-1.linux2.6.i386.rpm

查看運行狀態:

Service mysql status

 

一.串連mysql:

格式: mysql  -h主機地址 -u使用者名稱 p使用者密碼

 

二.修改密碼:

格式:mysqladmin -u使用者名稱 -p舊密碼 password 新密碼

三、常用命令:

1.顯示資料庫:use databases;

2.顯示具體的表: show tables;

3、 顯示資料表的結構:
describe表名;
4、 建庫:
create database 庫名;
5、 建表:
use 庫名;
create table 表名(欄位設定列表);
6、 刪庫和刪表:
drop database 庫名;
drop table 表名;
7、 將表中記錄清空:
delete from 表名;
8、 顯示表中的記錄:
select * from 表名;

9.其它常用的語句:

SELECT USER();

SELECT NOW();

Select  version(),current_date();

Select(20+5*10)/4;

10.資料更新

Insert into tablename(列名…)values(列值);

 

Insert into tables values(‘’,’’,’’);

Update tables setvalues=’’ where…

 

11.查詢結果限制條數【Limit】,類似sql server中的top

Select *from info limit10

 

 

資料類型:

整型:int

字元(串)類型:char(10),  可變字元:varchar(50);

時間類型:date,time,datetime,year;

 

12.mysql資料庫指令碼匯出與還原:

Mysql>mysqldumpbaibu –u root –R>baibu.sql    ---指令碼匯出

Mysql>use baibu;                     ---指令碼還原

Mysql> source /root/baibu.sql         ---資料庫指令碼存放目錄

 

SQL語句:

  1. 確定資料表裡有多少條記錄

Select  count(pubID)from publishers;

  2.去除多餘的Distinct

Select  count(distinct pubID)from publishers;

Selectcount(*)/count(distinct titleID)from info;

  3.對查詢結果進行排序

Select* from authors Order byauthname(desc)

  4.篩選資料記錄:【where,having】

Select* from info where sbbid=’yu2000’

Select* from info where snbid like(%yu);

Select* from info where snbid in(yu2000,yu2020);

  5.多表查詢:

需要使用Join文法來構造:

Inner join內連接

Outer join外連接

內串連(INNER JOIN):最常用使用一個比較操作符(=,<>),將以兩個表中共同的值為基礎來匹配兩個表中的行

SELECT publName,title

FROMpublishers INNERJOIN titles

ON publishers.publID=titles.publID

ORDER BY publName

  1. 合并查詢結果:Union

合并同一個資料表中的兩個SELECT命令的結果:

SELECT * FROMauthors WHERE authName LIKE ‘b%‘

UNION

SELECT * FROMauthors WHERE authName LIKE ‘g%

  2.修改資料

Insert

Update

Delete

執行個體:insert into

1)列出有關資料列的名字(有預設值,允許包含NULL,帶有AUTO_INCREMENT屬性的資料列不用列出)然後寫出被插入的資料:

USEmylibrary;

INSERT INTO titles(title,year)

VALUES(‘MySQL‘,2005);

2)出資料列的名字,則必須為所有的資料列按其順序提供資料值

INSERT INTO titles

VALUES(NULL,‘deleteme‘,‘‘,1,NULL,NULL,NULL,2005,NULL,NULL,NULL,NULL)

執行個體:update…set…where

 

UPDATE titles SET title=‘Linux,6th ed.‘ WHEREtitleID=1

 

執行個體:delete

Deletefrom titles where titleID=8;

Droptable

 

MYSQL服務:

1. 開機自動啟動服務:

  mysqlsystemTrayMonitor

   mysql系統管理員

   windows system Control

2. 命令載入和卸載:

   mysqld -install mysql -defaults-file="c:\mysql\my.ini"

   mysqld -remove mysql

3. 手動啟動或停止:

   Net start mysql

   Net stop mysql

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.