日韩欧美视频第二区,秋霞成人午夜鲁丝一区二区三区,美女日批视频在线观看,av在线不卡免费

電子開發(fā)網(wǎng)

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

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

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

目錄

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

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

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

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

分組與函數(shù)查詢

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

連接查詢

多表查詢

修改語句

刪除語句

字符查詢like

MySQL練習(xí)


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

進(jìn)入數(shù)據(jù)庫,在win系統(tǒng)下,打開cmd,切換用戶權(quán)限,進(jì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ù)庫名>;

進(jìn)入數(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)刪除一個(gè)表字段:alter table student1 drop number;

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

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

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

  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為單位自增長(zhǎng)的意思;

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

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

注釋:auto_increment自增長(zhǎng)的意思。+1。輸入該命令,表格會(huì)在新輸入自動(dòng)新增長(zhǎng)新的一行,id也會(huì)成自增。

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

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

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

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

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

例子:查詢成績(jī)表中,列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班的成績(jī)信息:mysql> select * from score where class=1 or class=2;

(2)查詢語文為77并且數(shù)學(xué)為88的成績(jī)信息:

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

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

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

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

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

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

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

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

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

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

(8) 統(tǒng)計(jì)成績(jī)表的總數(shù):mysql> select count(*) from score;

(9) 按照英語去重,顯示英語成績(jī)信息: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ù)字字母多種組成的形式都要加單引號(hào),表示字符串。

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

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

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

作業(yè):

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

;

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

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班語文成績(jī)?yōu)?0,數(shù)學(xué)成績(jī)?yōu)?8,的class與number信息,:

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

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

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

>

6,查詢2班與3班,語文成績(jī)與數(shù)學(xué)成績(jī)都大于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班級(jí)與學(xué)號(hào)信息

分組與函數(shù)查詢

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

分組函數(shù):group by

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

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

注釋: max:最大值;

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

注釋:最小值min;

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

注釋:sum:總分;

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

注釋:avg:平均值:

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

注釋:count:有價(jià)值的;

語句執(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í)行前面

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

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必須一一對(duì)應(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)連接查詢:兩個(gè)表同時(shí)都有的內(nèi)容才會(huì)顯示。

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

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

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;

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

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

顯示人總數(shù)大于等于20的班級(jí)的成績(jī)信息:

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)在的時(shí)間;

修改語句

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é)生的成績(jī)信息;

作業(yè):

1,把張三的計(jì)算機(jī)成績(jī)修改成60分

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

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

update score set grade=grade-5 where c_name='計(jì)算機(jī)';

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

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

4,把學(xué)號(hào)為904的學(xué)生,計(jì)算機(jī)成績(jī)改為85

update score set grade=85 where c_name='計(jì)算機(jī)' and stu_id=904;

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

delete from score where stu_id=904;

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

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

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

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

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

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,'計(jì)算機(jī)系', '北京市海淀區(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,'計(jì)算機(jī)系', '湖南省衡陽市');

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

select * from student limit 1,3;

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

mysql> select id,name,department from student;

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

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

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表中查詢每個(gè)院系有多少人

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

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

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

7.查詢李四的考試科目(c_name)和考試成績(jī)(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.計(jì)算每個(gè)學(xué)生的總成績(jī)

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

10.計(jì)算每個(gè)考試科目的平均成績(jī)

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

11.查詢計(jì)算機(jī)成績(jī)低于95分的學(xué)生信息

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

12.查詢同時(shí)參加計(jì)算機(jī)和英語考試的學(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= '計(jì)算機(jī)') 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='計(jì)算機(jī)' )and c_name='英語');

select student.* from student,(select stu_id from score where stu_id in (select stu_id from score where c_name='計(jì)算機(jī)' )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='計(jì)算機(jī)') and id in (select stu_id from score sc where sc.c_name='英語');

13.將計(jì)算機(jī)考試成績(jī)按從高到低進(jìn)行排序

select c_name,grade from score where c_name='計(jì)算機(jī)' order by grade;

14.從student表和score表中查詢出學(xué)生的學(xué)號(hào),然后合并查詢結(jié)果

select id from student union select id from score;

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

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é)生的姓名、年齡、院系和考試科目及成績(jī)中文系

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

17.查詢每個(gè)科目的最高分的學(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
  • 上一篇文章:
  • 下一篇文章: 沒有了
  • 請(qǐng)文明參與討論,禁止漫罵攻擊。 昵稱:注冊(cè)  登錄
    [ 查看全部 ] 網(wǎng)友評(píng)論
    推薦文章
    • 此欄目下沒有推薦文章
    熱門文章
    • 此欄目下沒有熱點(diǎn)文章
    關(guān)于我們 - 聯(lián)系我們 - 廣告服務(wù) - 友情鏈接 - 網(wǎng)站地圖 - 版權(quán)聲明 - 在線幫助 - 文章列表
    返回頂部
    刷新頁面
    下到頁底
    晶體管查詢
    主站蜘蛛池模板: 汝阳县| SHOW| 法库县| 郧西县| 绿春县| 陇南市| 常熟市| 海安县| 赫章县| 内黄县| 兴海县| 伊金霍洛旗| 宜兰县| 华宁县| 土默特右旗| 阜阳市| 长宁县| 西宁市| 鄂伦春自治旗| 平乡县| 霍州市| 闸北区| 罗甸县| 库尔勒市| 满洲里市| 鹤峰县| 芜湖市| 扬州市| 宁晋县| 静海县| 连山| 屯昌县| 西峡县| 广平县| 泾阳县| 即墨市| 宜兰县| 繁昌县| 西畴县| 晋州市| 延庆县|