当前位置:网站首页>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 .
边栏推荐
- 售价仅40元,树莓派Pico开发板加入WiFi模块,刚上市就脱销
- Wechat applet - arrows floating up and down
- Editor编辑器扩展在Scene View添加按钮和logo
- How to download wechat payment certificate (API certificate)
- ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
- 外包干了五年,废了...
- Deep understanding of ThreadLocal
- 【历史上的今天】7 月 2 日:BitTorrent 问世;商业系统 Linspire 被收购;索尼部署 PlayStation Now
- Modbus protocol communication exception
- 王者荣耀商城异地多活架构设计
猜你喜欢

wait_ for_ Gap -- restore archive from primary archive to secondary Archive

Daily question - xiaolele changes the number

MySQL --- 数据库的基本概念
![List summation [dummy+ tail interpolation + function processing list reference common pit]](/img/08/30e8ca2376104d648a82dca8a72c42.png)
List summation [dummy+ tail interpolation + function processing list reference common pit]

能解决 80% 故障的排查思路

From a professional background, I can't get into a small company for interview

Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8

每日一题——“水仙花数”
![[how is the network connected] Chapter 4 explores access networks and network operators](/img/50/d16f4dca571a5a5f9b20fada289d45.png)
[how is the network connected] Chapter 4 explores access networks and network operators
![[target tracking] |siamfc](/img/40/3419761d2eb7f1193b699cdd431761.png)
[target tracking] |siamfc
随机推荐
【Golang | gRPC】使用gRPC实现简单远程调用
辉芒微IO单片机FT60F11F-MRB
Does pytorch support 32 bits?
515. 在每个树行中找最大值
List summation [dummy+ tail interpolation + function processing list reference common pit]
JDBC
Troubleshooting ideas that can solve 80% of faults
Pms150c Yingguang MCU development case
win10 kms activator
[nonlinear control theory]8_ Comparison of three robust controllers
Ora-19838 -- restore control files to the standby database
辉芒微IO单片机FT60F011A-RB
android之循环定时器实现,实现定Android时缓存清理
Longest non repeating subarray
Daily question - "number of daffodils"
MySQL进阶-事务及索引
Modbus协议通信异常
Huimang micro IO MCU ft60f11f-mrb
uva1169
辉芒微IO单片机FT60F010A-URT