Two, two, MySQL syntax one (create database && user && show processlist && kill process)
- 2.1, MySQL field type (numeric type && string type && date type)
- 2.2, SQL type in MySQL && create table && set Unique_key
- 2.3, table building specifications
- 2.4, the character set in MySQL
- 2.5, data addition, deletion, modification, check && matters needing attention
3. Interview questions && homework for this course
1. The last course review
- https://blog.csdn.net/SparkOnYarn/article/details/104876632
- Soft connection (software upgrade, log migration), log migration scenario, the disk burst, move the data of the 50G hard disk to the 2T hard disk, and ln -s makes the soft connection.
Second, MySQL syntax one (create database && user && show processlist && kill process)
-
Create Database && User:
create database ruozedata; grant all privileges on *.* to 'root'@'%' identified by '960210'; flush privileges;
- For us, in big data, tables are the most used, and other developers use the rest of the views, indexes, and stored procedures.
-
Kill process in MySQL, log in using terminal
1、命令行不能带有密码,否则history查看会有泄露 hadoop:mysqladmin:/usr/local/mysql:>mysql -u root -p960210 mysql> hadoop:mysqladmin:/usr/local/mysql:>mysql -u root -p 960210 Enter password: 2、show processlist; 查看正在运行的进程: mysql> show processlist; +----+------+--------------------+-----------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+--------------------+-----------+---------+------+----------+------------------+ | 9 | root | 122.96.50.84:35845 | ruozedata | Sleep | 716 | | NULL | | 10 | root | 122.96.50.84:36362 | NULL | Sleep | 692 | | NULL | | 14 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+------+--------------------+-----------+---------+------+----------+------------------+ 3 rows in set (0.00 sec) 3、因为此时我们正在使用海狸连接,我们断开dbeaver连接,再次查看进程发现已经少了两个进程了。 mysql> show processlist; +----+------+-----------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+----------+------------------+ | 14 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+------+-----------+------+---------+------+----------+------------------+ 1 row in set (0.00 sec) 4、kill id杀掉进程 mysql> kill 15; Query OK, 0 rows affected (0.00 sec) mysql> kill 16; Query OK, 0 rows affected (0.00 sec) 5、注意:看时间,找到哪个消耗时间长的,有可能导致mysql的服务夯住,别导致锁死 --》 这个SQL的目的一定要搞清楚,到底能不能被kill掉。
2.1, MySQL field type
1. Value type:
Types of | Definition |
---|---|
int | Integer |
long | Long |
double | Double precision |
float | Single precision |
decimal | Decimal value (linked to amount) |
2. String type:
Types of | Definition |
---|---|
char | Fixed length, 0-255 bytes, such as an English letter a, it will automatically fill up 255 bytes when it takes up storage, wasting storage space |
varchar | Variable length, string, storage 0-65535 bytes, such as an English letter b, how much storage space it takes up |
3. Date type:
Types of | Definition |
---|---|
date | Date type, YYYY-MM-DD |
time | Time HH: MM: SS |
datetime | Year, month, day, hour, minute, and second, YYYY-MM-DD HH: MM: SS |
timestamp | Year, month, day, hour, minute, and second, YYYY-MM-DD HH: MM: SS |
Assignment: What is the difference between datetime and timestamp?
2.2, SQL type in MySQL & create data table
DDL: create drop data definition language
DML: insert update delete select Add delete and change check, data operation language
DCL: grant data control language
1、规范的创建rzdata数据表,关键最后5行
create table rzdata(
id int(10) not null auto_increment, //id不为空自增长
name varchar(30),
age int(3),
createuser varchar(200),
createtime timestamp not null default current_timestamp, //创建时间不为空且默认为当前时间
updateuser varchar(200),
updatetime timestamp not null default current_timestamp on update current_timestamp, //修改时间也不为空,当数据那一刹那发生改变就会留下一个记录时间。
primary key (id) //id做主键
);
2、数据准备:
insert into ruozedata.rzdata(name,age,createuser,updateuser) values ('john',24,'sail','sail')
3、测试对这条数据进行更新:
update ruozedata.rzdata set age=18 where name='john';
- Modify the age of john to 18, there will be a record:
2. Set the unique constraint of name so that data with the same name cannot be inserted:
graphical interface operation:
- Open the layer by layer, click on the constraint, right-click to create a new constraint, select the name field, set it to unique_key, click OK, there is a save in the lower corner, you need to click to execute.
insert into ruozedata.rzdata(name,age,createuser,updateuser) values ('john',24,'sail','sail');
insert into ruozedata.rzdata(name,age,createuser,updateuser) values ('john',25,'fox','fox');
出现错误提示:
SQL 错误 [1062] [23000]: Duplicate entry 'john' for key 'rzdata_un'
//下次遇见这个错误就应该能够明白:是设置了唯一值,所以值不能被重复。
- Every data of J head office has createuser, updateuser and upadatetime,
2.3, MySQL table building specification
a. The table name and field name cannot be Chinese or Pinyin; a unified style is needed
. b. A unified style: for example, when you build a table, you need to see what the style of the existing table is: take a field creator as For example, there are many ways to write: create_user, cuser, createuser; if you are not sure, you can take a leader check.
When the time comes, the style of the new project is by you. For
example: take the creation time as an example: ctime, createtime, cretime, cre_time, create_time;
c. The first field of the MySQL table is id self-increase, set as the primary key, it is It doesn't make sense; for mysql to be able to perform high-performance queries.
d. A table can only have one primary key: primary key ==> id, the primary key field must be unique, unique + not null
e. The last four fields need to be added: createuser, createtime, updateuser, updatetime; whether this record is Whether to add or modify is intuitive and clear.
f. Double-click "Column" under the table, and then pull to the end, there will be a comment column, add the comment, intuitively understand the meaning of each field, and finally click Save.
Interview question: Comparison of self-increasing primary key and UUID method
2.4, MySQL character set
1. Show the table-building statement for creating a database. The character set was not set when the database was created. The default setting is the latin character set:
- show create database ruozedata;
- CREATE DATABASE
ruozedata
/*!40100 DEFAULT CHARACTER SET latin1 */
// The database database has a character set, the table table has a character set, and the column column also has a character set.
2. The server also has a character set:
-
show variables like ‘%char%’;
character_set_client utf8 character_set_connection utf8 character_set_database latin1 character_set_filesystem binary character_set_results character_set_server latin1 character_set_system utf8 character_sets_dir /usr/local/mysql-5.7.11-linux-glibc2.5-x86_64/share/charsets/
2.5, MySQL addition, deletion and modification check
insert into ruoedata.rzdata(name,age,createuser,updateuser) values (‘john’,24,‘root’,‘root’);
select * from ruozedata.rzdata;
update ruozedata.rzdata set age = 19 where name = ‘john’;
delete from ruozedata.rzdata where name = ‘john’;
Note: Remember not to use * in production, write a limit 10;
select * from rzdata; // Remember to write sql in production and do not add *
select id, name, age from rzdata;
- In the financial system, delete from rzdata without the where condition will cause all data to be deleted; restore it through the binlog file (arch / mysql-bin).
In the future, whether it is update or delete, do not add where conditions, 1 or more data, createtime> = '2019-01-01 00:00:00'
Example: The data table has 100 million data, select * will result in failure to provide external services, select id, name, age from rzdata limit 3; Use limit 10 to view sample data
How does MySQL work in production:
- MySQL's master-slave replication, the master library in production should do write operations as much as possible, and the slave library performs write operations.
- Business ERP APP-> MySQL on server A: write-> data will be automatically synchronized to MySQL on server B: read; this is called read-write separation.
- Confirm clearly where to use select and delete.
3. Interview questions && homework for this course
Interview questions
1. What is the difference between datetime and timestamp?
2. What should I do if the id self-growth primary key is exceeded?
3. What is ENGINE = InnoDB? What is the difference with MyISAM
operation
1. Create a MySQL table specification
2. Sort out DDL and DML statements and execute them in sequence
3. Organize the three sentences, modify the password, grant permissions, refresh permissions