mysql database operation ------ table (addition, deletion, modification, check|CRUD), and constraints

notes:

Constraints:
|-- Primary key constraint (primary key)
|-- Foreign key constraint (foreign key)
|-- Non-null constraint (not null)
|-- Unique constraint (unique)
|-- Check constraint (check)
|-- Default


Operations on table contents:


In relational databases, the primary key is generally handed over to the database for maintenance (primary key self-increment):
|-- MySQL auto_increment


Increase (insert):
insert into t_name[(field 1 …)] values(v1 …);


Update:
modify the original data in the table to the data we need
update t_name set field 1 = new value, field 2 = new value... where condition
update t_hero set age = 18, gender = "female" where id = 7;


删除(delete):
delete from tablename where 条件
delete from t_hero where id =11;


truncate statement (use with caution)
This statement is also used to delete the table and cannot be recovered


select
select * from 表名;
select id, username, age, gender, tel from t_hero;
select username from t_hero;


Table modification: modifications
to the table structure : alter table

|-- Add new fields
ALTER TABLE table name ADD column name column type constraints;
alter table t_hero add address varchar(255);

|-- Modify field type
ALTER TABLE table name MODIFY column name column type;
alter table t_hero modify id bigint;

|-- Modify field name
ALTER TABLE table name CHANGE old column name new column name column type;
alter table t_hero change id hero_id int;

|-- Modify the table name
ALTER TABLE table name RENAME new table name;
alter table t_hero rename hero;
-The second way of writing
rename table hero to t_hero;


Table replication:

|-- create table 新表名 like 源表
create table xiyou like t_hero;

|-- create table 新表名 select * from 源表
create table sanguo select * from xiyou;

Practice:

Practice on t_hero;
Insert picture description here

update:
Insert picture description here
delete:
Insert picture description here
|-- add a new field
ALTER TABLE table name ADD column name column type constraint;
Insert picture description here
Insert picture description here
|-- modify field type
ALTER TABLE table name MODIFY column name column type;
Insert picture description here
|-- modify field name
ALTER TABLE table name CHANGE old column name new column name column type;

Insert picture description here
|-- Modify table name
ALTER TABLE table name RENAME new table name;
Insert picture description here
-The second way of writing
rename table hero to t_hero;
Insert picture description here
table copy:

Insert picture description here
Insert picture description here
The second method:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43790008/article/details/108898779