Addition, deletion and modification of data table

Modify the field format:

alter table 表名 modify 字段名

An example is as follows:
– Change the number field in the student table to a fixed length and place it in the second digit (after the id).

desc my_student;   查看表中数据
alter table my_student
modify number char(10) after id;
desc my_student;

First, log in to the database in the cmd environment and enter the mydatabase database to operate.
write picture description here
Modify fields:
write picture description here

rename field

alter table 表名 change 旧字段 新字段名 数据类型 [属性][位置];

Examples are as follows:
– Modify the gender field in the student table to sex.

desc my_student;
alter table my_student 
change gender sex varchar(10);
desc my_student;

write picture description here
Delete fields:

alter table 表名 drop 字段名;

– delete the age field (age) in the student table

desc my_student;
alter table my_student drop age;
desc my_student;

write picture description here
If there is already data in the table, then deleting a field will clear all the data in the field (irreversible)
delete the data table

drop table 表名1 表名2...;

– Multiple tables can be deleted at one time, but the database cannot delete multiple tables at one time, because the system considers this a dangerous operation.
For example:
delete the class data table.

show tables;
drop tables class;
show tables;显示所有表

write picture description here
When the instruction to delete the data table occurs, what exactly is executed?
1. In the table space, there is no specified table (data correspondingly gone).
2. Under the database folder, the file corresponding to the table (related to the storage engine) will also be deleted.
write picture description here

*Note: Deleting is dangerous, and the operation needs to be cautious (irreversible) *
Adding data:
There are two options for adding new data.
Option 1. Insert data into the fields of the whole table without specifying a list. The order in which the values ​​of the data appear must be the same as the order in which the fields appear in the table; all non-numeric data needs to be wrapped in quotation marks (single quotation marks). .
– Can insert multiple records at once

insert into 表名 values (值列表)[,(值列表)]; 

An example is as follows: insert data.

desc my_student;
insert into my_student values(1,'itcast0001','jim','male'),(2,'itcast0002','hanmeimei','female');
select * from my_stydent;

write picture description here
Option 2: To insert data into some fields, you need to select a field list; the order in which the field list appears has nothing to do with the order of the fields.
But the order of the list of values ​​must match the order of the selected fields.

insert into 表名 (字段列表)values (值列表)[,(值列表)];

– Insert data, specifying a list of fields.

select * from my_student;
insert into my_student (numbers,sex,name,id) values ('itcast0003','male','tom',3),
('itcast0004','female','lily',4);
select * from my_student;

write picture description here

View data

select */字段列表 from 表名 [where 条件];

View all data:

select * from my_student;

write picture description here

– View specified fields, specify condition data.

--查看满足ID为1的数据
select id,number,sex,name from my_student where id=1;

write picture description here

Update data format:

update 表名 set 字段=值[where 条件];
--建立都加上where,不然就是更新全部

– Update the data, remember that the update is not necessarily successful. If there is no real data to be updated, the meaning of the update is to see if there is any data impact.

select * from my_student;
update my_student set sex = 'female' where name='jim';
select * from my_student;

write picture description here
– Deletion of data, deletion is irreversible, deletion is risky, and operations should be cautious.

delete from 表名 [where 条件];

For example to remove male members:

select * from my_student;
delete from my_student where sex='male';
select * from my_student;

write picture description here

Chinese data problem:
The Chinese data problem is essentially a character set problem. Computers only recognize binary, while humans are more aware of symbols, and there needs to be a corresponding relationship between binary and characters (character set).

– Insert a set of Chinese data

insert into my-student values (5,'itcast0005','薛飞龙',‘男’);

An error will be reported, the client fails to insert Chinese data into the server.
write picture description here
Reason: \xA3\x9E\xE9\xBE\x99 represents the hexadecimal converted from the corresponding binary code of "Xue Feilong" under the current code (character set). Two Chinese characters == "four bytes (GBK).
Error: The server does not recognize the corresponding four bytes, the server thinks the data is utf8, a Chinese character has three bytes, read three bytes into Chinese characters (failure), and read the remaining three bytes (obviously is not enough), and ultimately fails.
All database servers think (expressed) that some particularity is stored through server-side variables, and the system reads its own variables first and then sees how it behaves.
1. //Check which character sets are recognized by the server.

show character set;
查看所有字符集。

write picture description here

Basically, the server is omnipotent for any character set it supports.
2. //Since the server recognizes so many, there is always a character set that the server deals with by default with the client.

show variables like 'character_set%';
查看服务器默认的对外处理的字符集。

write picture description here

A simple explanation for each line:
1. The server defaults to the character set UTF8 of the data from the client.
Second, the connection layer character set.
3. The character set of the current database.
Fourth, the file system.
5. The server's default character set for external data.
Eight, character set processing.
The source of the error problem: the client can only be GBK, and the server thinks it is UTF8, and a contradiction occurs.
write picture description here

Solution: Change the server to accept GBK by default.

set character_set_client=gbk;

Modify the character set of client data that the server considers to be GBK.
write picture description here

However, after inserting Chinese, it will still be garbled, but it will not report an error
. Reason: the data source is the server, and the parsing data is the client (the client only recognizes GBK, only two bytes and one Chinese character), but in fact the data given by the server is utf8 , three bytes and one Chinese character, so it will cause garbled characters.

Solution: Modify the data character set of the server to the client to GBK.

set character_set_results=gbk;
修改给定的数据字符集为GBK。

write picture description here

View data effects:
write picture description here

set variable = value; the modification is only at the session level (current client, the current connection is valid, and the shutdown is invalid) . When
the view is closed, it changes back to the original UTF8. We can use a shortcut to set the server's perception of the client's character set

set names 字符集;

set names gbk; will change three positions.
1, character_set_client
2, character_set_results
3, character_set_connection

快捷设置字符集
set names gbk;

Three pieces of information have been changed as shown.
write picture description here
Connection connection layer: It is the middleman of character set transformation. If unified, it is more efficient, and there is no problem in disagreeing.

Proof set problem:
proof set: way of data comparison
Proof set has three formats
_bin:binary, binary comparison, take out the comparison of binary bits one by one, case-sensitive.
_cs: case sensitive, case sensitive, case sensitive.
_ci: case insensitive, case insensitive, case insensitive.

Check out the collation sets supported by the database:

show collation;
查看所有校对集

write picture description here
Proof set application: The proof set will only take effect when the data is compared.
write picture description here

Comparison: Use UTF8's _bin and _ci to verify the effect of different proofreading sets.

--创建不同的校对集
create table my_collate_bin(
name char(1)
)charset utf8 collate utf8-bin;

create table my_collate_ci(
name char(1)
)charset utf8 collate utf8_general_ci;

--插入数据
insert into my_collate_bin values ('a'),('A'),('B'),('b');
insert into my_collate_ci values ('a'),('A'),('B'),('b');

--查看
select * from my_collate_bin;
select * from my_collate_ci;

write picture description here
Compare, sort according to a field: order by field name [asc|desc]; asc ascending, desc descending, the default is ascending.
– sort search

select * from my_collate_bin order by name;
区分大小写,顺序按照ascll码顺序改变。
select * from my_collate_ci order by name;
不区分大小写,不改变。

write picture description here

Proof set: It must be declared before there is no data. If there is data, and then modify the proof set, the modification will be invalid.
– Modify the proof set after data is available

alter table my_collate_ci collate=utf8_bin;
select * from my_collate_ci order by name;查看时候还是没有变化。

write picture description here

Let's change it back:

alter table my_collate_ci collate=utf8_general_ci;

write picture description here

Garbled code problem:
WEB garbled code problem:
Dynamic network consists of three parts: browser, Apache (Apache) server (PHP), database server, all three parts have their own character set (Chinese), data needs to be in three parts Passing back and forth between them is easy to generate garbled characters.
Solution: Unified encoding (three codes in one)
but in fact impossible: the browser is user management (it is impossible to control at all), but these problems must be solved, mainly relying on PHP operation. The diagram shows:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652547&siteId=291194637