当前位置:网站首页>What statements are added to MySQL
What statements are added to MySQL
2022-06-21 20:07:00 【Yisu cloud】
mysql What is the added statement
This article mainly explains “mysql What is the added statement ”, The explanation in the text is simple and clear , Easy to learn and understand , Next, please follow Xiaobian's ideas and go deeper slowly , Study and learn together “mysql What is the added statement ” Well !
Add statement has :1、CREATE DATABASE sentence , Used to add database , grammar “CREATE DATABASE Database name ;”;2、CREATE TABLE sentence , Used to add data table , grammar “CREATE TABLE Table name ( Name type );”;3、ALTER TABLE sentence , You can add fields to the data table , grammar “ALTER TABLE Table name ADD Field name type ;”;4、INSERT sentence , You can add data to a field .
The operating environment of this tutorial :windows7 System 、mysql8 edition 、Dell G3 The computer .
1、MySQL Add database (CREATE DATABASE sentence )
stay MySQL in , have access to CREATE DATABASE Statement create database , The basic syntax is as follows :
CREATE DATABASE [IF NOT EXISTS] Database name [CHARACTER SET Character set name ] [COLLATE Proofreading rule name ];
IF NOT EXISTS: Judge before creating the database , The operation can only be performed if the database does not currently exist . This option can be used to avoid the error of repeatedly creating the existing database .
CHARACTER SET: Specify the character set of the database . The purpose of specifying the character set is to avoid garbled data stored in the database . If the character set is not specified when creating the database , Then use the system's default character set .
COLLATE: Specifies the default collation rules for the character set .
MySQL Character set for (CHARACTER) And proofreading rules (COLLATION) It's two different concepts . The character set is used to define MySQL How to store strings , Proofing rules define how strings are compared . We'll explain it separately later MySQL The character set and proofreading rules of .
Example : Create a file called test_db The database of
CREATE DATABASE test_db;

View or display databases
SHOW DATABASES;

2、MySQL Add data sheet (CREATE TABLE sentence )
stay MySQL in , have access to CREATE TABLE Statement create table . Its grammatical form is :
CREATE TABLE < Table name > ([ Table definition options ])[ Table options ][ Partition options ]);
among ,[ Table definition options ] The format is :
< Name 1> < type 1> [,…] < Name n> < type n>
CREATE TABLE More command syntax , It is mainly created and defined by the table (create-definition)、 Table options (table-options) And partition options (partition-options) Made up of .
Tips : Use CREATE TABLE Create table time , The following information must be specified :
The name of the table to be created is not case sensitive , Out of commission SQL Keywords in language , Such as DROP、ALTER、INSERT etc. .
Each column in the data table ( Field ) Name and data type of , If you create multiple columns , Separate them with commas .
Example : Create a table in the specified database
notes : Data table belongs to database , Before creating the data table , Statement expected “USE< database >” Specify the database in which the operation is performed , If no database is selected , Will throw No database selected Error of .
Select the database to create the table test_db, establish tb_emp1 Data sheet :

CREATE TABLE tb_emp1(id INT(11),name VARCHAR(25),deptId INT(11),salary FLOAT);

After statement execution , You create a file named tb_emp1 Data sheet for , Use SHOW TABLES; Statement to check whether the data table is created successfully

3、MySQL Add fields to the data table (ALTER TABLE sentence )
stay MySQL Can be used in ALTER TABLE Statement to change the structure of the original table , For example, add or delete columns 、 Change the original column type 、 Rename columns or tables, etc .
A complete field includes the field name 、 Data types and constraints .MySQL The syntax format for adding fields is as follows :
ALTER TABLE < Table name > ADD < new field name >< data type >[ constraint condition ];
The syntax of the format is as follows :
< Table name > For the name of the data table ;
< new field name > For the name of the field to be added ;
< data type > The data type that can store data for the field to be added ;
[ constraint condition ] It's optional , Used to constrain the added fields .
This syntax format defaults to the last position in the table ( After the last column ) Add new fields .
Example : stay tb_emp1 Add fields to the data table
Use DESC see tb_emp1 Table structure
DESC tb_emp1;

Use ALTER TABLE Add a INT Type field age
ALTER TABLE tb_emp1 ADD age INT(4);

4、MySQL Add data (INSERT sentence )
After the database and table are created successfully , You need to insert data into the tables in the database . stay MySQL Can be used in INSERT Statement to insert one or more rows of tuple data into an existing table in the database .
INSERT There are two grammatical forms of sentences , Namely INSERT…VALUES Statement and INSERT…SET sentence .
1)、 INSERT…VALUES sentence
INSERT VALUES The grammar format of is :
INSERT INTO < Table name > [ < Name 1> [ , … < Name n>] ]VALUES ( value 1) [… , ( value n) ];
The grammar is as follows .
< Table name >: Specifies the name of the table being manipulated .
< Name >: Specify the name of the column to insert the data . If you insert data into all the columns in the table , Then all column names can be omitted , Direct adoption INSERT< Table name >VALUES(…) that will do .
VALUES or VALUE Clause : This clause contains the list of data to insert . The order of the data in the data list corresponds to the order of the columns .
2)、INSERT…SET sentence
The grammar format is :
INSERT INTO < Table name >SET < Name 1> = < value 1>, < Name 2> = < value 2>, …
This statement is used to specify the corresponding column values directly to some columns in the table , The column name of the data to be inserted is in SET Clause ,col_name For the specified column name , After the equal sign is the specified data , And for unspecified Columns , The column value is specified as the default value for that column .
notes : When using a single INSERT When a statement inserts multiple rows of data , Just enclose each row of data in parentheses .
Example : Add values to all fields in the table
stay test_db Create a course information table in the database tb_courses, Include course number course_id、 Course name course_name、 course credit course_grade And course notes course_info
CREATE TABLE tb_courses(course_id INT NOT NULL AUTO_INCREMENT,course_name CHAR(40) NOT NULL,course_grade FLOAT NOT NULL,course_info CHAR(100) NULL,PRIMARY KEY(course_id));
stay tb_courses Insert a new record into the table ,course_id The value is 1,course_name The value is “Network”,course_grade The value is 3,info The value is “Computer Network”
Before performing the insert operation , see tb_courses surface
SELECT * FROM tb_courses;

The query result shows that the content of the current table is empty , No data , Next, perform the operation of inserting data
INSERT INTO tb_courses(course_id,course_name,course_grade,course_info)VALUES(1,'Network',3,'Computer Network');

You can see that the record is inserted successfully . When inserting data , It specifies tb_courses All fields of the table , Therefore, a new value will be inserted for each field .
Thank you for reading , That's all “mysql What is the added statement ” Content. , After learning this article , I'm sure you're right mysql We have a deeper understanding of what the added sentence is , The specific use needs to be verified by practice . This is billion speed cloud , Xiaobian will push you articles with more relevant knowledge points , Welcome to your attention !
边栏推荐
- A simple architecture design logic | acquisition technology
- 汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(一)——整体系统设计
- 动态规划【一】(背包问题)
- Implementation of assembly language greedy snake and Tetris dual task design (II) -- detailed design of greedy snake
- Write down some pat topics (I)
- [force deduction 10 days SQL introduction] Day1
- How does the easycvr intelligent edge gateway hardware set power on self start?
- mysql如何查询第几条数据
- 机器学习之贝叶斯分类与集成学习
- 汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计
猜你喜欢

【ICML2022】CtrlFormer: 通过Transformer学习视觉控制的可迁移状态表示
![[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline](/img/fe/c204ea78051fb0544b27d44e6d1297.png)
[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline

汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计

剑指 Offer II 029. 排序的循环链表

Source code analysis of ArrayList

API interface for discharge summary identification - medical bill OCR identification / discharge diagnosis record / electronic medical record / claim settlement service
![婴儿名字[连通分量之邻接矩阵与DFS]](/img/60/83da6ce2fd2336fe1c4aead3260b77.png)
婴儿名字[连通分量之邻接矩阵与DFS]

播放量高达4000w+,情侣如何靠撒狗粮出圈?

508. Most Frequent Subtree Sum

技术实践 | 场景导向的音视频通话体验优化
随机推荐
Novice uses apiccloud visual development to build the mall home page
[dry goods knowledge] redis: from the application to the bottom, one article will help you
[comprehensive pen test] difficulty 2.5/5: "tree array" and "double tree array optimization"
金鱼哥RHCA回忆录:DO447Ansible Tower导航
Manjaro installs the downloaded TTF font file
mysql如何实现分组求和
自定义代码模板
Cloudcompare & PCL calculates transformation matrix based on matching points
RestTemplate多个认证信息Authorization问题
[icml2022] ctrlformer: learn the transferable state representation of visual control through the transformer
Uniapp applet opens the map and selects the location demo effect wx Chooselocation
汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计
W10 add system environment variable path
After the 80 version of Google browser, how to deal with the problem samesite cross domain problem
【力扣10天SQL入门】Day1
机器学习之模型评估与选择
Cloudcompare & PCL point cloud point matching (based on European distance)
Model evaluation and selection of machine learning
决策树的实现和调优(sklearn,GridSearchCV)
Whether Gorm database needs to set foreign keys