当前位置:网站首页>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;

mysql What is the added statement

View or display databases

SHOW DATABASES;

mysql What is the added statement

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 :

mysql What is the added statement

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

mysql What is the added statement

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

mysql What is the added statement

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;

mysql What is the added statement

Use ALTER TABLE Add a INT Type field age

ALTER TABLE tb_emp1 ADD age INT(4);

mysql What is the added statement

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;

mysql What is the added statement

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');

mysql What is the added statement

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 !

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211827198203.html