Database operation problems that need attention

Some problems in this record database operations occur.

First, the insertion of Chinese garbage problem

If you insert data into a table with a cmd window when inserted data is Chinese, an error message appears, please ignore software operation. . .
The error occurs is cmd window is used gbk encoding, so you can enter data in the cmd window are gbk coding, and coded default database are utf8, so there is a coding problem.
We can enter the instructions to view the database-related code sets.

show variables like 'character%';

Here Insert Picture Description
You will see that all the relevant code set database are utf8.
So how to solve the garbage problem insert it?
The first thing we think of is to modify the coding insert data, but can not be modified encoding cmd window, so we should modify the client code, ie connection, client, results of these three codes.
We enter the instruction to modify the client code set.

set names gbk;

The instruction will also modify the above-described three coding.
After editing, we re-look at the code set.
Here Insert Picture Description
Successfully modified. We again revised Chinese data will not insert a problem. However, when we re-open a cmd window, insert the Chinese problem has emerged. Because the method just described just change the encoding of the current cmd window, only temporarily set the current code sets window.
Now I will introduce a method once and for all.
Found in the mysql installation directory my.ini file, which is the configuration file for the database, we open it.
So there is a sign in the 55th row, [mysql]
it for the following client configuration,
and the first 67 rows [mysqld]
is the server's configuration made up.
We just need to [mysql] under the default-character-set=utf8change default-character-set=gbkcan be.
Then we restart the mysql service.
First input

net stop mysql;

Then enter

net start mysql

Next, we logged on to mysql, view the code set, you will find success code set is modified, so insert garbled problem is solved.

Second, some of the problems the update statement updates

Let's create a table and initialize data.

create table demo(
    name varchar(20),
    money varchar(20)
);

insert into demo values('zs','1000');

Now, we modify the data.

update demo set money = '2000' where name = 'zs';

Here Insert Picture Description
This operation I believe we have no problem, but if I would write an update statement it?

update demo set money = '3000' where name = 'ZS';

Or is this also write it?

update demo set money = '4000' where name = 'zs         ';

This is what we run two sql statements.
Here Insert Picture Description
You will find these two sql statements into force, but many people think that these two sql statement is problematic, but it can run successfully.
In fact, this is a bug mysql, then, since this sql statement in question, How can we make mysql detected it?
We can write sql statement.

update demo set money = '2000' where binary name = 'zs';

Before comparing only need to add binary, we tested it.
Here Insert Picture Description
Such uppercase name on the modified unsuccessful.

Third, some of the problems delete statement deletes

When data is deleted on the table, we have two ways.
The first,

delete from 表名

The second,

truncate 表名

The same effect of these two statements, but they are different, the difference between what it?
Use truncate table statement to delete data, the deleted records are not recoverable from transaction management. How It Works: delete the entire table, and then re-created.
Use the delete statement to delete table data, they can be transaction management, and delete data in a transaction can be rolled back. Principle: line by line to delete the data record.
So, truncate over delete all records of performance, it is superior to delete the.

Fourth, some of the problems of the select statement to query

Let's create a table, then initialize the data.

create table stu(
    name varchar(20),
    math varchar(20),
    english varchar(20)
);

insert into stu values('张三','80','65');
insert into stu values('李四','85','75');
insert into stu values('王五','70','95');

And then query the table data.

select * from stu;

Here Insert Picture Description
This statement is all too familiar with it.
Then the next there are several needs.
1, on all English student scores 10 points points and inquiries expertise
we can do this.

select name,english + 10 from stu;

Here Insert Picture Description
2, statistics for each student scores

select name,math + english from stu;

Here Insert Picture Description
3, using the alias represents a student's grade
in student scores just query time, the column name always gives a very good feeling, so we can give the column name from a nickname.

select name,math + english as 总分 from stu;

Here Insert Picture Description
When a column name from the nickname, as this can be omitted, such as:

select name 姓名 from stu;

Here Insert Picture Description
So here we need to pay attention:

select name math from stu;
select name,math from stu;

Observe the above two sql statements, you will find only a comma difference, but the effect of the two statements is completely different.
The first statement is actually omitted as, its role: to query the student's name and the name of the column named math.
Here Insert Picture Description
The role of the second statement: query name and column math.
Here Insert Picture Description
Reminder: Because query, so all of the above statements will not have any impact on the original data.
There are a few places to be mentioned:
in the database, null behalf 1/2, so any expression and null logical operation results are false;
logic operations, and, or ------ who high priority?

select * from stu where 2 >1 or 2 >3 and 3 > 4;

Implementation of that article sql statement discovery data can be queried, and instructions executed first, and a higher priority than or.
select grouping queries introduction grouping function:
COUNT returns the number of records the query results
Syntax: select count (*) from table name;

the sum of a data summation
syntax: select sun (field names) from table;
when the sum summation, null is not involved in computing

avg averaging a data
syntax: select avg (field names) from table;

max / min seeking the maximum and minimum of a data
syntax: select max (field names), mini (field names) from table;

One final note: where the grouping function can not be used, so in order to add restrictions in the process of grouping queries, we should use the having, the same syntax and where.

Guess you like

Origin www.cnblogs.com/blizzawang/p/11411705.html