Mysql教程:(一)資料庫常用基礎命令
阿新 • • 發佈:2021-11-06
資料庫常用命令
1、登入
- 進入資料庫,在win系統下,開啟cmd,切換使用者許可權,進入root:
- 沒許可權,用root登入:
mysql -uroot
- 如果root有密碼:
mysql -uroot -p
2、資料庫建立
- 查詢所有資料庫:
show databases;
- 建立資料庫:
create database <資料庫名>;
- 刪除資料庫:
drop database <資料庫名>;
- 進入資料庫:
use <資料庫名>;
3、資料表的操作
1)查詢資料庫下表:
show tables;
2)建立表1:
create table student(id int(4) primary key,name char(20));
- 註釋: id為表的第一列;
- int數字型別;
- primary key主鍵的意思,列不能重複。
- Name為表的第二列名字。
- char:型別;
建立表2:
create table score(id int(4) not null,class int(2));
註釋:not null欄位不能為空。
建立表3:
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)刪除一個表字段:
altertable student1 drop number;
4、表資料的增刪查改
提示:在資料庫匯入表時,要修改列的欄位型別並設定主鍵;
主鍵:表中經常有一個列或多列的組合,其值能唯一地標識表中的每一行。這樣的一列或多列稱為表的主鍵,通過它可強制表的實體完整性。當建立或更改表時可通過定義 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);
2) 查詢表資料格式:
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班的成績資訊: 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;
4、作業:
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;
本部落格所有文章僅用於學習、研究和交流目的,歡迎非商業性質轉載。
本文來自部落格園,作者:hello_殷,轉載請註明原文連結:https://www.cnblogs.com/yinzuopu/p/15516251.html
本文版權歸作者和部落格園共有,歡迎轉載,但必須給出原文連結,並保留此段宣告,否則保留追究法律責任的權利。