1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
| mysql端口3306 oracle端口1521 sqlserver端口1433
[root@localhost ~]
[root@localhost ~]
MariaDB [(none)]> \! hostname
MariaDB [mysql]> prompt \u@[\D \r:\m\s]
MariaDB [(none)]> show character set;
MariaDB [(none)]> show create database test;
MariaDB [mysql]> create database db3 character set utf8mb4;
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~] mysqld is alive
/*注释内容*/ --注释内容
MariaDB [(none)]> select user();
MariaDB [(none)]> select version();
MariaDB [(none)]> \h
MariaDB [(none)]> status
MariaDB [(none)]> show databases;
MariaDB [(none)]> use mysql
MariaDB [mysql]> show tables;
MariaDB [mysql]> desc user;
MariaDB [mysql]> show columns from user;
MariaDB [mysql]> select user,host,password from user;
MariaDB [mysql]> create table student ( -> stuid smallint unsigned auto_increment primary key, -> name char(10) not null, -> gender enum('m','f') default 'm', -> age tinyint unsigned,mobile char(11)); Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show create table student;
MariaDB [mysql]> drop table student;
MariaDB [mysql]> delete from mysql.student;
MariaDB [hellodb]> delete from teachers where name='yangyang';
MariaDB [mysql]> show table status like 'student'\G
MariaDB [mysql]> show table status from mysql\G
MariaDB [mysql]> create database db1;
MariaDB [mysql]> drop database db1;
MariaDB [mysql]> show databases;
MariaDB [db2]> select user,host,password from mysql.user;
创建user表,参考mysql.user中user,host,password三个字段 MariaDB [db2]> create table user select user,host,password from mysql.user;
MariaDB [db2]> create table user2 like mysql.user;
insert添加记录 MariaDB [mysql]> insert into student(stuid,name,gender,age,mobile)values(1,'alice','f',20,'13116527893'); MariaDB [mysql]> insert into student(age,name)values(22,'bob');
添加多条记录 MariaDB [mysql]> insert into student(name,age,mobile)values('xiaoming',18,'15116789319'),('xiaohong',14,'19087654288');
参考mysql.student表的结构建表 MariaDB [db2]> create table student like mysql.student;
修改表的字符集 MariaDB [db2]> alter table student character set utf8mb4;
[root@localhost ~] character-set-server=utf8mb4
[root@localhost ~]
MariaDB [mysql]> insert hello select * from student;
修改表update,第二条记录 MariaDB [mysql]> update student set name='aaa',mobile=10000 where stuid=2;
[root@localhost mysql] /etc/my.cnf.d/mysql-clients.cnf加入safe-updates
MariaDB [mysql]> delete from student where stuid=3;
MariaDB [mysql]> insert student(name,age,stuid)value('dd',23,3);
MariaDB [mysql]> select * from student;
MariaDB [mysql]> show tables;
MariaDB [mysql]> delete from hello; MariaDB [mysql]> truncate table db2.student;
[root@localhost ~]
MariaDB [hellodb]> select stuid,name,age from students;
MariaDB [mysql]> select host as 主机,user 用户,password 密码 from user;
where条件判断 MariaDB [hellodb]> select * from students where age<20;
MariaDB [mysql]> select * from students where gender = 'f';
MariaDB [hellodb]> select * from students where name like 's%';
MariaDB [mysql]> select * from students where name like '%s%';
MariaDB [hellodb]> select * from students where classid is null;
MariaDB [hellodb]> select * from students where classid is not null;
MariaDB [hellodb]> select * from students where age >=20 and age<=30;
MariaDB [mysql]> select * from students where age between 20 and 30;
MariaDB [hellodb]> select * from users where name='admin' and password=''or'1'='1';
MariaDB [hellodb]> select 1+2;
MariaDB [mysql]> select * from students where age in (18,20,22);
MariaDB [hellodb]> select * from students where name rlike '^s.*';
MariaDB [mysql]> select distinct gender from students;
avg()平均值函数 MariaDB [hellodb]> select avg(age) from students; MariaDB [hellodb]> select avg(age) from students where gender='M';
group by 分类 MariaDB [hellodb]> select gender,avg(age) from students group by gender; MariaDB [hellodb]> select gender,max(age) from students group by gender; MariaDB [hellodb]> select gender,count(*) from students group by gender; MariaDB [hellodb]> select gender 性别,count(*) 数量 from students group by gender;
MariaDB [hellodb]> select gender,count(*) from students group by gender having gender = 'm';
MariaDB [hellodb]> select gender,count(*) from students where gender = 'm' group by gender;
MariaDB [hellodb]> select classid,gender,avg(age) from students group by classid,gender;
MariaDB [hellodb]> select age from students order by age; MariaDB [hellodb]> select age from students order by age desc;
MariaDB [hellodb]> select classid,gender,avg(age) from students group by classid,gender order by avg(age) desc;
MariaDB [hellodb]> select classid,age from students order by classid,age;
MariaDB [hellodb]> select classid,age from students order by classid desc,age;
MariaDB [hellodb]> select * from students order by age desc limit 3;
MariaDB [hellodb]> select * from students order by age desc limit 2,3;
MariaDB [hellodb]> select classid,avg(age) from students group by classid having avg(age)>30;
MariaDB [hellodb]> select stuid,name,age from students -> union -> select tid,name,age from teachers;
MariaDB [hellodb]> select * from students cross join teachers;
MariaDB [hellodb]> select * from students inner join teachers on students.teacherid=teachers.tid;
MariaDB [hellodb]> select stuid,s.name,tid,t.name from students s inner join teachers t on s.teacherid=t.tid;
MariaDB [hellodb]> select stuid,s.name,tid,t.name from students s,teachers t where s.teacherid=t.tid;
MariaDB [mysql]> select stuid,s.name,tid,t.name from students s inner join teachers t on s.teacherid=t.tid and stuid >3;
ariaDB [hellodb]> select * from students left outer join teachers on students.teacherid=teachers.tid; 左外连接:左边的表内容全要,右边的表和左边表的相同部分
MariaDB [hellodb]> select * from students right outer join teachers on students.teacherid=teachers.tid; 右外连接:右边的表内容全要,左边的表和右边表的相同部分
MariaDB [hellodb]> select * from students left outer join teachers on students.teacherid=teachers.tid and teachers.tid is null;
MariaDB [hellodb]> select * from students left outer join teachers on students.teacherid=teachers.tid -> union -> select * from students right outer join teachers on students.teacherid=teachers.tid;
MariaDB [hellodb]> select stuid,name,age from students where age > (select avg(age) from students);
MariaDB [hellodb]> select * from (select s.stuid,s.name s_name,s.teacherid,t.tid,t.name t_name from students s left outer join teachers t on s.teacherid=t.tid union select s.stuid,s.name,s.teacherid,t.tid,t.name from students s right outer join teachers t on s.teacherid=t.tid) as a where a.teacherid is null or a.tid is null;
MariaDB [hellodb]> select st.stuid,st.name,sc.score,co.course from students as st inner join scores sc on st.stuid=sc.stuid inner join courses co on sc.courseid=co.courseid;
MariaDB [hellodb]> select emp.name emp_name,leader.name leader_name from employee emp inner join employee as leader on emp.leaderid=leader.id;
MariaDB [hellodb]> create view view_students as select stuid,name from students;
MariaDB [hellodb]> show table status like 'view_students'\G
MariaDB [hellodb]> drop view view_students;
https://dev.mysql.com/doc/refman/5.7/en/func-op-summary-ref.html 自定义函数保存在mysql.proc表中
mysql用户和权限管理
MariaDB [mysql]> create user hua@'192.168.2.100' identified by '123456';
[root@centos6 ~]
MariaDB [mysql]> drop user ''@localhost; MariaDB [mysql]> drop user ''@localhost.localdomain;
MariaDB [mysql]> select password('123456')
MariaDB [mysql]> set password for hua@'192.168.2.100'=password('77777');
MariaDB [mysql]> update mysql.user set password=password('123456') where user='root';
免密登录,用户破解账户密码[root@localhost ~] [mysqld] skip-networking skip-grant-tables [root@localhost ~] MariaDB [(none)]> update mysql.user set password=password('77777') where user='root';
mysql> grant all on wordpress.* to wpuser@'192.168.2.%' identified by '123456'; mysql> show grants for wpuser@'192.168.2.%'; MariaDB [(none)]> grant all on *.* to root@'192.168.2.%'; MariaDB [(none)]> show grants for root@'192.168.2.%'; Navicat for MySQL MariaDB [(none)]> revoke delete on wordpress.* from wpuser@'192.168.2.%';
|