In the previous article, we talked about issues such as logging in, adding users, and changing passwords. In the next article, we will take a look at the database operations in MYSQL. Note: You must first log in to MYSQL. The following operations are performed at the MYSQL prompt, and each command ends with a semicolon.
1. Operation skills
1. If you forget to add a semicolon after pressing Enter when typing a command, you do not need to type the command again, just type a semicolon and press Enter. In other words, you can divide a complete command into several lines and use a semicolon as the end mark.
2. You can use the cursor up and down keys to call out previous commands. But an old version of MYSQL that I used before does not support it. I am currently using mysql-3.23.27-beta-win.
2. Display command
1. Display the database list.
show databases;
There were only two databases at the beginning: mysql and test. The mysql library is very important. It contains MYSQL system information. When we change passwords and add new users, we actually use this library for operations.
2. Display the data tables in the library:
use mysql; //Open the library. Those who have learned FOXBASE will be familiar with it.
show tables;
3. Display the structure of the data table:
describe table name;
4. Create database:
create database database name;
5. Create table:
use database name;
create table table name (field setting list) ;
6. Delete database and table:
drop database database name;
drop table table name;
7. Clear the records in the table:
delete from table name;
8. Display the records in the table:
select * from table name;
3. An example of creating a database, creating a table and inserting data
drop database if school exists; //If SCHOOL exists, delete it
create database school; //Create library SCHOOL
use school; //Open library SCHOOL
create table teacher //Create table TEACHER
(
id int(3) auto_increment not null primary key ,
name char(10) not null,
address varchar(50) default 'Shenzhen',
year date
); //End of table creation
//The following are the inserted fields
insert into teacher values('','glchengang','Shenzhen No.1 Middle School','1976-10-10');
insert into teacher values('','jack','Shenzhen No.1 Middle School', '1975-12-23');
Note: In the table under construction
(1) Set the ID to a numeric field with a length of 3: int (3) and let each record automatically increase by one: auto_increment It cannot be empty: not null and make it the primary field primary key
(2) Set NAME to a character field of length 10
(3) Set ADDRESS to a character field of length 50 and default Value is Shenzhen. What is the difference between varchar and char? I can only wait for a future article to talk about it.