当前位置:网站首页>Basic MySQL database operations
Basic MySQL database operations
2022-06-22 22:34:00 【Fire eye Dragon】
1、DDL Database operation of operation
-- View all databases
show DATABASES;
-- Create database
CREATE DATABASE [IF not EXISTS] mydb1 [CHARSET=UTF8];
-- Choose to use the database
use mydb1;
-- Delete database
drop DATABASE[ if EXISTS] mydb1;
-- Modify database code
alter DATABASE mydb1 CHARACTER set utf8;
notes :1. Things in brackets can be omitted , You need to remove the brackets before copying
2.IF not EXISTS(if EXISTS) The difference between adding and not adding is that there is no error when adding , If not, if the database exists ( non-existent ) May be an error .
2、DDL Operation table creation
-- choice mydb1
use mydb1;
-- Create table
CREATE TABLE IF NOT EXISTS student(
sid int,
name varchar(20),
gender varchar(1),
age int,
birth date,
address varchar(20),
score double
);
data type :
Data type refers to specifying the data type for the fields in the table when creating the table , Only data that meets the type requirements can be stored , The principle of using data types is : Enough is enough. , Try to use a small range of , No big ones , To save storage space .
value type :
| type | size | Range ( A signed ) | Range ( Unsigned ) | purpose |
|---|---|---|---|---|
| TINYINT | 1 byte | (-128,127) | (0,255) | Small integer value |
| SMALLINT | 2 bytes | (-32768,32767) | (0,65535) | Large integer value |
| MEDIUMINT | 3 bytes | (-8388608,8388607) | (0,16777215) | Large integer value |
| INT or INTEGER | 4 bytes | (-2147483648,2147483647) | (0,4294967295) | Large integer value |
| BIGINT | 8 bytes | (-9223372036854775808,9223372036854775087) | (0,18446744073709551615) | Maximum integer value |
| FLOAT | 4 bytes | (-3.402823466E+38,3.402823466351E+38) | 0,(1.175494351E-38,3.402823466E+38) | Single precision floating point values |
| DOUBLE | 8 bytes | (-1.7976931348623157E+308,1.7976931348623157E+308) | 0,(2.2250738585072014E-308,1.7976931348623157E+308) | Double precision floating point value |
| DECIMAL | Depend on M and D Value | Depend on M and D Value | Small value |
String type :
| type | size | purpose |
|---|---|---|
| CHAR | 0-255 bytes | Fixed length string |
| VARCHAR | 0-65535 bytes | Variable length string |
| TINYBLOB | 0-255 bytes | No more than 255 Binary string of characters |
| TINYTEXT | 0-255 bytes | Text string |
| BLOB | 0-65535 bytes | Long text data in binary form |
| TEXT | 0-65535 bytes | Long text data |
| MEDIUMBLOB | 0-16777215 bytes | Medium length text data in binary form |
| MEDIUMTEXT | 0-16777215 bytes | Medium length text data |
| LONGBLOB | 0-4294967295 bytes | Maximum text data in binary form |
| LONGTEXT | 0-4294967295 bytes | Large text data |
The date type :
| type | size | Range | Format | purpose |
|---|---|---|---|---|
| DATE | 3 bytes | 1000-01-01/9999-12-31 | YYYY-MM-DD | Date value |
| TIME | 3 bytes | ‘-838:59:59’/‘838:59:59’ | HH:MM:SS | Time value or duration |
| YEAR | 1 byte | 1907/2155 | YYYY | The year is worth |
| DATETIME | 8 bytes | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | Mix date and time values |
| TIMESTAMP | 4 bytes | 1970-01-01 00:00:00/2038 The end time is the 2147483647 second , Beijing time. 2038-1-19-11:14:07, GMT 2038 year 1 month 19 Japan In the morning 03:14:07 | YYYYMMDD HHMMSS | Mix date and time values , Time stamp |
3、DDL Table of operations other operations
-- View all tables in the current database
SHOW TABLES;
-- View the creation statement of the specified table
SHOW CREATE TABLE student;
-- View table structure
DESC student;
-- Delete table
DROP TABLE student;
4、 Modify table structure
-- Add columns to :alter table Table name add listed type ( length )【 constraint 】;
ALTER TABLE student ADD dept VARCHAR(20);
-- Change column name and type :
-- alter table Table name change Old column names New column names type ( length ) 【 constraint 】;
ALTER TABLE student CHANGE dept department VARCHAR(30);
-- Modify table delete column :
-- alter table Table name drop Name ;
ALTER TABLE student drop department;
-- Modify the name of the table :
-- rename table Table name to The new name of the table ;
RENAME TABLE student to stu;
- DML Data insertion for operation
-- Format 1:insert into surface ( Name 1, Name 2……)values( value 1, value 2……);
-- Give one row of data at a time
INSERT INTO stu(sid,name,gender,age,birth,address,score)
VALUES(1001,' Zhang San ',' male ',18,'2001-12-23',' Beijing ',85.5);
-- Give multiple rows of data at a time
INSERT INTO stu(sid,name,gender,age,birth,address,score)
VALUES(1002,' Li Si ',' male ',18,'2001-10-23',' nanjing ',70.0),
(1003,' Wang Wu ',' Woman ',17,'2000-12-23',' Shenzhen ',88.5);
-- Assign values to only one or more values
INSERT INTO stu(sid) values(1004);
INSERT INTO stu(sid,name) values(1005,' Zhao Liu ');
-- Format 2:insert into surface values( value 1, value 2……);
INSERT INTO stu VALUES(1006,' Huang San ',' male ',18,'2001-5-23',' Beijing ',99.5);
notes : Format 1 The case where multiple lines are given at one time in the format also applies 2
- DML Data modification of operation
-- Data modification
-- Format 1:update Table name set Field name = value , Field name = value ……;
-- Format 2:update Table name set Field name = value , Field name = value ……where Conditions ;
UPDATE stu set address = ' Chongqing ';
UPDATE stu set address = ' Beijing ' WHERE sid = 1004;
UPDATE stu set address = ' Shanghai ' WHERE sid >1004;
UPDATE stu set address = ' Beijing ',score = 100 WHERE sid = 1005;
- DML Data deletion of operation
-- Data deletion
-- Format :delete from Table name [where Conditions ];
-- truncate table Table name perhaps truncate Table name
DELETE FROM stu WHERE sid = 1004;
-- Delete all data in the table
DELETE FROM stu;
-- Clear table data
TRUNCATE TABLE stu;
notes :delete and truncate The principle is different ,delete Only delete content , and truncate Be similar to drop table , It can be understood as deleting the whole table , Then create the table .
5、MySQL database DML Operation practice
边栏推荐
- The required reading for candidates | PMP the test on June 25 is approaching. What should we pay attention to?
- NFT can only be viewed from afar, but not blatantly played
- CYCA少儿形体礼仪 深圳市培训成果考核圆满落幕
- SPA项目开发之首页导航+左侧菜单
- 【李沐】 如何读论文【论文精读】
- CSV add a new column
- Plan and change of continuous repair
- 二级造价工程师考前必备15个知识点来了!祝你旗开得胜!
- Es total number of data queried by criteria
- LinkedList 源码解析
猜你喜欢

Home page navigation + left menu of spa project development

Uniapp applet mall develops thinkphp6 points mall, group purchase and seckill packaged app

The xinjietu x70s has been listed for 87900 times and has leapfrogged the class in space safety. It is worthy of being a 7-seat SUV of the National University of China

新捷途X70S上市8.79万起,空间安全越级,不愧是网红国民大7座SUV

Core and semiconductor "RF eda/ filter design platform" shines ims2022
![[geometric vision] 4.2 piecewise linear transformation](/img/1e/a810f4d7e9a6a34647b5cb56fdde67.png)
[geometric vision] 4.2 piecewise linear transformation

In a frame because it set 'X-FRAME-OPTIONS' to' deny '
[database] SQL Server quickly creates tables to simulate departments, courses, teachers, students and scores

7-9 超级玛丽
![组合总数[标准回溯 + 回溯技巧--降低栈深度]](/img/88/3a07589bf8edab618139b1bf1680e8.png)
组合总数[标准回溯 + 回溯技巧--降低栈深度]
随机推荐
NFT can only be viewed from afar, but not blatantly played
In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!
Research hotspot - Official publicity! The release time of JCR zoning and impact factors will be determined in 2022!
6-5 图的深度遍历-邻接矩阵实现
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
Linux安装Mysql(包成功!!)
【论文解读】关于基于视觉无人机自主降落平台的论文梳理
Cryptography series: certificate format representation of PKI X.509
Liunx installing MySQL
NiO copy file call getchannel method transferfrom()
Is it bad for NFT that the market starts to cool down?
The necessary materials for the soft test have been released. All the soft test materials for the whole subject have been prepared for you!
RuntimeError: CUDA error: CUBLAS_ STATUS_ EXECUTION_ FAILED when calling `cublasSgemmStridedBatched( ha
如何在物联网低代码平台中使用数据字典功能?
自助图书馆系统-Tkinter界面和openpyxl表格综合设计案例
自助圖書館系統-Tkinter界面和openpyxl錶格綜合設計案例
[geometric vision] 4.2 piecewise linear transformation
Developing salary management system based on C language course paper + source code and executable EXE file
6-3 二叉树的非递归遍历
R language data Table data import practice: data Rename the name of the table data column (rename)