当前位置:网站首页>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 .
边栏推荐
- 松翰SN8P2511 SOP8单片机 可代烧录 提供单片机方案开发 单片机解密
- uva1169
- 如何下载微信支付证书(API证书)
- What should we pay attention to in the development process of Yingguang single chip microcomputer?
- 读写 XML/JSON/INI 和 UBJSON 等格式的数据文件的统一接口
- Aloam code reading and summary
- Keras' deep learning practice -- gender classification based on vgg19 model
- How to download wechat payment certificate (API certificate)
- Pfc232-sop8/14/16 should be wide-ranging and can be tape programmed with burning program
- Problems needing attention in the development and debugging of Yingguang single chip microcomputer
猜你喜欢
[how to connect the network] Chapter 5 explore the server
【曆史上的今天】7 月 2 日:BitTorrent 問世;商業系統 Linspire 被收購;索尼部署 PlayStation Now
Modbus协议通信异常
VirtualLab基础实验教程-7.偏振(2)
Keras深度学习实战——基于VGG19模型实现性别分类
[nonlinear control theory]8_ Comparison of three robust controllers
Chrome browser quick access stackoverflow
透过华为军团看科技之变(六):智慧公路
ORA-19838 -- 恢复控制文件到备库
MySQL安装与配置
随机推荐
uva1169
Many scenic spots are temporarily closed due to the typhoon. The provincial culture and tourism department reminds you to pay attention to safety!
PMS150C应广单片机开发案例
wait_for_gap -- 从主库归档备库恢复归档
Aloam code reading and summary
Troubleshooting ideas that can solve 80% of faults
应广单片机开发调试应注意的问题
Yingguang single chip microcomputer pms150/pmc150/pms150c consumer single chip microcomputer
Keras深度学习实战——基于VGG19模型实现性别分类
怎么可以省去大量的switch语句,省去switch语句
Ora-19838 -- restore control files to the standby database
Mb10m-asemi rectifier bridge mb10m
读写 XML/JSON/INI 和 UBJSON 等格式的数据文件的统一接口
Deep understanding of ThreadLocal
如何下载微信支付证书(API证书)
Use of nexttile function in MATLAB
Easyai notes - deep learning
原厂原装 应广单片机PMS134方案开发应用案例
Problems needing attention in the development and debugging of Yingguang single chip microcomputer
Linux中,mysql设置job任务自动启动