18luck网站

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

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

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

目錄

數據庫常用命令

數據庫的創建

數據表的操作

表數據的增刪查改

分組與(yu) 函數查詢

運算符:數學運算符

連接查詢

多表查詢

修改語句

刪除語句

字符查詢like

MySQL練習(xi)


數據庫常用命令

進入數據庫,在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為(wei) 表的第一列;

int數字類型;

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

Name為(wei) 表的第二列名字。

char:類型;

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

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

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

Field (列名),Type(字段類型),null(是否為(wei) 空),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)刪除一個(ge) 表字段:alter table student1 drop number;

表數據的增刪查改

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

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

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

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

例子:

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

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。輸入該命令,表格會(hui) 在新輸入自動新增長新的一行,id也會(hui) 成自增。

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班與(yu) 2班的成績信息:mysql> select * from score where class=1 or class=2;

(2)查詢語文為(wei) 77並且數學為(wei) 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);

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

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

注釋: !在數據庫裏麵為(wei) 否定的意思:

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

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

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

(7)查詢語文成績大於(yu) 等於(yu) 80小於(yu) 等於(yu) 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表示從(cong) 第3行開始到第7行,共有4行;

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

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

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

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

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

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

作業(ye) :

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

;

2,查詢4班,語文成績大於(yu) 80小於(yu) 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班語文成績為(wei) 90,數學成績為(wei) 68,的class與(yu) 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班與(yu) 3班,語文成績與(yu) 數學成績都大於(yu) 80的class與(yu) number信息:

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

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

分組與函數查詢

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

分組函數: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:有價(jia) 值的;

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

例句:

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

from後麵可以加茲(zi) 查詢,語句先執行後麵再執行前麵

運算符:數學運算符

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:為(wei) 別名。student stu left join score:student:為(wei) 主表,score為(wei) 副表顯示。 left join:為(wei) 左連接。 兩(liang) 表關(guan) 聯:其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;

內(nei) 連接查詢:兩(liang) 個(ge) 表同時都有的內(nei) 容才會(hui) 顯示。

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

顯示查詢數據連接:把後表與(yu) 前排合起來在一個(ge) 表顯示。

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;

題目練習(xi)

顯示總分大於(yu) 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;

顯示班級總數大於(yu) 等於(yu) 20的班級:

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

顯示人總數大於(yu) 等於(yu) 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:撤回的意思,命令執行後;可以撤回為(wei) 修改刪除前的數據;

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

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

修改語句

update 表名 set where 條件

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

把張老大的出生日期修改為(wei) 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 '北京%';

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

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

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

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

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

作業(ye) :

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,把學號為(wei) 904的學生,計算機成績改為(wei) 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,刪除王姓與(yu) 張姓同學英語成績

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

8,刪除年紀大於(yu) 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));

為(wei) 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,'計算機係', '廣東(dong) 省深圳市');

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

向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.從(cong) student表查詢所有學生的學號(id)、姓名(name)和院係(department)的信息

mysql> select id,name,department from student;

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

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

4.從(cong) 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.從(cong) student表中查詢每個(ge) 院係有多少人

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

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

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.計算每個(ge) 學生的總成績

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

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

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

11.查詢計算機成績低於(yu) 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.將計算機考試成績按從(cong) 高到低進行排序

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

14.從(cong) 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.查詢每個(ge) 科目的最高分的學生信息.

分解: 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
  • 上一篇文章:
  • 下一篇文章: 沒有了
  • 請文明參與討論,禁止漫罵攻擊。 昵稱:注冊  登錄
    [ 查看全部 ] 網友評論
    推薦文章
    • 此欄目下沒有推薦文章
    熱門文章
    • 此欄目下沒有熱點文章
    關於我們 - 聯係我們 - 廣告服務 - 友情鏈接 - 網站地圖 - 版權聲明 - 在線幫助 - 文章列表
    返回頂部
    刷新頁麵
    下到頁底
    晶體管查詢