<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. 電子開發(fā)網(wǎng)

      電子開發(fā)網(wǎng)電子設(shè)計 | 電子開發(fā)網(wǎng)Rss 2.0 會員中心 會員注冊
      搜索: 您現(xiàn)在的位置: 電子開發(fā)網(wǎng) >> 編程學(xué)習(xí) >> 數(shù)據(jù)庫語言 >> 正文

      MySQL數(shù)據(jù)庫常用命令匯總(全網(wǎng)最全)_mysql命令行操作大全

      作者:佚名    文章來源:本站原創(chuàng)    點擊數(shù):    更新時間:2023/11/14

      目錄

      數(shù)據(jù)庫常用命令

      數(shù)據(jù)庫的創(chuàng)建

      數(shù)據(jù)表的操作

      表數(shù)據(jù)的增刪查改

      分組與函數(shù)查詢

      運算符:數(shù)學(xué)運算符

      連接查詢

      多表查詢

      修改語句

      刪除語句

      字符查詢like

      MySQL練習(xí)


      數(shù)據(jù)庫常用命令

      進入數(shù)據(jù)庫,在win系統(tǒng)下,打開cmd,切換用戶權(quán)限,進入root。

      沒權(quán)限,用root登錄:mysql -uroot
      如果root有密碼:mysql -uroot -p

      數(shù)據(jù)庫的創(chuàng)建

      查詢所有數(shù)據(jù)庫:show databases;

      創(chuàng)建數(shù)據(jù)庫:create database <數(shù)據(jù)庫名>;

      刪除數(shù)據(jù)庫:drop database <數(shù)據(jù)庫名>;

      進入數(shù)據(jù)庫:use <數(shù)據(jù)庫名>;

      數(shù)據(jù)表的操作

      1)查詢數(shù)據(jù)庫下表:show tables;

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

      注釋: id為表的第一列;

      int數(shù)字類型;

      primary key主鍵的意思,列不能重復(fù)。

      Name為表的第二列名字。

      char:類型;

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

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

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

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

      3)查看表結(jié)構(gòu):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;

      表數(shù)據(jù)的增刪查改

      提示:在數(shù)據(jù)庫導(dǎo)入表時,要修改列的字段類型并設(shè)置主鍵;

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

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

      先導(dǎo)入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. 查詢表數(shù)據(jù)格式:select * from <表名> where

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

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

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

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

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

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

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

      4)表數(shù)據(jù)查詢操作:

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

      (2)查詢語文為77并且數(shù)學(xué)為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;

      注釋: !在數(shù)據(jù)庫里面為否定的意思:

      (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) 統(tǒng)計成績表的總數(shù):mysql> select count(*) from score;

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

      注釋: distinct 去除重復(fù)的意思;

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

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

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

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

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

      注釋:只要不是數(shù)字,有漢字?jǐn)?shù)字字母多種組成的形式都要加單引號,表示字符串。

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

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

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

      作業(yè):

      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,查詢學(xué)生表中5到10行的數(shù)據(jù):select * from student limit 4,6;

      4,顯示3班語文成績?yōu)?0,數(shù)學(xué)成績?yōu)?8,的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班,語文成績與數(shù)學(xué)成績都大于80的class與number信息:

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

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

      分組與函數(shù)查詢

      溫馨提示:分組之后查詢其他函數(shù)結(jié)果是不正確的;

      分組函數(shù):group by

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

      不分班級查詢總?cè)藬?shù)最高分: select max(maths) from score;

      注釋: max:最大值;

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

      注釋:最小值min;

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

      注釋:sum:總分;

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

      注釋:avg:平均值:

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

      注釋:count:有價值的;

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

      例句:

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

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

      運算符:數(shù)學(xué)運算符

      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:為左連接。 兩表關(guān)聯(lián):其ID必須一一對應(yīng)(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;

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

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

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

      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;

      題目練習(xí)

      顯示總分大于200的學(xué)生信息:

      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;

      顯示班級總數(shù)大于等于20的班級:

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

      顯示人總數(shù)大于等于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:撤回的意思,命令執(zhí)行后;可以撤回為修改刪除前的數(shù)據(jù);

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

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

      修改語句

      update 表名 set where 條件

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

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

      mysql> update student set birth=birth-5;

      把所有學(xué)生的年紀(jì)增加5歲;

      刪除語句

      mysql> delete from student where id=901;

      刪除901同學(xué)的,學(xué)生信息

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

      刪除湖南籍學(xué)生的信息

      mysql> delete from student;

      清空學(xué)生表信息

      字符查詢like

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

      查詢地址為北京的學(xué)生信息

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

      查詢地址為北京市昌平區(qū)的學(xué)生信息

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

      查詢湖南籍學(xué)生的成績信息;

      作業(yè):

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

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

      2,把計算機科目的分?jǐn)?shù)降低5分

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

      3,把湖南省學(xué)生計算機分?jǐn)?shù)提高5分

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

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

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

      5,刪除904學(xué)生的成績

      delete from score where stu_id=904;

      6,刪除湖南籍貫學(xué)生的成績

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

      7,刪除王姓與張姓同學(xué)英語成績

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

      8,刪除年紀(jì)大于30的學(xué)生的計算機成績

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

      MySQL練習(xí)

      創(chuàng)建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) );

      創(chuàng)建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,'計算機系', '北京市海淀區(qū)');

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

      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表查詢所有學(xué)生的學(xué)號(id)、姓名(name)和院系(department)的信息

      mysql> select id,name,department from student;

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

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

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

      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.用連接的方式查詢所有學(xué)生的信息和考試信息

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

      9.計算每個學(xué)生的總成績

      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分的學(xué)生信息

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

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

      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表中查詢出學(xué)生的學(xué)號,然后合并查詢結(jié)果

      select id from student union select id from score;

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

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

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

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

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

      分解: 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,數(shù)據(jù)庫,sql,命令  
      責(zé)任編輯:admin
      請文明參與討論,禁止漫罵攻擊。 昵稱:注冊  登錄
      [ 查看全部 ] 網(wǎng)友評論
      關(guān)于我們 - 聯(lián)系我們 - 廣告服務(wù) - 友情鏈接 - 網(wǎng)站地圖 - 版權(quán)聲明 - 在線幫助 - 文章列表
      返回頂部
      刷新頁面
      下到頁底
      晶體管查詢
      主站蜘蛛池模板: 狠狠做深爱婷婷久久综合一区| 伊人丁香狠狠色综合久久| 麻豆久久婷婷综合五月国产| 亚洲成色在线综合网站| 久久综合日韩亚洲精品色| 中文网丁香综合网| 色婷婷色综合激情国产日韩| 久久久久综合网久久| 国产成人人综合亚洲欧美丁香花| 国产综合视频在线观看一区 | 精品综合一区二区三区| 亚洲人成伊人成综合网久久久| 亚洲五月丁香综合视频| 久久综合琪琪狠狠天天| 91久久婷婷国产综合精品青草 | 色婷婷久久综合中文网站| 久久综合久久综合久久| 乱色熟女综合一区二区三区| 麻豆狠色伊人亚洲综合网站| 国产精品无码久久综合| 色欲综合久久躁天天躁| 婷婷激情五月综合| 亚洲欧美日韩综合久久久久| 伊人久久综合热线大杳蕉下载| 伊人久久大香线蕉综合影| 久久综合给合综合久久| 久久九色综合九色99伊人| 亚洲国产欧美国产综合一区| 亚洲色偷偷综合亚洲av78| 亚洲国产成人综合| 色综合天天综合网站中国| 伊色综合久久之综合久久| 97久久久精品综合88久久 | 亚洲狠狠婷婷综合久久| 亚洲av综合av一区二区三区| 国产亚洲综合精品一区二区三区| 国产香蕉尹人综合在线观看| 激情综合色五月六月婷婷| 狠狠色狠狠色综合日日不卡| 狠狠色婷婷综合天天久久丁香| 久久亚洲综合色一区二区三区|