当前位置:网站首页>MySQL -- basic operation of database
MySQL -- basic operation of database
2022-07-02 17:56:00 【Sauerkraut】
Prepare test data
To learn SQL Query statement , First of all, we must solve a problem , Data problems .
Here's a test.sql file .
Sign in MySQL, Input source xxx/test.sql Import sql file ,sql The file is actually a script file , There are many lines in it SQL sentence , adopt source Commands can be executed in batches .

After execution , Use show databases; View all databases , I found a new one named test The database of .

Use show tables; see test All data tables under the database , Found four tables .

Import the database in another way
-- Create data table
CREATE TABLE IF NOT EXISTS dept (
deptno SMALLINT PRIMARY KEY,
dname VARCHAR(14) ,
loc VARCHAR(13) ) ;
-- Insert test data —— 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 Database table
Later on SQL At the time of statement , The main use is test Four tables under the database are used as cases , So first of all, you must have a basic understanding of the functions of these tables and the data types of columns .

Departmental table :dept


Relevant English Translation :
accounting accounting new york New York
research researcher dallas Dalas
sales sales chicago Chicago
operarions operating boston Boston
Employee list :emp


DECLMAL(7,2) All in all 7 position , There are two digits after the decimal point
SMALLINT, Two bytes ,65535
Relevant English Translation :
clerk clerk
salesman The salesman
manager The manager
analyst Laboratory Technician
president Chairman of the board of directors
Pay scale :salgrade


Wage subsidy table ( Pay slip ):bonus

There is no data in the wage subsidy table
SQL Statement specification
Use SQL Statement please follow the following specifications :
SQL Statement is case insensitive . But string constants are case sensitive , It is recommended that the order be capitalized , Table name 、 Database name in lowercase ;
SQL Statements can be written in one or more lines , Every statement should end with a semicolon ;
Use spaces and indentation to improve the readability of statements .
notes : There are three styles of annotation
A single line comment can be made with “#”
The second way to write a single line note is “-- ”,“--" There is a space between and comments .
Multiline comments can be made with /* */
Basic operation of database
stay MySQL There are many databases , You can use the following command to view all databases :
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema | # It mainly stores some database object information in the system , For example, user table information 、 Column information 、 Permission information 、 Character set information, partition information, etc .
| mysql | #MySQL The core database of , Mainly responsible for storing database users 、 User access rights, etc MySQL Control and management information that you need to use .
| performance_schema | # Mainly used to collect database server performance parameters .
| sys | #sys The database mainly provides some views , The data comes from performation_schema, The main purpose is to make it easier for developers and users to view performance problems .
+--------------------+These databases can be switched between each other , The basic syntax of switching is as follows :
USE <dbname>; You want to know which database you are currently using , Then you can use select database(); Order to see :
SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)There must be multiple data tables under one database , At this time, you can also directly use the following command to view all data tables :
SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| bonus |
| dept |
| emp |
| salgrade |
+----------------+
4 rows in set (0.02 sec)View the information of a specific table
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)If you want to know the table structure of some data tables / Field type , Then you can use DESC command :
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)If you want to create your own database , You need to use the following command :
CREATE DATABASE <dbname>; If the database already exists, an error will be reported Can't create database '<dbname>'; database exists, We can first judge whether the database exists , Create... If it doesn't exist , Being ignores ( Only warning without error ).
CREATE DATABASE IF NOT EXISTS <dbname>;If you want to delete the database , You can use the following command :
DROP DATABASE <dbname>; If the database does not exist, an error will be reported Can't drop database 'ss'; database doesn't exist, When deleting the database, you can determine whether it exists .
DROP DATABASE IF EXISTS <dbname>; # If the database exists, delete Use DROP DATABASE Be very careful when ordering , After executing the order ,MySQL No prompt confirmation message will be given .DROP DATABASE After deleting the database , All data tables and data stored in the database will also be deleted , And it can't be restored .
边栏推荐
- My creation anniversary
- 售价仅40元,树莓派Pico开发板加入WiFi模块,刚上市就脱销
- From a professional background, I can't get into a small company for interview
- Pms132b single chip microcomputer TWS digital tube Bluetooth charging chamber program development
- Typescript
- 515. 在每个树行中找最大值
- pytorch支持32位吗?
- 应广单片机(MCU单片机科普)
- easyAI笔记——深度学习
- 原厂原装 应广单片机PMS134方案开发应用案例
猜你喜欢

Use of nexttile function in MATLAB

Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
![[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)](/img/ef/1ac272dbd0e5c4d08a8f01f61d334d.png)
[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)

每日一题——小乐乐改数字

aloam 代码阅读与总结

【网络是怎样连接的】第六章 请求到达服务器以及响应给客户端(完结)

Alibaba cloud sub account - Permission Policy - full control permission granted to an account and an OSS bucket

自定义一个loading指令

Taiwan Feiling fm8pb513b MCU provides MCU program development product design

【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)
随机推荐
[how to connect the network] Chapter 5 explore the server
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
Yingguang single chip microcomputer (MCU popular science)
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
怎么可以省去大量的switch语句,省去switch语句
Yingguang single chip microcomputer development specification pmc131 with AD chip to detect battery voltage single chip microcomputer sop8/14
Develop a controller that prohibits deleting namespaces
Huimang micro IO MCU ft60f011a-rb
567.字符串中的排列
辉芒微IO单片机FT60F011A-RB
Experience Alibaba cloud character recognition OCR
VirtualLab基础实验教程-7.偏振(1)
应广单片机开发 工规 PMC131 带AD芯片检测电池电压单片机SOP8/14
Solution pour arrêter automatiquement les paquets Jar en cours d'exécution en éteignant le serveur de connexion xshell
HDU - 1114 Piggy Bank (full backpack)
Atcoder beginer contest 237 VP supplement
原装应广单片机 MCU芯片PMS152 SOP8封装 单片机开发
Many scenic spots are temporarily closed due to the typhoon. The provincial culture and tourism department reminds you to pay attention to safety!
自定义一个loading指令
体验一下阿里云文字识别OCR