mysql8学习心得

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_34789167/article/details/83418079

一:安装mysql

rpm安装

wget 'https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.3-0.1.rc.el7.x86_64.rpm-bundle.tar'

tar xvf mysql-8.0.3-0.1.rc.el7.x86_64.rpm-bundle.tar

sudo rpm -i mysql-community-{server-8,client,common,libs}* 

sudo yum install mysql-community-{server-8,client,common,libs}* -y

rpm -qa | grep -i mysql-community

启动关闭和重启mysql

    service mysqld start/stop/restart
    systemctl start/stop/restart mysqld

登录mysql

mysql -uroot -p

密码:5Ns)CM9)R#&k
grep "temporary password" /var/log/mysqld.log

change the root password

 alter user 'root'@'localhost' identified by 'new_password'

关闭mysql:

sudo service mysqld stop
sudo systemctl stop mysqld
mysqladmin -uroot -p shutdown

checking the status of the mysql server

sudo systemctl status mysqld
sudo /etc/init.d/mysql status

Uninstalling mysql8 :略
Downgrading from MySQL 8.0 略

Managing the mysql server with systemd

mysql Upgrading

Check for obsolete datatypes or triggers

sudo mysqlcheck -u root -p --all-databases --check-upgrade

image

更新表结构为InnoDB

ALTER TABLE table_name ENGINE = INNODB;

移除表分区
alter table table_name remove partitions;


二:Using Mysql

  1. connecting to mysql using the command-line client

mysql -h localhost -p 3306 -u <username> -p <password>

mysql --host=localhost --port=3306 --user=root--password

  1. to cancel a command:ctrl+c or type \c;

  2. Creating databases

graph LR
A[Database Server]-->B[Databases]
B-->C[Tables]
C-->D[Rows]

create database company

create database test
create database `my.contacts`;

3.1 use database

use test
use commany

3.2 query database

show databases

3.3 find which database you are connected to

select database();

3.4 find your current data directory

show variables like 'datadir';

image

image

  1. create table

4.1 datatype:

1. numberic: tinyint smallint meniumint int bigint and bit
2. Floating nummbers: decimal float double
3. string: char varchar binary varbinary blob text enum set
4. spatial datatypes 
5. The json datatype

The JSON datatype is a new extension

4.2 The table contains the column definition:

mysql> CREATE TABLE IF NOT EXISTS
`company`.`customers` (
`id` int unsigned AUTO_INCREMENT PRIMARY KEY,
`first_name` varchar(20),
`last_name` varchar(20),
`country` varchar(20)
) ENGINE=InnoDB;
mysql> CREATE TABLE `company`.`payments`(
`customer_name` varchar(20) PRIMARY KEY,
`payment` float
);

List all the tablesshow tables;

image

to see the structure of the table

show create table table_name;

image

desc table_name

image

Mysql creates .idb files inside the data direcctory;

image

Cloning table structure

create table new_table like old_table;

  1. Insert Update and Delete rows
    The insert update,delete and select opertions are called DML(Data Manipulation Language)

5.1 Insert statement is used to create new records in a table

mysql> INSERT IGNORE INTO
`company`.`customers`(first_name, last_name,country)
VALUES
('Mike', 'Christensen', 'USA'),
('Andy', 'Hollands', 'Australia'),
('Ravi', 'Vedantam', 'India'),
('Rajiv', 'Perera', 'Sri Lanka');

5.2 Updating

mysql> UPDATE customers SET first_name='Rajiv',
country='UK' WHERE id=4;

replace insert on duplicate key update

REPLACE INTO customers VALUES (1,'Mike','Christensen','America');

INSERT INTO payments VALUES('Mike Christensen', 200) ON DUPLICATE KEY UPDATE payment=payment+VALUES(payment);
 
INSERT INTO payments VALUES('Ravi Vedantam',500) ON DUPLICATE KEY UPDATE payment=payment+VALUES(payment);

 INSERT INTO payments VALUES('Mike Christensen', 300) ON DUPLICATE KEY UPDATE payment=payment+VALUES(payment);

Truncating tables 清空表

truncate table tablename;

Loading sample data

wget 'https://codeload.github.com/datacharmer/test_db/zip/master' -O master.zip

unzip master.zip

cd test_db-master

mysql -u root -p < employees.sql

SELECT Syntax

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      SQL_NO_CACHE [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
      [PARTITION partition_list]
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
    [HAVING where_condition]
    [WINDOW window_name AS (window_spec)
        [, window_name AS (window_spec)] ...]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED] 
      | LOCK IN SHARE MODE]]

Operators
image

TencentItemManageService.jar -> TencentItemManageService.1947572018092018806.jar

Simple pattern matching

  • Find the count of all employees whose firstname starts with Christ :

select count(*) from employees where first_name like 'Christ%';

select count(*) from employees where first_name rlike '^Christ';

  • Find the count of all employees whose first name starts with Christ and ends with ed :

select count(*) from employees where first_name like "Christ%ed";

  • Find the count of all employees whose first name contains sri :

select count(*) from employees where first_name like "%sri%";

  • Find the count of all employees whose first sname ends with er

select count(*) from employees where first_name like "%ed";
select count(*) from employees where first_name rlike "ed$";
select count(*) from employees where first_name regexp "ed$";

  • Find the count of all employees whose first name starts with any two characters followed by ka and then followed by any number of characters:

select count(*) from employees where first_name like "__ka%";

  • Find the count of all employees whose last name does not contain vowels (a, e, i, o, or u):

select count(*) from employees where last_name not regexp ‘[aeiou]’

select count(*) from employees where last_name not regexp '[aeiou]';

Limiting results

SELECT first_name, last_name FROM employees WHERE hire_date < '1986-01-01' LIMIT 10;

image

Using the table alias

select count(*) as count from employees WHERE hire_date BETWEEN '1986-12-01' AND '1986-12-31';

image

Sorting results

SELECT emp_no,salary FROM salaries ORDER BY salary DESC LIMIT 5;

select emp_no,salary from salaries order by 2 desc limit 5;

image

Grouping results (aggregate functions)

select gender,count(*) as count from employees group by gender;

image

  • find the 10 most common first names of the employees.

select first_name,count(*) as count from employees group by first_name order by count desc limit 10;

image

sum

select year(from_date),sum(salary) as sum from salaries group by year(from_date) order by sum desc;

image

select month(from_date)as month,sum(salary) as sum from salaries group by month order by month desc;

image

AVERAGE

Find the 10 employees with the highest average salaries:

select emp_no,avg(salary) as avg from salaries group by emp_no order by avg desc limit 10;

image

select emp_no,avg(salary) as avg from salaries group by emp_no order by avg desc limit 10;

image

DISTINCT 去重
select distinct title from titles;
image

  • find the employees with an average salary of more than 140,000

select emp_no,avg(salary) as avg from salaries group by emp_no having avg>140000 order by avg desc;

发布 因为使用的有道云笔记会员markdown格式,故图片显示不出,这里附上笔记链接:
http://note.youdao.com/noteshare?id=c109e307813fff8baa48bfc9c4904d39&sub=0D00DFD88C0E4AF2A9DF30C30F8401EE

猜你喜欢

转载自blog.csdn.net/sinat_34789167/article/details/83418079
今日推荐