<li id="8g3ty"><tbody id="8g3ty"><th id="8g3ty"></th></tbody></li>
    <label id="8g3ty"><samp id="8g3ty"></samp></label>
  • <span id="8g3ty"></span>

    1. <center id="8g3ty"><optgroup id="8g3ty"></optgroup></center>
    2. <bdo id="8g3ty"><meter id="8g3ty"><bdo id="8g3ty"></bdo></meter></bdo><center id="8g3ty"><optgroup id="8g3ty"></optgroup></center>
      <label id="8g3ty"><samp id="8g3ty"></samp></label>

    3. 電子開發網

      電子開發網電子設計 | 電子開發網Rss 2.0 會員中心 會員注冊
      搜索: 您現在的位置: 電子開發網 >> 編程學習 >> 數據庫語言 >> 正文

      MySQL數據庫常用命令匯總(全網最全)_mysql命令行操作大全

      作者:佚名    文章來源:本站原創    點擊數:    更新時間:2023/11/14

      目錄

      數據庫常用命令

      數據庫的創建

      數據表的操作

      表數據的增刪查改

      分組與函數查詢

      運算符:數學運算符

      連接查詢

      多表查詢

      修改語句

      刪除語句

      字符查詢like

      MySQL練習


      數據庫常用命令

      進入數據庫,在win系統下,打開cmd,切換用戶權限,進入root。

      沒權限,用root登錄:mysql -uroot
      如果root有密碼:mysql -uroot -p

      數據庫的創建

      查詢所有數據庫:show databases;

      創建數據庫:create database <數據庫名>;

      刪除數據庫:drop database <數據庫名>;

      進入數據庫:use <數據庫名>;

      數據表的操作

      1)查詢數據庫下表:show tables;

      2)創建表:create table student(id int(4) primary key,name char(20));

      注釋: id為表的第一列;

      int數字類型;

      primary key主鍵的意思,列不能重復。

      Name為表的第二列名字。

      char:類型;

      創建表:create table score(id int(4) not null,class int(2));

      注釋: not null字段不能為空。

      創建表:create table student1(id int(4) not null,name char(20));

      Field (列名),Type(字段類型),null(是否為空),key(主鍵)

      3)查看表結構:describe student; desc student;

      4)修改表名:alter table <表名> rename <表名>;

      5)刪除表:drop table <表名>;

      6)修改表字段信息:alter table student change id id int(20);

      7)增加表字段信息:alter table student1 add class int(4) not null after id;

      8)刪除一個表字段:alter table student1 drop number;

      表數據的增刪查改

      提示:在數據庫導入表時,要修改列的字段類型并設置主鍵;

      主鍵:表中經常有一個列或多列的組合,其值能唯一地標識表中的每一行。這樣的一列或多列稱為表的主鍵,通過它可強制表的實體完整性。當創建或更改表時可通過定義 PRIMARY KEY 約束來創建主鍵。一個表只能有一個 PRIMARY KEY 約束,而且 PRIMARY KEY 約束中的列不能接受空值。由于 PRIMARY KEY 約束確保唯一數據,所以經常用來定義標識列

      1. 表數據新增格式:insert into 表格名(列名) values(值)

      先導入student和score表,表為Excel,可以自己編寫。

      例子:

      mysql> insert into student(id,class,number,name) values(81,4,19,'stu81');

      mysql> insert into student(id,class,number) values(82,4,20);

      mysql> insert into student values(83,4,21,'stu83');

      mysql> alter table student change id id int(2) auto_increment;

      注釋:auto_increment以1為單位自增長的意思;

      mysql> insert into student(class,number,name) values(4,22,'stu84');

      mysql> alter table score change id id int(4) auto_increment;

      注釋:auto_increment自增長的意思。+1。輸入該命令,表格會在新輸入自動新增長新的一行,id也會成自增。

      mysql> insert into score(class,number,maths,chinese,english) values(4,19,80,78,98);

      mysql> insert into score(class,number,maths,chinese,english) values(4,20,98,88,68);

      mysql> insert into score(class,number,maths,chinese,english) values(4,21,91,83,78);

      mysql> insert into score(class,number,maths,chinese,english) values(4,22,67,83,88);

      1. 查詢表數據格式:select * from <表名> where

      注釋:語句以逗號做分隔,*通配符,select是展示的意思,where是條件;

      例子: 查詢學生信息表中所有信息:select * from student;

      查詢成績表中,列id,class,chinese的信息:select id,class,chinese from score;

      3)表數據排序操作:升序:order by 降序:升序語句末尾加 desc

      例子:查詢成績表中,列id,chinese的信息并且以列chinese排序

      select id,chinese from score order by chinese;(升序)

      select id,chinese from score order by chinese desc;(降序)

      4)表數據查詢操作:

      (1)查詢1班與2班的成績信息:mysql> select * from score where class=1 or class=2;

      (2)查詢語文為77并且數學為88的成績信息:

      mysql> select * from score where chinese=77 and maths=88;

      (3)查詢1,2,3班的成績信息:mysql> select * from score where class in (1,2,3);

      查詢不為4班的成績信息: mysql> select * from score where class not in (4);

      (4)查詢不為4班的成績信息: mysql> select * from score where class !=4;

      注釋: !在數據庫里面為否定的意思:

      (5) 查詢1班到3班的成績信息: mysql> select * from score where class between 1 and 3;

      注釋: between:在```之間,中間的意思:

      (6) 查詢不為3班與4班的成績信息:mysql> select * from score where class not in (3,4);

      (7)查詢語文成績大于等于80小于等于90的成績信息

      mysql>select * from score where chinese>=80 and chinese<=90;

      (8) 統計成績表的總數:mysql> select count(*) from score;

      (9) 按照英語去重,顯示英語成績信息:mysql> select distinct English from score;

      注釋: distinct 去除重復的意思;

      (10) 顯示4到7行的數據:mysql> select * from score limit 3,4;

      注釋:數據庫數據排列:0,1,2,3; 3顯示第4行; 4,5,6,7共有4行; 3,4 ;

      3表示第4行,4表示從第3行開始到第7行,共有4行;

      (11) 按chinese排序,顯示4,5行數據: mysql> select * from score order by chinese limit 3,2;

      (12) 查詢出學生姓名為stu10的學生信息:mysql> select * from student where name='stu10';

      注釋:只要不是數字,有漢字數字字母多種組成的形式都要加單引號,表示字符串。

      (13) 查詢出學生姓名為stu10或者stu15的學生信息:

      mysql> select * from student where name in ('stu10','stu15');

      (14) 分組查詢每個班的人數:mysql> select class,count(*) from student group by class;

      作業:

      1,查詢4班的成績信息:select * from score where class=4;

      ;

      2,查詢4班,語文成績大于80小于90的成績信息:

      select * from score where class in (4) and chinese>80 and chinese<90;

      3,查詢學生表中5到10行的數據:select * from student limit 4,6;

      4,顯示3班語文成績為90,數學成績為68,的class與number信息,:

      select class, number from score where class=3 and chinese=90 and maths=68;

      5,查詢出4班成績并且按語文成績倒序排序:

      select * from score where class=4 order by chinese desc;

      >

      6,查詢2班與3班,語文成績與數學成績都大于80的class與number信息:

      select class, number from score where class in (2,3) and chinese>80 and maths>88;

      7,查詢學生名不為stu18,stu22,stu35,stu46,stu54,stu72班級與學號信息

      分組與函數查詢

      溫馨提示:分組之后查詢其他函數結果是不正確的;

      分組函數:group by

      按班級分組,查詢出每班數學最高分:select class,max(maths) from score group by class;

      不分班級查詢總人數最高分: select max(maths) from score;

      注釋: max:最大值;

      按班級分組,查詢出每班數學最低分:select class,min(maths) from score group by class;

      注釋:最小值min;

      按班級分組,查詢出每班數學總分:select class,sum(maths) from score group by class;

      注釋:sum:總分;

      按班級分組,查詢出每班數學平均分:select class,avg(maths) from score group by class;

      注釋:avg:平均值:

      按班級分組,查詢出每班學生總數:select class,count(*) from score group by class;

      注釋:count:有價值的;

      語句執行順序: from先執行,后執行where, 再接著執行having,limit等。

      例句:

      select class,max(maths) from score where group by(分組) class having(所有) order by(排序) limit

      from后面可以加茲查詢,語句先執行后面再執行前面

      運算符:數學運算符

      mysql> select class,number,maths,maths+5 from score;

      mysql>select class,number,chinese+maths+english from score;

      mysql> select *,maths+chinese+english as total from score;

      mysql> select *,maths+chinese+english as total from score order by total desc;

      mysql> select class*2,number,maths+chinese+english as total from score order by total desc;

      連接查詢

      左連接查詢:

      mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu left join score sc on stu.id=sc.id;

      注釋:stu:為別名。student stu left join score:student:為主表,score為副表顯示。 left join:為左連接。 兩表關聯:其ID必須一一對應(stu.id=sc.id);

      右連接查詢:

      mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu right join score sc on stu.id=sc.id;

      內連接查詢:兩個表同時都有的內容才會顯示。

      mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu join score sc on stu.id=sc.id;

      顯示查詢數據連接:把后表與前排合起來在一個表顯示。

      select id,name,class from student union select class,number,maths from score;

      多表查詢

      select name,student.class,student.number,maths,chinese,english from student,score where student.id=score.id;

      題目練習

      顯示總分大于200的學生信息:

      select stu.name,sc.maths,sc.chinese,sc.english,sc.maths+sc.chinese+sc.english from student stu,score sc where stu.id=sc.id and sc.maths+sc.english+sc.chinese>200;

      顯示班級總數大于等于20的班級:

      select class,count(*) as total from student group by class having total>=20;

      顯示人總數大于等于20的班級的成績信息:

      mysql> select sc.class,sc.number,sc.maths from score sc,(select class,count(*) as total from student group by class having total>=20) s where sc.class=s.class;

      注釋:commit:保存提交的意思,一般文件刪除修改都要做保存;

      Rollback:撤回的意思,命令執行后;可以撤回為修改刪除前的數據;

      truncate table score:永久刪除的意思,盡量少用,刪除則無記錄找回;

      select now():查詢現在的時間;

      修改語句

      update 表名 set where 條件

      mysql> update student set birth=1988,department='中文系' where id=901 and name='張老大';

      把張老大的出生日期修改為1988,院系修改成中文系

      mysql> update student set birth=birth-5;

      把所有學生的年紀增加5歲;

      刪除語句

      mysql> delete from student where id=901;

      刪除901同學的,學生信息

      mysql> delete from student where address like "湖南%";

      刪除湖南籍學生的信息

      mysql> delete from student;

      清空學生表信息

      字符查詢like

      mysql> select * from student where address like '北京%';

      查詢地址為北京的學生信息

      mysql> select * from student where address like '%北京%平%';

      查詢地址為北京市昌平區的學生信息

      mysql> select * from score where stu_id in (select id from student where address like '湖南%');

      查詢湖南籍學生的成績信息;

      作業:

      1,把張三的計算機成績修改成60分

      update score set grade=60 where stu_id in(select id from student where name='張三')and c_name='計算機';

      2,把計算機科目的分數降低5分

      update score set grade=grade-5 where c_name='計算機';

      3,把湖南省學生計算機分數提高5分

      update score set grade=grade+5 where c_name='計算機'and stu_id in(select id from student where address like '湖南%');

      4,把學號為904的學生,計算機成績改為85

      update score set grade=85 where c_name='計算機' and stu_id=904;

      5,刪除904學生的成績

      delete from score where stu_id=904;

      6,刪除湖南籍貫學生的成績

      delete from score where stu_id in(select id from student where address like '湖南%');

      7,刪除王姓與張姓同學英語成績

      delete from score where stu_id in (select id from student where name like '王%'or name like '張%') and c_name='英語';

      8,刪除年紀大于30的學生的計算機成績

      delete from score where stu_id in (select id from student where 2016-birth>30);

      MySQL練習

      創建student和score表

      CREATE TABLE student (id INT(10) NOT NULL PRIMARY KEY ,name VARCHAR(20) NOT NULL ,sex VARCHAR(4) ,birth YEAR,department VARCHAR(20) ,address VARCHAR(50) );

      創建score表,SQL代碼如下:

      CREATE TABLE score (id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT ,

      stu_id INT(10) NOT NULL ,c_name VARCHAR(20) ,grade INT(10));

      為student表和score表增加記錄

      向student表插入記錄的INSERT語句如下:

      INSERT INTO student VALUES( 901,'張老大', '男',1984,'計算機系', '北京市海淀區');

      INSERT INTO student VALUES( 902,'張老二', '男',1987,'中文系', '北京市昌平區');

      INSERT INTO student VALUES( 903,'張三', '女',1991,'中文系', '湖南省永州市');

      INSERT INTO student VALUES( 904,'李四', '男',1993,'英語系', '遼寧省阜新市');

      INSERT INTO student VALUES( 905,'王五', '女',1990,'英語系', '福建省廈門市');

      INSERT INTO student VALUES( 906,'王六', '男',1989,'計算機系', '湖南省衡陽市');

      INSERT INTO student VALUES( 907,'老七', '男',1991,'計算機系', '廣東省深圳市');

      INSERT INTO student VALUES( 908,'老八', '女',1990,'英語系', '山東省青島市');

      向score表插入記錄的INSERT語句如下:

      INSERT INTO score VALUES(NULL,901, '計算機',98);

      INSERT INTO score VALUES(NULL,901, '英語', 80);

      INSERT INTO score VALUES(NULL,902, '計算機',65);

      INSERT INTO score VALUES(NULL,902, '中文',88);

      INSERT INTO score VALUES(NULL,903, '中文',95);

      INSERT INTO score VALUES(NULL,904, '計算機',70);

      INSERT INTO score VALUES(NULL,904, '英語',92);

      INSERT INTO score VALUES(NULL,905, '英語',94);

      INSERT INTO score VALUES(NULL,906, '計算機',90);

      INSERT INTO score VALUES(NULL,906, '英語',85);

      INSERT INTO score VALUES(NULL,907, '計算機',98);

      1.查詢student表的第2條到4條記錄

      select * from student limit 1,3;

      2.從student表查詢所有學生的學號(id)、姓名(name)和院系(department)的信息

      mysql> select id,name,department from student;

      3.從student表中查詢計算機系和英語系的學生的信息

      select * from student where department in ('計算機系' ,'英語系');

      4.從student表中查詢年齡23~26歲的學生信息

      select * from student where birth between 1990 and 1993; 2016-23=1993 2016-26=1990

      select id,name,sex,2016-birth as age,department,address from student where 2016-birth;

      5.從student表中查詢每個院系有多少人

      select department,count(id) from student group by department;

      6.從score表中查詢每個科目的最高分。

      select c_name,max(grade) from score group by c_name;

      7.查詢李四的考試科目(c_name)和考試成績(grade)

      select c_name,grade from score,student where score. stu_id=student.id and name='李四';

      select c_name,grade from score where stu_id=(select id from student where name='李四');

      8.用連接的方式查詢所有學生的信息和考試信息

      select stu.*,sc.* from student stu left join score sc on stu.id=sc.id;

      9.計算每個學生的總成績

      select stu_id,sum(grade) from score group by stu_id;

      10.計算每個考試科目的平均成績

      select c_name,avg(grade) from score group by c_name;

      11.查詢計算機成績低于95分的學生信息

      select student.*, grade from score,student where student.id=score.stu_id and c_name like '計算機' and grade<95;

      12.查詢同時參加計算機和英語考試的學生的信息

      select student.*,c_name from student,score where student.id=score.stu_id and student.

      id =any( select stu_id from score where stu_id in (select stu_id from score where c_name= '計算機') and c_name= '英語' );

      select * from student where id in(select stu_id from score where stu_id in (select stu_id from

      score where c_name='計算機' )and c_name='英語');

      select student.* from student,(select stu_id from score where stu_id in (select stu_id from score where c_name='計算機' )and c_name='英語') t1 where student.id=t1.stu_id;

      select * from student where id in (select stu_id from score sc where sc.c_name='計算機') and id in (select stu_id from score sc where sc.c_name='英語');

      13.將計算機考試成績按從高到低進行排序

      select c_name,grade from score where c_name='計算機' order by grade;

      14.從student表和score表中查詢出學生的學號,然后合并查詢結果

      select id from student union select id from score;

      15.查詢姓張或者姓王的同學的姓名、院系和考試科目及成績

      select name,department,c_name,grade from score sc,student st where st.id=sc.stu_id and (name like'張%'or name like '王%');

      16.查詢都是湖南的學生的姓名、年齡、院系和考試科目及成績中文系

      select name,2016-birth age,department,address,c_name,grade from student,score where student.id=score.stu_id and address like'湖南%';

      17.查詢每個科目的最高分的學生信息.

      分解: score=t1, t2=select c_name,max(grade) as grade from score group by c_name, t1.stu_id注解

      分解: select * from student where id in (select t1.stu_id from score t1,t2 t2 where t1.c_name=t2.c_name and t1.grade=t2.grade);

      select * from student where id in (select t1.stu_id from score t1,(select c_name,max(grade) as grade from score group by c_name) t2 where t1.c_name=t2.c_name and t1.grade=t2.grade);

      select student.* from student,(select score.* from score,(select max(grade) grade,c_name from score group by c_name) t1 where score.c_name=t1.c_name and score.grade=t1.grade) t2 where student.id=t2.stu_id;

      Tags:mysql,數據庫,sql,命令  
      責任編輯:admin
    4. 上一篇文章:
    5. 下一篇文章: 沒有了
    6. 請文明參與討論,禁止漫罵攻擊。 昵稱:注冊  登錄
      [ 查看全部 ] 網友評論
      推薦文章
      • 此欄目下沒有推薦文章
      熱門文章
      • 此欄目下沒有熱點文章
      關于我們 - 聯系我們 - 廣告服務 - 友情鏈接 - 網站地圖 - 版權聲明 - 在線幫助 - 文章列表
      返回頂部
      刷新頁面
      下到頁底
      晶體管查詢
      主站蜘蛛池模板: 九九综合九九综合| 亚洲国产成人久久综合一区| 狠狠狠色丁香婷婷综合久久五月 | 亚洲五月综合缴情在线观看| 狠狠久久综合伊人不卡| 色欲综合久久中文字幕网| 久久综合丝袜长腿丝袜| 久久天天日天天操综合伊人av| 亚洲国产国产综合一区首页| 亚洲综合精品伊人久久| 国产成人99久久亚洲综合精品| 久久综合噜噜激激的五月天| 色综合久久久久久久| 69国产成人综合久久精品91| 亚洲七久久之综合七久久 | 亚洲国产成人久久综合一| 成人综合久久综合| 色噜噜综合亚洲av中文无码| 狠狠色丁香婷婷综合| 日韩亚洲人成在线综合| 亚洲综合久久久久久中文字幕| 狠狠色狠狠色综合久久| 一本色道久久鬼综合88| 色狠台湾色综合网站| 色天使亚洲综合在线观看| 99久久国产综合精品swag| 国产成人精品综合久久久久 | 亚洲VA综合VA国产产VA中| 亚洲人成网站999久久久综合| 精品综合久久久久久888蜜芽| 久久综合亚洲色HEZYO国产| 天天综合网网欲色| 亚洲国产综合精品一区在线播放 | 亚洲国产精品综合久久网络 | 狠狠色狠狠色综合曰曰| 伊人久久大香线蕉综合电影网| 亚洲国产日韩成人综合天堂| 一本大道久久a久久综合| 一本色道久久综合网| 国产精品天干天干综合网| 色综合久久久久久久久五月|