mysql常用操作

來源:互聯網
上載者:User

標籤:

一、什麼是資料庫

資料庫(Database)是按照資料結構來組織、儲存和管理資料的倉庫。

SQL( Structured Query Language)語言的全稱是結構化查詢語言 (SQL)。資料庫管理系統通過SQL語言來管理資料庫中的資料。

SQL語言分為三個部分:資料定義語言 (Data Definition Language)( Data DefinitionLanguage,簡稱為DDL)、資料操作語言( DataManipulation Language,簡稱為DML)和資料控制語言( Data Control Language,簡稱為DCL)。

DDL語句: CREATE、 ALTER、 DROP

DML語句: update、 insert、 delete、 select
DCL語句:是資料庫控制功能,是用來設定或更改資料庫使用者或角色許可權的語句,包括( grant,deny,revoke等)語句。

二、基本資料庫操作

1.建立資料庫及刪除資料庫

 (1)create database  資料庫名;

  例:create database hhf charset=utf8;

 (2)drop database 資料庫名;

  例:drop database hhf;

2.建立表

 (1)建立表  create table 表名 (

    列名1 列類型 [<列的完整性條件約束>],
    列名2 列類型 [<列的完整性條件約束>],
    ... ...);

  例:   

  create table students
  (
  id int(10) not null unique auto_increment primary key,
  name varchar(20) not null,
  sex varchar(4) ,
  age int(10),
  class varchar(20) not null,
  addr varchar(50)
  );  

  

  create table score
  (
  id int(10) not null unique auto_increment primary key,
  stu_id int(10) not null,
  c_name varchar(20),
  grade int(10)
  );

 (2)刪除表資料:delete  表名   刪除表所有資料    truncate 表名  清空表所有資料   truncate students ;

 (3)刪除表  drop table 表名    例:drop table students;

3.資料庫及表的查看

 show databases; 查看系統中資料庫

 show create database 資料庫名;  查看資料庫的相關資訊

 use  databasename;選擇資料庫   例如:use  hhf ;

 describe tablename; 查看錶結構  或者desc  tablename 

 show create table students;  查看student表結構

   flush privileges 重新整理資料庫

4.遠程登入mysql

 mysql  -h  主機地址   -u使用者名稱 -p使用者密碼   例如:mysql -h 192.168.1.119 -u root -p123456    密碼和-p之間不能有空格 

  退出mysql命令:exit

3.插入資料insert語句

  (1)insert中不指定欄位插入    

  insert into students values  (801,‘劉海洋‘,‘男‘,21,‘aa‘,‘北京市海澱區‘);

 (2)insert中指定欄位插入

  insert into students (name,sex,age,class) values  (801,‘劉海洋‘,‘男‘,21,‘aa‘);

 (3)inser into 表名1 (屬性列表1)  select  屬性列表2 from表名2 where 條件運算式;

   insert into new_student select * from students;

 (4)replace插入新記錄  

  replace語句的文法格式有三種文法格式。

  文法格式1: replace into 表名 [(欄位列表) ] values (值列表)

  文法格式2: replace [into] 目標表名[(欄位列表1)]  select (欄位列表2) from 源表 where 條件運算式  

  文法格式3:replace [into] 表名set 欄位1=值1, 欄位2=值2

  例如:replace  into students values  (801,‘劉海洋‘,‘男‘,21,‘aa‘,‘北京市海澱區‘);

3.修改資料update語句

  update 表名

  set 屬性名稱1=取值1, 屬性名稱2=取值2,…,屬性名稱n=取值n

  where 條件運算式;

  例:update students  set name=‘花花‘  where  age=18;

4.刪除操作delete語句

  delete from 表名    where  條件運算式;

  例:delete from students  where age =18;

5.查詢操作select語句  

  select
  {*|<欄位列表>}
  [
  from <表1>,<表2>...
  [where <運算式>]
  [group by 欄位名]
  [having 運算式]
  [order by 欄位名]
  [limit [<offset>,]<row count>]
  ]
  select [欄位1,欄位2,...,欄位n]
  from [表或視圖]
  where [查詢條件]

(1)條件查詢   條件判斷符=,<>,!=,<,<=,>,>=,between...and(位於兩者之間)     is null、  is not null    in not in  

  select * from students  where age between 18 and 25;#查詢年齡在18-25歲之間的學生資訊

  select * from students  where  addr is null or addr=‘‘;#查詢地址為null或者為‘‘的學生資訊

      select * from students  where  id in(801,802)#查詢id在801、802的學生資訊

  帶like的字元匹配查詢  1)百分比符號萬用字元‘ %‘,匹配任意長度的字元,甚至包括零字元  2)底線萬用字元‘_‘,一次只能匹配任意一個字元

  select * from students where name like ‘李_‘; #查詢姓李名字為2個字的學生資訊
  select * from students where name like ‘張%;#查詢姓張的學生的資訊
  select * from students where name like ‘%三%‘;#查詢名字中含有‘三‘的學生資訊 

(2)分組

  select * from students group by class;#以calss分組

  彙總函式:sum(),count(),avg(),max(),min()

  select avg(grade) from score; #查詢score表平均分數  select sum(grade) from score;#查詢score表分數總和  select min(grade) from score;#查詢score表中分數最低的記錄  select max(grade) from score;#查詢score表中分數最高的記錄  select count(*) from score;#查詢score表中記錄行數

(3)排序

  select * from score  order by stu_id desc ;#按照stu_id降序排序

(4)多表查詢 

  多表查詢是指從多張表中查詢所需要的資料,一般查詢的這幾張表都有一個相同的欄位關聯這幾張表。多表串連可以通過join關鍵字來串連,也可以直接用關聯表中相同的id來進行關聯;

  join:left join:左串連, 串連兩張表,以左邊表的資料匹配右邊表中的資料,如果左邊表中的資料在右邊表中沒有,會顯示左邊表中的資料。

  right join:右串連,串連兩張表,以右邊表的資料匹配左邊表中的資料,如果左邊表中的資料在左邊邊表中沒有,會顯示右邊表中的資料。     

      inner join:內串連,串連兩張表,匹配兩張表中的資料,和前面兩個不同的是只會顯示匹配的資料。

  select a.name 學生姓名,b.score 學產生績 from students a left join score b on   a.id=b.student_id;     

      select a.name 學生姓名,b.score 學產生績 from students a right join score b on   a.id=b.student_id;

  select a.name 學生姓名,b.score 學產生績 from students a inner join score b on  a.id=b.student_id;  

    select a.name 學生姓名,b.score 學產生績 from students a,score b wherea.id=b.student_id;

  串連查詢:  內串連:select  表名.列名,……             from 表1 inner join 表2   on 條件   外串連:left join(左串連)  select  表名.列名,…表2.列名……  from 表1 left  (outer )join  表2   on 條件         right join(右串連)  select  表名.列名,…表2.列名……  from 表1 right  (outer )join  表2   on 條件  

(5)子查詢  

  比如說要把成績低於60分的學生的名字都改成笨蛋     

  update students set name = ‘笨蛋‘ where id in (select a.student_id from score a where a.score<60);

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.