mysql入門

來源:互聯網
上載者:User

標籤:des   style   io   os   ar   使用   sp   資料   on   

一、基本操作

#mysql -h localhost -u root -p;//連結mysql

#mysql -u root -p password 123456;//修改root使用者密碼為123456

 >show databases;//查看資料存在

>create database db1;//建立資料庫db1

>use db1;//進入資料庫db1

>drop database db1;//刪除資料庫db1

>show tables;//查看錶存在

>exit;//退出mysql

>create table person(id int(4),name char(10),age int);//建立表person

>create table p(id int,name char(10))charset=gbk;//建立支援插入中文(gbk/utf8)記錄的表p

>create table p(id int auto_increment primary key,name char(10) not null,age int default 100);//帶列完整性條件約束建立

>desc person;//查看錶結構描述

>insert into person(id,name,age) values(1,‘aaa‘,22);//向表person插入記錄

>insert into person set id=1,name=‘aaa‘,age=22;//另一種插入方式

>select * from person;//查詢所有記錄

>update person set age=20 where name=‘aaa‘;//更新資料記錄

*********where條件**********

1.where後跟一個邏輯運算式;

2.邏輯運算式內部可使用比較符號,如=,<,>,<=,>=,<>等,還有between...and,in等運算子;

3.邏輯運算式之間可以用and,or操作;

***************************

>delete from person where id=1;//刪除記錄

>truncate person;//清空表

>drop table person;//刪除表person

********修飾符*****************

1.主鍵:在寫完所有欄位後,加一個primary key(col1,col2,....)

2.自增:auto_increment

3.非空:not null

4.唯一:unique

5.預設值:default value

*****************************

二、查詢簡單查詢

select *是效率最差的查詢方式。

文法格式:select 列1,列2... from 表名 其他條件

>select name,age from person where id=1;

>select name as 姓名,age as 年齡 from person;//指定別名

統計資訊:count(列名),sum(列名),max(列名),min(列名),avg(列名)

>select now();//目前時間

>select 8*7*9;//計算機

>select distinct id,age from person;//去重

>select id,name from person where id between 3 and 7;//查詢id在3和7之間的記錄

>select name from person where id not in(3,4,5);//查詢id不為3,4,5的記錄

>select name from person where age is not null;//查詢年齡不為空白的記錄

not可以和between,in,is搭配,意味取反。

>select name from person where name is like ‘%A_‘;//查詢名字倒數第二個字元為A的記錄

>select name from person where name regexp ‘大‘;//Regex查詢含大的記錄

>select count(name) from person where group by age;//按年齡分組統計人數

>select count(name) from person where group by age having count(name)>1;//過濾掉人數為1的記錄

>select count(name) from person where group by age having age!=40;//年齡40的不參與分組

  where having
順序 在group之前 在group之後
可用欄位 所有 group,select,外查詢
習慣 常用 與group by連用

 

>select * from person limit 0,3;//取前三條記錄

>select * from person order by name asc,age desc;//查詢結果按姓名升序年齡降序排序

>select * from p1 union select * from p2;//聯合,去重

>select * from p1 union all select * from p2;//聯合,不去重

子查詢

將一個查詢語句嵌套到另一個查詢、插入、修改、刪除語句中去,這個查詢語句就是一個子查詢。

執行個體:

>create table p1(id int,name char(10),age int)charset=utf8;

>create table p2(id int,name char(10),age int)charset=utf8;

>select id,name,age from p1 where id>=(select max(id) from p2);//where型子查詢(單一標量)

>select id,name,age from p1 where id in (select id from p2);//列子查詢

>select * from (select * from p1 where id<=floor(ran()*5)) as p3 where name regexp ‘大‘;//from型子查詢

>select * from p1 where exists(select * from p2 where p1.age=p2.age);//exists型子查詢

串連查詢

串連查詢即多表查詢。

外串連:左串連、右串連、全串連(mysql不支援全串連)

>select * from p1 left join p2 on p1.id=p2.id;//左串連

>select * from p1 right join p2 on p1.id=p2.id;//右串連

交叉串連:表間進行笛卡爾積

>select * from p1,p2;

內串連:內串連每條記錄的串連條件都是相同的

>select * from p1 inner join p2 on p1.id=p2.id;//內串連1

>select * from p1 inner join p2 using(id);//內串連2

自然串連:

>select * from p1 natural join p2;

 

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.