码迷,mamicode.com
首页 > 数据库 > 详细

centos7 安装install_mysql5.7网络教程安装_无报错.sh

时间:2020-07-06 10:25:57      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:ram   ESS   job   general   unit   efault   占用   exp   文章   

标题

#!/bin/bash



#uninstall system installed mysql(rpm version)
rpm -qa |grep mysql
rpm -e --nodeps *mysql*
rpm -qa |grep mariadb
rpm -e --nodeps mariadb*

#create folder
mkdir /mysql
mkdir -p /mysql/app
mkdir -p /mysql/data/3306
mkdir -p /mysql/log/3306
mkdir /backup

cd /usr/local/src/
wget http://download.zhufunin.com/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.26-linux-glibc2.12-x86_64 /mysql/app
mv /mysql/app/mysql-5.7.26-linux-glibc2.12-x86_64 /mysql/app/mysql

echo "export PATH=$PATH:/mysql/app/mysql/bin/" >> /etc/profile
source /etc/profile
#create mysql user and group

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

cat >> /mysql/data/3306/my3306.cnf <<EOF 

[client]
default-character-set=utf8
socket=/mysql/data/3306/mysql.sock
[mysqld]
#skip-name-resolve
port = 3306
socket = /mysql/data/3306/mysql.sock
basedir = /mysql/app/mysql
datadir= /mysql/data/3306/data
character-set-server=utf8
skip-character-set-client-handshake=1
default-storage-engine=INNODB
innodb_buffer_pool_size = 200M
max_allowed_packet = 16M
explicit_defaults_for_timestamp=1
log-output=FILE
general_log=0
general_log_file=/mysql/log/3306/general.log
slow_query_log = ON
slow_query_log_file = /mysql/log/3306/slow-query.log
long_query_time = 10
log-error = /mysql/log/3306/error.log
EOF

chown -R mysql:mysql /mysql/

#ln -sf /mysql/data/3306/my3306.cnf /etc/my.cnf

#initialize mysql,if initialize failed need delete data folder, password can find in /mysql/log/3306/error.log

/mysql/app/mysql/bin/mysqld --defaults-file=/mysql/data/3306/my3306.cnf --initialize --user=mysql --basedir=/mysql/app/mysql/ --datadir=/mysql/data/3306/data/ 


cat >> /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/mysql/app/mysql/bin/mysqld --defaults-file=/mysql/data/3306/my3306.cnf
LimitNOFILE = 65536
LimitNPROC = 65536

EOF

chmod 755 /usr/lib/systemd/system/mysqld.service


#systemctl daemon-reload
#systemctl stop mysqld
#systemctl start mysqld
#systemctl enable mysqld
#systemctl status mysqld



changerootpwd_DEMO () {
# will have error, if not
ln -s /mysql/data/3306/mysql.sock /tmp/mysql.sock
 mysql -uroot -p -S /mysql/data/3306/mysql.sock
#insert password
ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘Dreamfuture88‘;
exit;
}


manual_start-stop_mysql_DEMO () {
nohup /mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my3306.cnf >/dev/null 2>&1 &
#method1
mysqladmin -uroot -p shutdown -S /mysql/data/3306/mysql.sock
#method2
mysqladmin -uroot -p shutdown -S /mysql/data/3306/mysql.sock -pDreamfuture88
}


allowrootremote_DEMO () {
mysql -u root -p
#insert passowrd
use mysql;
select host,user from user where user=‘root‘;
create USER ‘root‘@‘%‘ IDENTIFIED BY ‘Dreamfuture88‘;
grant all privileges on *.* to ‘root‘@‘%‘ with grant option;

GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘116.15.151.147‘ IDENTIFIED BY ‘netCrownloto88‘ WITH GRANT OPTION;
exit;

}


create_database () {

create database itpuxdb DEFAULT CHARSET utf8mb4;
create database itpuxdb DEFAULT CHARSET utf8mb4 default collate utf8mb4_general_ci;

}


create_user () {

create user ‘itpux‘@‘%‘ IDENTIFIED BY ‘itpux‘;

}

check_user () {

select host,user from mysql.user;

}


grant_privileges () {

grant all privileges on itpuxdb.* to ‘itpux‘@‘%‘ IDENTIFIED by ‘itpux‘;
grant all privileges on itpuxdb.* to ‘itpux‘@‘localhost‘ IDENTIFIED by ‘itpux‘;

}

create_tables () {

use itpuxdb;
create table itpux01(
id int auto_increment primary key,
name varchar(15)
) engine = InnoDB;

}


insert_data () {

INSERT INTO itpux01 VALUES (1, ‘fgedu01‘);
INSERT INTO itpux01 VALUES (2, ‘fgedu02‘);
INSERT INTO itpux01 VALUES (3, ‘fgedu03‘);
INSERT INTO itpux01 VALUES (4, ‘fgedu04‘);
INSERT INTO itpux01 VALUES (5, ‘fgedu05‘);
COMMIT;

}




check_data () {

select * from itpuxdb.itpux01;
use itpuxdb;
select * from itpux01;
}


mysqldump_backup () {
#mysqldump Error binlogging on server not active, no need parameter --single-transaction --master-data=2 --flush-logs
mysqldump -uroot -pDreamfuture88  --routines --databases itpuxdb > itpuxdb.sql

mysqldump -uroot -pDreamfuture88 --single-transaction --master-data=2 --routines --flush-logs --databases itpuxdb > itpuxdb.sql

}

mysqldump_recovery () {

mysql -uroot -pDreamfuture88 < itpuxdb.sql

mysqldump --help

}

mysqlpump_backupall_DB () {
mysqlpump -uroot -pDreamfuture88 --single-transaction --default-character-set=utf8mb4 --default-parallelism=2 --all-databases --users > itpux_alldb_01.sql 

}

mysqlpump_recovery () {

mysql -uroot -pDreamfuture88 < itpux_alldb_01.sql

}




mysql_command () {

show databases;
show databases like ‘itpuxdb‘;
show databases like ‘it%‘;

create database if not exists itpux1 default character set UTF8 default collate utf8_general_ci;
#pure number database name need use ` `
create database `1111`;
show create database itpux1;
alter database itpux2 character set gbk;

drop database if exists `111`;

mysqladmin create itpux1 -uroot -pDreamfuture88
mysqladmin drop itpux1 -uroot -pDreamfuture88

create table itputx2(id01 tinyint);
desc itpux1;
insert into itputx2 values(‘-200‘); //-200会报错

案例一
create table itpux01(id01 tinyint,id02 int);
insert into itpux01 values(100,101);
insert into itpux01 values(1,12);
insert into itpux01 values(-1,12);
select * from itpux01;
案例二
create table itpux02(id01 tinyint(3) unsigned zerofill,id02 int(3) unsigned zerofill );;
desc itpux02;
insert into itpux02 values (1,1);
select * from itpux02;
insert into itpux02 values(12,1234);
案例三
创建一个没有格式控制的float
create table itpuxf1(f1 float, f2 double);
desc itpuxf1;
insert into itpuxf1 values(-12.123,-1234.5678);
insert into itpuxf1 values(-12.123,-1234.5678);
select * from itpuxf1;
案例四
创建一个有格式控制的表
create table itpuxf2(f1 float(5,2) zerofill,f2 double(6,3) zerofill);
desc itpuxf2;
insert into itpuxf2 values (12.34,12.34);
insert into itpuxf2 values (1.1,1.2);
insert into itpuxf2 values (123.45,123.456);
select * from itpuxf2;
+--------+---------+
| f1     | f2      |
+--------+---------+
|  12.34 |  12.340 |
|  01.10 |  01.200 |
| 123.45 | 123.456 |
+--------+---------+

科学计数法(E),小数点移动几位
insert into itpuxf2 values(0.1234E2,0.123456E3);
select * from itpuxf2;
+--------+---------+
| f1     | f2      |
+--------+---------+
|  12.34 |  12.340 |
|  01.10 |  01.200 |
| 123.45 | 123.456 |
|  12.34 | 123.456 |
+--------+---------+

插入多了,就会四舍五入
insert into itpuxf2 values (12.126,12.34);
select * from itpuxf2;

+--------+---------+
| f1     | f2      |
+--------+---------+
|  12.34 |  12.340 |
|  01.10 |  01.200 |
| 123.45 | 123.456 |
|  12.34 | 123.456 |
|  12.13 |  12.340 |
+--------+---------+

案例五,decimal 类型定点数,zerofill,高精度
create table itpux03(f decimal(10,4) unsigned zerofill);
insert into itpux03 values (123.45);
insert into itpux03 values (123.4567);
insert into itpux03 values (123.45678);
select * from itpux03;

+-------------+
| f           |
+-------------+
| 000123.4500 |
| 000123.4567 |
| 000123.4568 |
+-------------+

案例六,decimal 类型定点数,没有zerofill
create table itpux04(f decimal(10,4));
insert into itpux04 values (123.45);
insert into itpux04 values (123.4567);
insert into itpux04 values (123.45678);
select * from itpux04;


}

char_leixing () {

tinyint  -128~127, 0~255,占一个字节,取值3位

smallint  -32768~32767, 无符号,0~65535 占2个字节,取值5位

mediumint -8388608~8388607 无符号,0~16777215 占3个字节,取值8位

int       -2147483648~2147483647, 无符号, 0~4294967295,占用4个字节,取值11位
int[(M)][unsigned][zerofill]

bigint   有符号-20位~20位,无符号,0~20位,占8个字节,取值20位

M 取值范围:int(5) 取值5位, 表示最小宽度是多少
[unsigned] 无符号,控制是否有正负数,如果是正数,那就取值是0~255,如果是负数,那就是-128~127
[zerofill] 用来进行行前导零填充。001,如果类型后面写了zerofill, 默认就是 unsigned 无符号


decimal (M,D)-- (numeric) 
非压缩浮点数,不能是无符号的,每个小数对应一个字节,M 代表长度,D代表小数位数
存储空间,变长,大致是每9个数字,采用4个字节存储,整数和分数分开计算
M,最大是65
D,最大是30
默认是10,2
最小: -9E+65
最大: 9E+65

float (M,D) 
单精度:不能使用无符号的浮点数字, M 代表长度,D代表小数位数,值10,2 一共10位,其中2位小数,占4个字节
不写(M,D) , 自动处理M为7左右
最小值:-3.40E+38
最大值: 3.4E+38

double (M,D) 
双精度:不能使用无符号的双精度浮点数据, M 代表长度,D代表小数位数,值10,2 一共10位,其中2位小数,占8个字节
不写(M,D),自动处理M为15左右
最小值:-1.790E+308
最大值: 1.790E+308


}

date-format () {

datetime (年月日时分秒)
create table itpuxd1 (mydate datetime);
insert into itpuxd1 values(‘20180305103030‘);
insert into itpuxd1 values(20180305103030);
insert into itpuxd1 values(‘2018-03-05 10:30:30‘);
select * from itpuxd1;
+---------------------+
| mydate              |
+---------------------+
| 2018-03-05 10:30:30 |
| 2018-03-05 10:30:30 |
| 2018-03-05 10:30:30 |
+---------------------+


timestamp 用整数,字符串都可以存储
create table itpuxd2 (t timestamp);
insert into itpuxd2 values (‘20180305103030‘);
insert into itpuxd2 values (‘2018-03-05 10:30:30‘);
select * from itpuxd2;
+---------------------+
| t                   |
+---------------------+
| 2018-03-05 10:30:30 |
| 2018-03-05 10:30:30 |
+---------------------+

+0 查看时间戳,显示整数
select t+0 from itpuxd2;
+----------------+
| t+0            |
+----------------+
| 20180305103030 |
| 20180305103030 |
+----------------+


date (年月日)
create table itpuxd3 (t date);
insert into itpuxd3 values (‘20180305‘);  //推荐格式
insert into itpuxd3 values (‘2018-03-05‘); //推荐格式
insert into itpuxd3 values (‘2018:03:05‘);
insert into itpuxd3 values (‘18:03:05‘);
insert into itpuxd3 values (‘80:03:05‘);
insert into itpuxd3 values (‘20180305103030‘); //只会显示年月日
select * from itpuxd3;
+------------+
| t          |
+------------+
| 2018-03-05 |
| 2018-03-05 |
| 2018-03-05 |
| 2018-03-05 |
| 2018-03-05 |
| 1980-03-05 |
+------------+


time (时间)D HH:MM:SS D代表天,最大代表34天,代表过去多少时间
create table itpuxd4 (t time);
insert into itpuxd4 values (‘10:30:30‘); 
insert into itpuxd4 values (‘5 10:30:30‘); 
select * from itpuxd4;
+-----------+
| t         |
+-----------+
| 10:30:30  |
| 130:30:30 |
+-----------+


year YYYY

create table itpuxd5 (`y` year);
insert into itpuxd5 values (1234); //会报错
insert into itpuxd5 values (2018);
insert into itpuxd5 values (2016);
select * from itpuxd5;
+------+
| y    |
+------+
| 2018 |
| 2016 |
+------+


}

char_type () {

字符类型

--char(M) M 代表字符固定长度,255个字节

--varchar(M) M表示字符可变长度,65536个字节,需要1~2个字节保存信息,超过255的长度就是用2个字节来保存

utf8: 一个字符占用3个字节 65535/3=21845 -1~2=21844/21843
gbk: 一个字符占用2个字节 65535/2=32767 -1~2=32767/32765

最大长度是最大65535字节和所使用的字符集有关

create table itpuxz1 (c CHAR(255));
insert into itpuxz1 values(‘Yes‘);
insert into itpuxz1 values(‘No‘);
insert into itpuxz1 values(‘Y‘);
insert into itpuxz1 values(‘Y  ‘);
select * from itpuxz1;

+------+
| c    |
+------+
| Yes  |
| No   |
| Y    |
| Y    |
+------+


create table itpuxz2(c varchar(65535)); \\错误
create table itpuxz2(c varchar(21845)); \\错误
create table itpuxz2(c varchar(21844));
desc itpuxz2;
insert into itpuxz2 values(‘Yes‘);
insert into itpuxz2 values(‘No‘);
insert into itpuxz2 values(‘Y‘);
insert into itpuxz2 values(‘Y  ‘);
select * from itpuxz2;
select c,LENGTH(c) from itpuxz2;


--text

text 和 blob 区别,blob用来保存二进制数据,text保存字符数据
text 和 char/varchar 区别,text不需要指定存储长度
存储长度:1字节~4GB
tinytext: 256 bytes,1字节开销,少于255个字符,就比较好,比如,文章摘要
text: 65535 bytes, 最大64K, 相当于65535个字符,2字节开销,比如文章正文
mediumtext 最大16MB ,相当于16777215, 3个字节开销,存储相对大的文本数据
longtext 最大4GB

create table text1 (id int, name tinytext);


--blob
tinyblob 最大255字节
blob 最大65K
mediumblob 最大16K
longlob 最大4G

create table blobtest (id int, name blob);

--enum
最多65535 个枚举项,2个字节的消耗,
create table itpuxe1(e enum(‘Y‘,‘N‘));
insert into itpuxe1 values(‘Y‘);
insert into itpuxe1 values(‘N‘);
insert into itpuxe1 values(‘1‘);
insert into itpuxe1 values(‘2‘);
insert into itpuxe1 values(‘Y1‘); \\出错
select * from itpuxe1;
select e+0 from itpuxe1;


--set
集合,相当于多个选择题
create table itpuxs1(s set(‘A‘,‘B‘,‘C‘,‘D‘));
insert into itpuxs1 values (‘A‘);
insert into itpuxs1 values (‘A,B‘);
insert into itpuxs1 values (‘C‘);
insert into itpuxs1 values (‘C,D‘);
insert into itpuxs1 values (‘C,E‘); \\出错
select * from itpuxs1;

}

create_tables () {

--创建表
需要信息:表名,表字段名,表字段的定义(什么类型)

create table table_name 列定义 选项
create table table_name like old_table_name; \\like : 包括旧表的结构+信息+索引
create table table_name as select * from old_table_name; \\ as 包括旧表的结构+信息

通用语法:
create table table_name (colume_name columa_type);

案例创建表一
create table itpuxt1 
(
    id int(20) unsigned auto_increment not null,
    name varchar(20) not null,
    jobdate date,
    primary key(id)

)engine=InnoDB default charset=utf8;
desc itpuxt1;
show create table itpuxt1;

show tables;
show tables from db_name;
show tables like ‘关键字‘;


}

select_format () {

select * from tables where dname = ‘it‘;
select * from tables limit 2; \\查看两行
select count(*) from tables where dname = ‘it‘;

}

creat_insert_delete () {

create table itpux11(id int(3), name varchar(12), sex varchar(6));
create table itpux12(id int(3), name varchar(12), age int(5));
insert into itpux11 values(1,‘itpux01‘,‘男‘);
insert into itpux11 values(2,‘itpux02‘,‘男‘);
insert into itpux11 values(3,‘itpux03‘,‘女‘);
insert into itpux11 values(4,‘itpux04‘,‘女‘);
insert into itpux11 values(5,‘itpux05‘,‘女‘);
commit;

insert into itpux12 values(1,‘itpux01‘,‘20‘);
insert into itpux12 values(2,‘itpux02‘,‘21‘);
insert into itpux12 values(3,‘itpux03‘,‘18‘);
insert into itpux12 values(4,‘itpux04‘,‘19‘);
insert into itpux12 values(5,‘itpux055‘,‘28‘);
commit;

select * from itpux11;
select * from itpux12;

语法: insert into table_name(表字段) values(值列表);
insert into itpux11(id,name,sex) values (6,‘itpux06‘,‘男‘);
insert into itpux11 values (7,‘itpux07‘,‘男‘);
select * from itpux11;
insert into itpux11 set id=8,name=‘itpux08‘,sex=‘男‘;

delete from itpux11 where sex is NULL;


insert into itpuxt1 (id,name,jobdate,age2)values(1, ‘itpux01‘, NOW(),20);

insert into itpuxt1 values(2, ‘itpux02‘,default,20);

create table itpux13 select * from itpux11;
truncate table itpux13;
select * from itpux13;
insert into itpux13 select * from itpux11;

}

update_format () {
update 表名 set 列名=值 where 条件;
update inpux13 set name = ‘itpux11‘ where id=1;
}

delete_format () {
delete from 表名 [where 条件] [order by] [limit ROW_COUNT]
delete from itpux13 where id=1;
delete from itpux13 limit 2;
delete from itpux11 limit 1;

select * from itpux11;

select * from itpux12;
select * from itpux11 inner join itpux12 using (id,name);
delete from itpux11,itpux12 using itpux11 inner join itpux12 using (id,name) where id=2;

select * from itpux11 order by id desc limit 3;
delete from itpux11 order by id desc limit 3;

}

select_ () {

select 语法:

select 字段/表名 from 表名/视图名 where 查询条件;

查询条件
1)where 条件
2) group by 分组
3) having 分组后再聚合
4) limit 限制多少行
5) order by [asc |desc] 排序 正、反

表达式
1) 变量
select 10+20;
+-------+
| 10+20 |
+-------+
|    30 |
+-------+
select NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2019-05-21 09:10:38 |
+---------------------+

select NOW(),UNIX_TIMESTAMP();

+---------------------+------------------+
| NOW()               | UNIX_TIMESTAMP() |
+---------------------+------------------+
| 2019-05-21 09:10:27 |       1558444227 |
+---------------------+------------------+

2) from 子句

select * from yg;
select email from yg;
select email, job_id from yg;
select * from yg,bm;
select * from itpux11,itpux12;
select * from itpux11 a1,itpux12 a2 where a1.name=a2.name; \\a1, a2是别名

-- 列连接

select name, CONCAT(name,‘-‘,sex) from itpux11;
+---------+----------------------+
| name    | CONCAT(name,‘-‘,sex) |
+---------+----------------------+
| itpux01 | itpux01-男          |
| itpux03 | itpux03-女          |
| itpux04 | itpux04-女          |
| itpux05 | itpux05-女          |
| itpux06 | itpux06-男          |
| itpux07 | itpux07-男          |
| itpux08 | itpux08-男          |
+---------+----------------------+

3) 别名,as/也可以省略
select * from itpux11 a1,itpux12 a2;
select name, CONCAT(name,‘-‘,sex) as xingming from itpux11;
select name, CONCAT(name,‘-‘,sex) as ‘姓名和性别‘ from itpux11;
+---------+-------------+
| name    | xingming    |
+---------+-------------+
| itpux01 | itpux01-男 |
| itpux03 | itpux03-女 |
| itpux04 | itpux04-女 |
| itpux05 | itpux05-女 |
| itpux06 | itpux06-男 |
| itpux07 | itpux07-男 |
| itpux08 | itpux08-男 |
+---------+-------------+

4)虚拟表dual

select NOW() from dual;

5) SQL 语句注释方式

#select NOW() from dual; 
select NOW() from dual; -- 查询当前系统时间,后面接 --

select NOW() /* date */ from dual; -- 用/* 注释内容 */

/*
select NOW() from dual;
select NOW() from dual;
*/
select NOW() from dual;

6) SQL语句编写规则

A.大小写不敏感
B. SQL可以占用多行
select * from itpux11
where
name=‘itpux01‘;

C. 关键字不能省略,要么折成多行
D. 任何SQL最后都加一个";"

select 运算符

常用的运算符:
= : id=15
> : id>15
< : id<15
>= : id>=15
<> : id <> 15
!= : id!=15
is null : address is null
is not null : 
like : 模糊查询 address like ‘北京%‘
not like : 模糊查询 address not like ‘北京%‘
between: 在什么范围内,id between 1 and 15
not between: 在什么范围内,id not between 1 and 15
in: 在什么范围值内, id in (1,2,3,5,10),id not in (1,2,3,5,10)
not in:

select * from itpux12 where age=20;
select * from itpux12 where age!=20;
select * from itpux12 where age>20;
select * from itpux12 where age<20;
select * from itpux12 where age>=20;
select * from itpux12 where age>=20;
select * from itpux12 where age between 18 and 25;
select * from itpux12 where age not between 18 and 25;
select * from itpux12 where age>=18 and age <=25;

select * from itpux12 where age>=18 && age <=25;

select * from itpux12 where age in (18,19,28);
select * from itpux12 where age not in (18,19,28);
select * from itpux12 where name like ‘itpux%‘;

--逻辑运算
非 not, ! 非null. 为null
与 and , &&
或 or

select null is not null, null b is null;

+------------------+--------------+
| null is not null | null is null |
+------------------+--------------+
|                0 |            1 |
+------------------+--------------+

select null <=> null, 10 <=> null;

+---------------+-------------+
| null <=> null | 10 <=> null |
+---------------+-------------+
|             1 |           0 |
+---------------+-------------+

--组合
select * from itpux12 where name like ‘itpux0%‘;

select * from itpux11 where name =‘itpux01‘ or sex = ‘女‘;

select * from itpux11 where name is null;
select * from itpux11 where name is not null;

-- like
非精确查询 , (精确查询=)
A%, %A, %A% %A%C% A%C

 查询分组和排序

 group by
 order by

 group by 列 {asc升序,desc降序}, {with rollup} 组合聚合计算
 分组一般常用到的函数

 MAX: 最大值
 MIN: 最小值
 avg: 求平均
 sum: 列/表达式综合
COUNT: 行数总和


select * from itpux_sales;

select count(*) from itpux_sales;
select goods,count(*) from itpux_sales group by goods;

select goods,count(*) from itpux_sales group by 1 order by 2 asc;
select goods,count(*) from itpux_sales group by 1 order by 2 desc;
select goods,count(*) from itpux_sales group by 1 order by 2 desc limit 1;

slect SUM(salary) from yg;

select region, dept, sum(salary) from itpux_yg group by region,dept;

select region, sum(salary) from itpux_yg group by region order by 2;
select region, sum(salary) from itpux_yg where salary > 10000 group by region order by 2;

--with rollup

利用组合条件进行统计后,再次统计

select region, dept, sum(salary) from itpux_yg group by region,dept with rollup;

--order by 多列排序,默认以第一个字段排序

select * from itpux_yg order by jobid, age;

--limit 
select * from yg;
select * from yg limit 5;
select * from yg limit 3,4; -- 第三行之后的四行

select * from yg order by salary desc limit 1;

-- distinct

select sex from itpux_yg;
select distinct sex from itpux_yg;

-- all
 select sex from itpux11;
 select all sex from itpux11; -- 默认省略了all


 -- union
五重并集:把多个结果组合并去重,再以第一列的结果进行升序排序

select * from itpux11;
select * from itpux12;

select name from itpux11 union select name from itpux12;

+----------+
| name     |
+----------+
| itpux01  |
| itpux03  |
| itpux04  |
| itpux05  |
| itpux06  |
| itpux07  |
| itpux08  |
| itpux055 |
+----------+

-- union all
有重并集,把多个结果组合不去重,不排序
select name from itpux11 union all select name from itpux12;


-- for update
 会锁表,不允许其他人更新
select * from itpux11 for update;


}


learn_from_imooc () {


create database test;
show databases;
use test;
select database();
练习一
create table if not exists tb1(
    username varchar(20),
    age tinyint unsigned,
    salary float(8,2) unsigned
    );

show tables;
show columns from tb1;    
show tables from mysql;
insert tb1 values (‘tom‘,25,7863.25);
select * from tb1;
insert tb1(username,salary) values (‘john‘,4500.69);
select * from tb1;

练习二,设置not null
create table if not exists tb2(
    username varchar(20) not null,
    age tinyint unsigned null,
    salary float(8,2) unsigned
    );

show columns from tb2;

insert tb2 values (‘tom‘,null,1);

insert tb2 values (null,26,1); //报错

练习三,auto_increment, 设置主键primary key
create table if not exists tb3(
    id smallint unsigned auto_increment primary key,
    username varchar(20) not null
    );
show columns from tb3;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+

insert tb3(username) values(‘john‘);
insert tb3(username) values(‘rose‘);
insert tb3(username) values(‘tom‘);

练习五 unique key
create table if not exists tb5(
    id smallint unsigned auto_increment primary key,
    username varchar(20) not null unique key,
    age tinyint unsigned
    );
show columns from tb5;

insert tb5(username,age) values (‘tom‘,25);
insert tb5(username,age) values (‘tom‘,25);

练习六 default
create table if not exists tb6(
    id smallint unsigned auto_increment primary key,
    username varchar(20) not null unique key,
    sex enum(‘1‘,‘2‘,‘3‘) default ‘3);

show columns from tb6;
insert tb6(username) values (‘tom‘);
iselect * from tb6;

约束包括
not null 非空约束
primary key 主键约束
unique key 唯一约束
default 默认约束
foreign key外键约束

查看索引
show indexes from tb6;



插入新的tables 列, after,before,first,

alter table tb6 add password varchar(32) not null after username;
alter table tb6 add age tinyint unsigned not null default 10;
alter table tb6 add turename varchar(20) not null first;

添加多列, 不能定义位置,只能放最后
alter table tbl_name add (col_name01 column_definition, col_name02 column_definition,...);

删除列
alter table tbl_name drop col_name01,drop col_name02;


add 增加列,drop删除列

添加primary key
alter table tb6 add constraint primary key (id);

添加唯一约束
alter table tb_name add [constraint [symbol]] unique [index|key] [index_name] [index_type] 
添加外键约束
alter table users2 add forgeign key (pid) references provinces (id);

修改添加删除默认约束

alter table tbl_name alter [column] col_name set default 100; //修改和添加默认约束
alter table tbl_name alter [column] col_name set drop default; //删除默认约束

删除主键约束
alter table tab_name drop primary key; //因为主键有唯一性,所以不需要指定column

删除索引/约束
alter table tbl_name drop index index_name;

show indexes from tb6;
alter table tb6 drop index username;

删除外键约束foreign key
查看有外键约束的column
show create table tb6;
alter table tbl_name drop foreign key fk_symbol

修改某个column字段的位置,比如弄到第一位,注意最后面的first
修改column的位置的同时可以修改column的定义值
show create table tb6;
show columns from tb6;
alter table tb6 modify id smallint unsigned not null first;
alter table tb6 modify id smallint(5) unsigned NOT NULL AUTO_INCREMENT first;

修改列名称,change 旧名 新名
alter table tbl_name change old_col_name new_col_name column_definition [first|after col_name]
 alter table tb6 change age ages tinyint(3) unsigned NOT NULL DEFAULT ‘100‘ after username;






}

结尾

centos7 安装install_mysql5.7网络教程安装_无报错.sh

标签:ram   ESS   job   general   unit   efault   占用   exp   文章   

原文地址:https://www.cnblogs.com/faberbeta/p/13253349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!