当前位置:网站首页>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 .
边栏推荐
- PMS132B单片机TWS数码管蓝牙充电仓方案开发
- 基数排序的简单理解
- ORA-19838 -- 恢复控制文件到备库
- MySQL --- 数据库的基本概念
- 【网络是怎么连接的】第四章 探索接入网和网络运营商
- VirtualLab基础实验教程-7.偏振(1)
- Solution to the problem that the easycvr kernel of intelligent video analysis platform cannot be started as a service
- Keras深度学习实战——基于VGG19模型实现性别分类
- Rk1126 platform project summary
- POJ - 1458 common subsequence (longest common subsequence)
猜你喜欢
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
售价仅40元,树莓派Pico开发板加入WiFi模块,刚上市就脱销
Deep understanding of ThreadLocal
Editor编辑器扩展在Scene View添加按钮和logo
Alibaba cloud sub account - Permission Policy - full control permission granted to an account and an OSS bucket
Easyswoole3.2 restart failed
Ssm+ wechat applet to realize property management system
透过华为军团看科技之变(六):智慧公路
Modbus协议通信异常
Taiwan Feiling fm8pb513b MCU provides MCU program development product design
随机推荐
Redisson high performance redis distributed lock source code analysis
基数排序的简单理解
Linux中,mysql设置job任务自动启动
每日一题——“水仙花数”
Android cycle timer implementation, to achieve fixed Android cache cleaning
松翰SN8P2511 SOP8单片机 可代烧录 提供单片机方案开发 单片机解密
[how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)
pytorch支持32位吗?
ORA-19838 -- 恢复控制文件到备库
Finally detailed explanation
[how to connect the network] Chapter 5 explore the server
开发一个禁止删除namespace的控制器
怎么可以省去大量的switch语句,省去switch语句
Rk1126 platform project summary
Typescript
What should we pay attention to in the development process of Yingguang single chip microcomputer?
【Golang | gRPC】使用gRPC实现简单远程调用
应广单片机PMS150/PMC150/PMS150C消费类单片机
第十五章 字符串本地化和消息字典(一)
Development of original Yingguang MCU chip pms152 sop8 encapsulated MCU