MySQL database _ data addition, deletion, modification and query

MySQL database

Data addition, deletion and modification

Data addition, deletion and modification

  • Addition, deletion and modification (CURD)
    • C: Create【Create】
    • U: Update【Update】
    • R: Read【Retrieve】
    • D: Delete【Delete】

increase

format:INSERT INTO table_name (column_name1,column_name2,...) VALUES(VALUE1,VALUE2,...)

  • Note: The primary key column grows automatically, but it needs to occupy the position when inserting the whole column. Usually 0 or default or null is used to occupy the position. After the insertion is successful, the actual data shall prevail.
  • Full column insertion: the order of the values ​​corresponds to the order of the fields in the table
insert into 表名 values(...)
例:
insert into students values('张三',18,'上海市浦东区','2000-1-2');
  • Partial column (field) insertion: the order of the values ​​corresponds to the order of the given columns
insert into 表名(列1,...) values(值1,...)
例:
insert into students(name,hometown,birthday) values('李四','上海杨浦区','2001-3-2');
  • The above statement can insert one row of data into the table at a time, and it can also insert multiple rows of data at one time, which can reduce the communication with the database
  • Full column multi-row insertion: the order of the values ​​corresponds to the order of the given columns
insert into 表名 values(...),(...)...;
例:
insert into classes values(0,'文科班'),(0,'理科班');
insert into 表名(列1,...) values(值1,...),(值1,...)...;
例:
insert into students(name) values('张三'),('李四'),('王五');

delete

  • DELETE FROM table_name where conditional judgment
delete from 表名 where 条件
例:
delete from students where id=5;
  • Logical deletion is essentially a modification operation
update students set isdelete=1 where id=1;
  • Here,'isdelete' means a field, and '1' means whether it is a deleted state

modify

format:UPDATE tasble_name SET column_name1=VALUE1, column_name2=VALUE2,... where 条件判断

update 表名 set 列1=值1,列2=值2... where 条件
例:
update students set birthday='2001-3-2',hometown='上海宝山区' where name='张三';

Inquire

  • Query all columns
select * from 表名;
例:
select * from students;
  • Query the specified column
  • You can use as to specify aliases for columns or tables
select 列1,列2,... from 表名;
例:
select id,name as 姓名,age as 年龄,hometown as 家庭住址 from students;

Data backup and recovery

Backup

  • Run the mysqldump command
mysqldump –uroot –p 数据库名 > python.sql;

# 按提示输入mysql的密码

restore

  • Connect to mysql, create a new database
  • Exit the connection and execute the following command
mysql -uroot –p 新数据库名 < python.sql

# 根据提示输入mysql密码

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90273934