Database operation (1) basics of adding, deleting, modifying and checking

MySQL database used

1. Library

increase

Create a new shuju library

create database shuju;

delete

Delete the shuju library

drop database shuju;

check

query all databases

show databases;

Two, table

increase

auto_increment is self-increment; primary key(id) sets id as the primary key; int and varchar are both character types followed by parentheses to specify the character length; not null is not empty;

create table users(
id int auto_increment,
name varchar(100) not null,
password varchar(10) not null,
primary key(id)
);

Only int type and primary key can use auto_increment
The created table is as follows:
new table

delete

delete users table

drop table users;

change

Add a mail column to the users table and set a default value.

alter table users add mail varchar(20) default "[email protected]";

add a column
Delete the newly added column

alter table users drop mail;

check

Query all tables in a library (first enter this database)

show tables;

Query table structure

describe users;

insert image description here


3. Content

increase

Add a piece of data to the users table:

insert into users(
id,name,password
)
values(
1,"zhangsan","pass"
);

If a column has a default value, the column can be omitted when inserting a row, and the value of this column is not filled. For example, the id in the users table is a self-increasing field, and the data of Li Si is added:

insert into users(
name,password
)
values(
"李四","123456"
);

The newly added and completed table is shown in the following figure:
Add field content

delete

Delete Li Si's data

delete from users where id=2;

Directly clear the records in the table and delete them all.

delete from users;

change

Change the piece of data whose id is 1 in the users table, and change the contents of the name and password fields.

update users
set name="张三",password="456789"
where id=1;

Field content change operation

check

*The asterisk is a wildcard, which means to query all columns.

select id,name,password from users;
或者:
select * from users; 

Additional Supplement
1. Use the command line to operate MySQL, first start the service, and then log in to the MySQL database.

net start mysql   # 开启服务
mysql -uroot -p   # 使用root用户登录数据库

2. If the command line wants to operate on the table, it must first enter the corresponding database.

use 库名;

3. The content of the primary key is not allowed to be reused. Take a chestnut: delete the record of Li Si, and add a record of Wang Wu. The id of Wang Wu will be automatically assigned a value of 3 instead of 2. Because id=2 is Li Si's id, even if Li Si is deleted, its id value will not be used for new data.
Primary keys are not reusable
4. The update and delete statements should keep up with the where clause when used, otherwise the entire table will be updated/deleted directly.
5. It is best to use the primary key when using "where", because the primary key is unique and non-empty, and changes and reuse are not allowed.

Guess you like

Origin blog.csdn.net/zibery/article/details/125280071