当前位置:网站首页>MySQL --- 數據庫的基本操作
MySQL --- 數據庫的基本操作
2022-07-02 17:56:00 【小雪菜本菜】
准備測試數據
要學習SQL查詢語句,首先必須解决一個問題,數據問題。
這裏提供了一個test.sql文件。
登錄MySQL,輸入source xxx/test.sql
導入sql文件,sql文件實際上是一個脚本文件,裏面有多行SQL語句,通過source命令可以批量執行。
執行完畢之後,使用show databases;
查看所有數據庫,發現多了一個名為test的數據庫。
使用show tables;
查看test數據庫下所有的數據錶,發現有四個錶。
使用另一種方式導入數據庫
-- 創建數據錶
CREATE TABLE IF NOT EXISTS dept (
deptno SMALLINT PRIMARY KEY,
dname VARCHAR(14) ,
loc VARCHAR(13) ) ;
-- 插入測試數據 —— dept
INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO dept VALUES (20,'RESEARCH','DALLAS');
INSERT INTO dept VALUES (30,'SALES','CHICAGO');
INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON');
test 數據庫錶
以後講解SQL語句的時候,主要使用的是test數據庫下的四張錶作為案例,所以首先就必須對這些錶的作用以及列的數據類型做一個基本的了解。
部門錶:dept
相關英語翻譯:
accounting 會計 new york 紐約
research 研究員 dallas 達拉斯
sales 銷售 chicago 芝加哥
operarions 運營 boston 波士頓
雇員錶:emp
DECLMAL(7,2) 總共有 7 比特,小數點後面有兩比特
SMALLINT,兩字節,65535
相關英語翻譯:
clerk 店員
salesman 售貨員
manager 經理
analyst 化驗員
president 董事長
工資等級錶:salgrade
工資補貼錶(工資條):bonus
工資補貼錶沒有數據
SQL語句規範
使用SQL語句請遵循以下規範:
SQL語句不區分大小寫。但字符串常量區分大小寫,建議命令大寫,錶名、庫名小寫;
SQL語句可單行或多行書寫,每一個語句結束之後要以分號結尾;
用空格和縮進來提高語句的可讀性。
注釋:有三種風格的注釋
單行注釋可以用“#”
單行注釋第二種寫法用“-- ”,“--" 與注釋之間是有空格的。
多行注釋可以用/* */
數據庫的基本操作
在MySQL之中有許多的數據庫,可以使用以下命令查看所有數據庫:
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema | #主要存儲了系統中的一些數據庫對象信息,比如用戶錶信息、列信息、權限信息、字符集信息和分區信息等。
| mysql | #MySQL的核心數據庫,主要負責存儲數據庫用戶、用戶訪問權限等 MySQL 自己需要使用的控制和管理信息。
| performance_schema | #主要用於收集數據庫服務器性能參數。
| sys | #sys 數據庫主要提供了一些視圖,數據都來自於 performation_schema,主要是讓開發者和使用者更方便地查看性能問題。
+--------------------+
這些數據庫彼此之間是可以進行相互切換的,切換的基本語法如下:
USE <dbname>;
要想知道當前使用的是哪個數據庫,那麼可以使用select database();
命令來查看:
SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
在一個數據庫下一定會存在多張數據錶,那麼這個時候也可以直接利用以下命令查看所有數據錶:
SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| bonus |
| dept |
| emp |
| salgrade |
+----------------+
4 rows in set (0.02 sec)
查看具體某一張錶的信息
SELECT * FROM emp;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600 | 300 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250 | 500 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 | 0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300 | NULL | 10 |
+-------+--------+-----------+------+------------+------+------+--------+
14 rows in set (0.01 sec)
而如果想要知道某些數據錶的錶結構 / 字段類型,那麼可以使用DESC命令:
DESC emp;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | smallint | NO | PRI | NULL | |
| ename | varchar(10) | YES | | NULL | |
| job | varchar(9) | YES | | NULL | |
| mgr | smallint | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | smallint | YES | | NULL | |
| comm | smallint | YES | | NULL | |
| deptno | smallint | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.01 sec)
如果想要自己創建數據庫,則需要使用以下命令:
CREATE DATABASE <dbname>;
如果數據庫已經存在則會報錯Can't create database '<dbname>'; database exists
,我們可以先判斷數據庫是否存在,不存在就創建,存在則忽略(只有警告沒有報錯)。
CREATE DATABASE IF NOT EXISTS <dbname>;
如果想要删除數據庫,則可以使用以下命令:
DROP DATABASE <dbname>;
如果數據庫不存在則會報錯Can't drop database 'ss'; database doesn't exist
,在删除數據庫時可以判斷是否存在。
DROP DATABASE IF EXISTS <dbname>; #如果數據庫存在則删除
使用 DROP DATABASE 命令時要非常謹慎,在執行該命令後,MySQL 不會給出任何提示確認信息。DROP DATABASE 删除數據庫後,數據庫中存儲的所有數據錶和數據也將一同被删除,而且不能恢複。
边栏推荐
- 【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)
- 毕业总结
- 台湾飞凌FM8PB513B单片机提供单片机方案开发 产品设计
- 台风来袭,多景区暂时关闭,省文旅厅提醒注意安全!
- Two pieces of nature a day! Duan Fengfeng, an alumnus of the University of science and technology of China, was the third Chinese winner of the belby medal
- [target tracking] | data set summary
- 开发一个禁止删除namespace的控制器
- 【网络是怎么连接的】第四章 探索接入网和网络运营商
- The bottom simulation implementation of vector
- 体验一下阿里云文字识别OCR
猜你喜欢
Modbus协议通信异常
freemarker+poi实现动态生成excel文件及解析excel文件
Virtual lab basic experiment tutorial -7 Polarization (1)
每日一题——小乐乐改数字
Navigateur Chrome pour un accès rapide au stackoverflow
Develop a controller that prohibits deleting namespaces
[target tracking] |siamfc
Mb10m-asemi rectifier bridge mb10m
【网络是怎样连接的】第六章 请求到达服务器以及响应给客户端(完结)
Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
随机推荐
Pms150c Yingguang MCU development case
Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
JDBC
Longest non repeating subarray
原厂原装 应广单片机PMS134方案开发应用案例
[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)
[nonlinear control theory]8_ Comparison of three robust controllers
[how to connect the network] Chapter 5 explore the server
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
如何下载微信支付证书(API证书)
Aloam code reading and summary
好评率计算
Redisson high performance redis distributed lock source code analysis
Daily question - xiaolele changes the number
Easyswoole3.2 restart failed
Yilong em78p153k dip14 MCU
easyAI笔记——深度学习
Rk1126 platform project summary
透过华为军团看科技之变(六):智慧公路
Taiwan Feiling fm8pb513b MCU provides MCU program development product design