当前位置:网站首页>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 .
边栏推荐
- Huimang micro IO MCU ft60f010a-urt
- Does pytorch support 32 bits?
- Modbus protocol communication exception
- Solution pour arrêter automatiquement les paquets Jar en cours d'exécution en éteignant le serveur de connexion xshell
- Wechat applet - arrows floating up and down
- Taiwan Feiling fm8pb513b MCU provides MCU program development product design
- 每日一题——“水仙花数”
- Atcoder beginer contest 237 VP supplement
- Redisson 高性能 Redis 分布式锁源码分析
- 567. Arrangement in string
猜你喜欢

售价仅40元,树莓派Pico开发板加入WiFi模块,刚上市就脱销

Chrome browser quick access stackoverflow

Easyswoole3.2 restart failed

每日一题——倒置字符串

【Zuul】com.netflix.zuul.exception.ZuulException: Hystrix Readed time out

【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)

Troubleshooting ideas that can solve 80% of faults

第十五章 字符串本地化和消息字典(一)

深入理解ThreadLocal

EdgeNeXt打出了一套混合拳:集CNN与Transformer于一体的轻量级架构
随机推荐
义隆EM78P153K DIP14单片机 MCU
Longest non repeating subarray
Chrome browser quick access stackoverflow
aloam 代码阅读与总结
Redisson high performance redis distributed lock source code analysis
HDU - 1114 Piggy Bank (full backpack)
嵌入式开发板 ~ 说明
详解Kubernetes网络模型
我的创作纪念日
Pms132b single chip microcomputer TWS digital tube Bluetooth charging chamber program development
Larvel document reading notes custom authentication login and registration using larvel 8
ORA-19838 -- 恢复控制文件到备库
Problems needing attention in the development and debugging of Yingguang single chip microcomputer
应广单片机开发调试应注意的问题
辉芒微IO单片机FT60F11F-MRB
深入理解ThreadLocal
Chapter 15 string localization and message Dictionary (1)
Yingguang pmc131 SOP16 16pin eight bit MCU
wait_for_gap -- 从主库归档备库恢复归档
JDBC