DBMS信息
顯示DBMS的版本
select version();
顯示DBMS狀態
status;
顯示DBMS資源狀態
show status;
顯示DBMS支持的權限
show privileges;
查看DBMS用戶的權限
show grants for user_name;
DBMS變量
顯示DBMS的變量名稱及值
show variables;
顯示DBMS的使用端口
show variables like 'port';
顯示DBMS的編碼
show variables like 'character%';
顯示DBMS的數據文件存放路徑
show variables like '%datadir%';
顯示DBMS的的最大連接數
show variables like '%max_connections%';
數據庫
顯示DBMS的所有數據庫;
show databases;
創建數據庫
create database db_name;
刪除數據庫
drop database db_name;
選擇數據庫
use db_name;
顯示當前使用的數據庫
select database();
顯示當前登錄的用戶名稱
select user();
顯示當前數據庫支持及默認的存儲引擎
show engines;
顯示當前數據庫的觸發器信息
show triggers;
數據庫表
顯示當前數據庫的表信息
show tables;
創建數據庫表
create table table_name;
刪除數據庫表
drop table table_name;
顯示當前數據庫的表狀態
show table status;
顯示表結構信息
describe table_name; 或 desc table_name; 或 show columns from able_name;
顯示表中的所有記錄
select * from table_name;
查看狀態
show table status;
show procedure status;
show function status;
查看創建屬性
show create procedure procedure_name;
show create function function_name;
show create view view_name;
show create table table_name;
異常信息反饋
查看上一條執行語句的異常信息反饋(錯誤、提醒和警告)
show errors;
show warnings;
示例-1
1-1 創建數據庫
[root@CentOS-7 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database sample;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sample |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use sample;
Database changed
MariaDB [sample]> show tables;
Empty set (0.00 sec)
MariaDB [sample]>
1-2 創建表
- Students表包含學號、姓名、年齡: Students(StudentID,StudentName,StudentAage)
- Classes表包含課程編號、課程名稱:Classes(ClassID,ClassName)
- Grade表包含學號、所選的課程編號、成績:StuentsClasses(StudentID,ClassID,Score)
CREATE TABLE Students
(
StudentID char(10) NOT NULL ,
StudentName char(50) NOT NULL ,
StudentAge int NULL
);
CREATE TABLE Classes
(
ClassID char(10) NOT NULL ,
ClassName char(50) NOT NULL
);
CREATE TABLE Grades
(
StudentID char(10) NOT NULL ,
ClassName char(10) NOT NULL ,
Score char(50) NOT NULL
);
1-3 插入數據
INSERT INTO Students(StudentID, StudentName, StudentAge) VALUES('st1', 'aaa', 11);
INSERT INTO Students(StudentID, StudentName, StudentAge) VALUES('st2', 'bbb', 22);
INSERT INTO Students(StudentID, StudentName, StudentAge) VALUES('st3', 'ccc', 33);
INSERT INTO Classes(ClassID, ClassName)
SELECT '001', 'Java' UNION ALL
SELECT '002', 'Python' UNION ALL
SELECT '003', 'Linux' ;
INSERT INTO Grades(StudentID, ClassName, Score)
SELECT 'st1', 'Java', '85' UNION ALL
SELECT 'st2', 'Java', '92' UNION ALL
SELECT 'st3', 'Java', '96' ;
INSERT INTO Grades(StudentID, ClassName, Score)
SELECT 'st1', 'Python', '88' UNION ALL
SELECT 'st2', 'Python', '81' UNION ALL
SELECT 'st3', 'Python', '97' ;
INSERT INTO Grades(StudentID, ClassName, Score)
SELECT 'st1', 'Linux', 'Unkown' UNION ALL
SELECT 'st3', 'Linux', 'Unkown' ;
1-4 查詢數據
查詢學生表格的結構和所有數據
desc Students;
select * from Students;
查詢選修課程的學生人數
select count(distinct StudentID) from Grades;
查詢年齡大於20的學生ID和姓名
select StudentName,StudentAge from Students where StudentAge > 20;
查詢選修Linux課程的學生ID和姓名
select StudentID, StudentName from Students where StudentID in (select StudentID from Grades where ClassName='Linux');
查詢不選修Linux課程的學生ID和姓名
select StudentID, StudentName from Students where StudentID not in (select StudentID from Grades where ClassName='Linux');
查詢選修2門課程的學生ID和姓名
select StudentID, StudentName from Students where StudentID in (select StudentID from Grades group by StudentID having count(distinct ClassName)=2);
1-5 更改數據
將學生ID為st1的Python課程成績修改為99
Update Grades set Score='99' where StudentID='st1';
在Classes表格增加Effort列
alter table Classes add Effort Char(12);
在Classes表格刪除Effort列
alter table Classes drop column Effort;
1-6 刪除數據
刪除表中的數據
delete from Grades where StudentID='st2' and ClassName='Python';
刪除表
drop tables Grades;
刪除數據庫
drop database sample;
不登陸MySQL界面,刪除數據庫
[root@CentOS-7 ~]# mysqladmin -u root -p drop sample
Enter password:
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'sample' database [y/N] y
Database "sample" dropped
[root@CentOS-7 ~]#
示例-2
表Story中包含貨物種類(list)A和B的庫存總量(StoryCount)分別為997和1234;
表Sale中貨物種類(list)A有2次出庫數量(SaleNumber)記錄分別為105和213;
表Sale中貨物種類(list)B有3次出庫數量(SaleNumber)記錄分別為116、211和303;
建立數據表並用一條SQL語句求出貨物A,B各剩下多少?
2-1 創建數據表
創建數據表Story結構並添加數據
CREATE TABLE Story(list VARCHAR(10), StoryCount INT);
INSERT INTO Story(list, StoryCount)
SELECT 'A','997' UNION ALL
SELECT 'B','1234' ;
創建數據表Sale結構並添加數據
CREATE TABLE Sale(list VARCHAR(10), SaleNumber INT);
INSERT INTO Sale(list, SaleNumber)
SELECT 'A','105' UNION ALL
SELECT 'A','213' UNION ALL
SELECT 'B','116' UNION ALL
SELECT 'B','221' UNION ALL
SELECT 'B','303' ;
查詢表結構和所有數據
desc Story;
select * from Story;
desc Sale;
select * from Sale;
2-2 計算貨物的剩余數量
MariaDB [demo]> select list, StoryCount from Story;
+------+------------+
| list | StoryCount |
+------+------------+
| A | 997 |
| B | 1234 |
+------+------------+
2 rows in set (0.00 sec)
MariaDB [demo]> select list, sum(SaleNumber)SaleCount from Sale group by list;
+------+-----------+
| list | SaleCount |
+------+-----------+
| A | 318 |
| B | 640 |
+------+-----------+
2 rows in set (0.00 sec)
MariaDB [demo]> select Story.list, Story.StoryCount - sum(Sale.SaleNumber) from Story, Sale where Story.list=Sale.list group by list;
+------+-----------------------------------------+
| list | Story.StoryCount - sum(Sale.SaleNumber) |
+------+-----------------------------------------+
| A | 679 |
| B | 594 |
+------+-----------------------------------------+
2 rows in set (0.00 sec)
MariaDB [demo]>
2-3 刪除數據表
DROP TABLE Story;
DROP TABLE Sale;
