“This is the 9th day of my participation in the November Gwen Challenge. See the event details: The Last Gwen Challenge 2021” @TOC
Basic database operations
So here’s a book strap to get you familiar with the basic operations of the database. And then we’re slowly building up to playing with the database.
Operations on tables
Create table delete table view table
@TOC
This is the common type of the database
1. Value type
2. The value is a string
3. Date type
Create a database
1.1 Viewing All Databases
This command is used to display the current database. Note that the SQL statement must start with “; “. At the end.
show databaes;
Copy the code
! [insert picture description here] (https://img-blog.csdnimg.cn/57c782855de44e46b3cb5d740bb01553.bmp#pic_center)Copy the code
Once the instructions are entered, they are printed out as tables, with each row representing a separate database.
1.2 Creating a Database
The code to create a database looks like this
Here, take Book as an example
create database book;
Copy the code
If you see OK, you have successfully created a database. Good for you!
Note that databae cannot add S here because we are creating a database, not looking at many databases. This will result in syntax errors.
An error is specified for a single line of incorrect statements. Here is the Databases book.
1.3 Programmer taboo! Deleting a Database
Let’s take the database book as an example.
Drop database + database name; Don’t forget the semicolon and no S.
drop database book;
Copy the code
Deleting a database is a very dangerous operation. Once deleted, it is generally difficult to recover, which can cause great losses. We must be careful in the process of using it.
Second, create a book table in the created database
2.1 Using (Select) the database
Since we will most likely have more than one database, you need to select the database for the table you want to create. For example, if I want to create a book table in the database book, I need to select the database Book first. The specific format is as follows.
Ues the book;Copy the code
Don’t forget the semicolon, use + the specific database name. Database Changed means you are successful
2.2 create a table
We design a book table, including the following fields: book name, book author, book price, book classification. The code format is as follows
create tableBook (表 名字A -- 表 名字) comment '表 名字'; Select * from (select * from (select * from (select * from (select * from (select * from)))));; 表 示 名 称 C -- 表 示 名 称 (表 示 名 称) comment '表 示')Copy the code
create table bookInfo(
bookName varchar(20) comment 'Book Name',
bookAuthor varchar(20) comment 'Book author',
bookPrice varchar(20) comment 'Book prices',
bookSort varchar(20) comment 'Book Classification'
);
Copy the code
Specifically, create table is followed by the name of the table, except for the penultimate line, at the end of every line within parentheses.The parentheses after the field type define their size, and the comments after the field type refer to the comment surrounded by single quotation marks. And finally, after the parentheses, we also add “;” Semicolon, doesn’t it look like a C language structure?
See, OK, we did it.
See table 2.3
After creating a table, we can view the table through desc. Desc is short for the English word describe.
descBookInfo;Copy the code
We might be curious. Key, null, extra, and default are not included in the table when it is created.
Field is the name of the Field we just defined ourselves. Type is the field Type, which is what we defined, and Null means whether or not it’s allowed to be Null, which means there’s nothing there, and you can give no value. Key refers to the index type. Default is the Default value, which is NULL, and Extra is the extension value, which is not written here.
2.4 delete table
Okay, I don’t need it anymore, so how do I delete it? Drop table + table name.
drop tableBookInfo;Copy the code
OK again, we’ve done it again, OK now the database book is gone.
Three, environmental choice
Since we are typing code in the Mysql black box console, the font is black and not very bright, which makes it easy to make mistakes. Do you want to write like C or Java code with highlighting and error checking? Mysql Workbench meets all the needs of a ~! .Like this code is highlighted, different colors for different types, so you don’t have to worry about making mistakes anymore.
Four,
We learned to create a database, delete, display operations, for the creation of table display delete operations, finally mention a deleted library run this behavior is not necessary!