mysql 学习

远程登录mysql: mysql -h ip -u username -p -P 3306

C:\Users\I>mysql -h 192.168.1.104 -u root -p -P 3306
Enter password: *
Welcome to the MySQL monitor.  Commands end with ; or \g.

设置和重设用户密码

1.查看密码

select host, user, authentication_string, plugin from user;//查看用户密码信息
//authentication_string 为用户密码 plugin 加密方式

 2.清空密码

update user set authentication_string='' where user='root';

3.设置密码

 ALTER user 'root'@'localhost' IDENTIFIED BY '1';
flush privileges;

 

查看用户: 

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from mysql.user;//show databases;

+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
4 rows in set (0.00 sec)

mysql>

 
4 rows in set (0.00 sec)

mysql> 

 删除用户

mysql>Delete FROM user Where User='test' and Host='localhost';

   mysql>flush privileges;

   mysql>drop database testDB; //删除用户的数据库

删除账户及权限:>drop user 用户名@'%';

        >drop user 用户名@ localhost;

创建用户

mysql> create user 'root'@'%' identified by '1';#  %代表所有端口
Query OK, 0 rows affected (0.07 sec)

mysql> select host,user from mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| %         | usrabc           |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+
5 rows in set (0.00 sec)

查看数据库列表DataBases:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

mysql>

 

添加DB:    create database gc;   //gc为要添加的数据库

[root@host]# mysqladmin -u root -p create RUNOOB //命令行直接创建DB

mysql> create database gc;
Query OK, 1 row affected (0.14 sec)

php添加DB

<?php
$dbhost = 'localhost:';
$dbuser = 'root';
$dbpass = '1';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass);//连接数据库
if(! $conn)
{
       die('连接错误'. mysqli_error($conn));
}
echo '连接成功<br />';
$sql = 'CREATE DATABASE RUNOOB';
$retval = mysqli_query($conn,$sql);
if(! $retval)
{
      die('创建数据库失败: '. mysqli_error($conn));
}
echo "数据库 RUNOOB 创建成功\n";
mysqli_close($conn);
?>

删除DB:   drop databa 

root @ host] #mysqladmin -u root -p drop RUNOOB //命令行直接删除DB

mysql> drop database gc;
Query OK, 0 rows affected (0.15 sec)

php删除数据库

<?php
$dbhost = 'localhost:';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn)
{
       die('connect fail: '. mysqli_error($conn));
}
echo 'connect sccess <br />';
$sql = 'DROP DATABASE gc';
$retval = mysqli_query($conn,$sql);
if(! $retval)
{
      die('delete DB fail: '. mysqli_error($conn));
}
echo 'DB gc delete sccess\n';
mysqli_close($conn);
?>

 创建数据表table

1.选定数据库 use gc;

mysql> use gc;
Database changed

2.显示当前DB全部的table

mysql> show tables;
+--------------+
| Tables_in_gc |
+--------------+
| accou1       |
+--------------+
1 row in set (0.00 sec)

 

3.table的格式

create table table_name{
    colum_name data_type,  //colum_name是列名  data_type是数据类型
    colum_name data_type,
    .
    .
    .
    colum_name data_type,
};

4.创建table : CREATE TABLE IF NOT EXISTS accou1();

mysql> CREATE TABLE IF NOT EXISTS accou1(
    -> id bigint(20),
    -> createTime datetime,
    -> ip varchar(255),
    -> mobile varchar(255),
    -> nickname varchar(255),
    -> passwd varchar(255),
    -> username varchar(255),
    -> avatar varchar(255),
    -> brief text,
    -> job varchar(255),
    -> location varchar(255),
    -> qq varchar(255),
    -> gender int(11),
    -> city varchar(255),
    -> province varchar(255)
    -> );
Query OK, 0 rows affected (0.58 sec)

php创建table

<?php
 $dbhost = "localhost:3306";
 $dbuser = "root";
 $dbpass = "1";
 $dbname = "gc";
 $conn = new mysqli($dbhost, $dbuser, $dbpass,$dbname);
 if($conn -> connect_error)
 {
        die('conncet fail ' . $conn -> connect_error);
}
else
{
      echo 'connect successs<br />';
}
$sql = "CREATE TABLE IF NOT EXISTS accou1(
        id bigint(20),
        createTime datetime,
        ip varchar(255),
        mobile varchar(255),
        nickname varchar(255),
        passwd varchar(255),
        username varchar(255),
        avatar varchar(255),
        brief text,
        job varchar(255),
        location varchar(255),
        qq varchar(255),
        gender int(11),
        city varchar(255),
        province varchar(255)  #注意这里不能有逗号
        )";
if($conn -> query($sql)=== true) echo "table ctreated successfully";
else
        echo "your table created fail: \n" . $conn ->error;
$conn -> close();
?>

 

5.删除table; drop table tablename;

mysql> drop table accou1;
Query OK, 0 rows affected (0.38 sec)

 php删除 table

<?php
  $dbhost = "localhost:3306";
  $dbuser = "root";
  $dbpass = "1";
  $dbname = "gc";
  $conn = new mysqli($dbhost, $dbuser, $dbpass,$dbname);
  if($conn -> connect_error)
  {
         die('conncet fail ' . $conn -> connect_error);
}
else
{
      echo 'connect successs<br />';
}
$sql = "DROP TABLE accou1";
if($conn -> query($sql)=== true) echo "table ctreated successfully";
else
        echo "your table created fail: \n" . $conn ->error;
$conn -> close();
?>

显示表所有的字段

DESCRIBE accou1;

 

删除列

alter table [table_name] add [column_name] [data_type] [not null][default]  #not null 非空 ,default 默认值

mysql> alter table accou1 add c1 int(11) not null default 4;
Query OK, 0 rows affected (0.43 sec)
Records: 0  Duplicates: 0  Warnings: 0

 删除列

alter table [table_name] drop [column_name]

mysql> ALTER TABLE accou1 drop c2;
Query OK, 0 rows affected (0.93 sec)
Records: 0  Duplicates: 0  Warnings: 0

 

 修改列的信息

alter table [table_name] change [old_column_name] [new_column_name] [data type]

1. 只改变列名:

  data_type和原理一样,old_column_name ! = new_columne

2. 只改变数据类型:

  old_culumn_name == new_column_name, data_type 改变

3. 列名和数据类型都改变

mysql> ALTER TABLE accou1 CHANGE c2 c2 int(20);
Query OK, 0 rows affected (1.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE accou1 CHANGE c2 c3 int(20);
Query OK, 0 rows affected (0.24 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE accou1 CHANGE c3 c1 char(20);
Query OK, 0 rows affected (1.32 sec)
Records: 0  Duplicates: 0  Warnings: 0

 

 修改表名

alter table [table_name] rename [new_table_name]

mysql> ALTER TABLE accou1 RENAME accout1;
Query OK, 0 rows affected (0.36 sec)

 

查看&插入表数据

查看表数据

select * from table_name;  #查看表所有数据

selcet col_name,col_name2,...   from table_name; #查看特定列的数据

mysql> SELECT * FROM books;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| 30      | NULL            | NULL |
+---------+-----------------+------+
2 rows in set (0.00 sec)

mysql> SELECT author FROM books;
+---------+
| author  |
+---------+
| feilong |
| 30      |
+---------+
2 rows in set (0.00 sec)

 

 

插入表数据

insert into [table_name] values(值2,值2,...);  #所有列同时插入数据

mysql> INSERT INTO books VALUES('feilong','www.feilong.com',30);#字符要加引号''
Query OK, 1 row affected (0.10 sec)

 

insert into [table_name] (列1,列2,...) value(值1,值2,...); #插入特定列的数据

mysql> INSERT INTO books (author) VALUES(30);
Query OK, 1 row affected (0.15 sec)

mysql> SELECT * FROM books;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| 30      | NULL            | NULL |
+---------+-----------------+------+
2 rows in set (0.00 sec)

 where条件查询(σ 选择 )

select * from table_name where col_name 运算符 值

mysql> SELECT * FROM books WHERE age > 10;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
+---------+-----------------+------+

 

between 值1 and 值2     在连个值范围内查找

mysql> SELECT * FROM books WHERE age BETWEEN 22 AND 26 ;
+--------+-------------+------+
| author | site        | age  |
+--------+-------------+------+
| qiu    | www.qiy.com |   23 |
| qiu    | www.qiy.com |   24 |
| qiu    | www.qiy.com |   25 |
+--------+-------------+------+

 

like                按通配符查找

mysql> SELECT * FROM books WHERE site LIKE '%.qy.%';
+--------+------------+------+
| author | site       | age  |
+--------+------------+------+
| qi     | www.qy.com |   21 |
+--------+------------+------+

 

mysql> SELECT * FROM books WHERE site NOT LIKE '%.qy.%';
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| qiyu    | www.qiyu.com    |   28 |
| qiu     | www.qiy.com     |   20 |
| qiu     | www.qiy.com     |   23 |
| qiu     | www.qiy.com     |   24 |
| qiu     | www.qiy.com     |   25 |
+---------+-----------------+------+

 

REGEXP 使用正则表达式查找,也可以使用 NOT REGEXP 查找

mysql> SELECT * FROM books WHERE site REGEXP '^w{3,}..*.com$';
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| qiyu    | www.qiyu.com    |   28 |
| qiu     | www.qiy.com     |   20 |
| qi      | www.qy.com      |   21 |
| qiu     | www.qiy.com     |   23 |
| qiu     | www.qiy.com     |   24 |
| qiu     | www.qiy.com     |   25 |
+---------+-----------------+------+

 

组合条件 and, or

select * from table_name where coll = xxx and col2 = xx or col3 > x;

mysql> SELECT * FROM books WHERE age > 10 or author = '30';
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| 30      | NULL            | NULL |
+---------+-----------------+------+

 

mysql> SELECT * FROM books WHERE author = 'qiu' and( age = 23 or age =25);#小括号先运算
+--------+-------------+------+
| author | site        | age  |
+--------+-------------+------+
| qiu    | www.qiy.com |   23 |
| qiu    | www.qiy.com |   25 |
+--------+-------------+------+

 null字段的判断  //不能用cul_name=null判断

select * from table_name where col_name is  null;

select * from table_name where col_name is not null;

mysql> SELECT * FROM books WHERE site IS NOT NULL;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| qiyu    | www.qiyu.com    |   28 |
| qiu     | www.qiy.com     |   20 |
| qi      | www.qy.com      |   21 |
| qiu     | www.qiy.com     |   23 |
| qiu     | www.qiy.com     |   24 |
| qiu     | www.qiy.com     |   25 |
+---------+-----------------+------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM books WHERE site IS NULL;
+--------+------+------+
| author | site | age  |
+--------+------+------+
| 30     | NULL | NULL |
+--------+------+------+
1 row in set (0.00 sec)

 去掉重复查询结果 distincet(精确的)

select distincet col_name from table_name;

mysql> SELECT DISTINCT author from books;
+---------+
| author  |
+---------+
| feilong |
| 30      |
| qiyu    |
| qiu     |
| qi      |
+---------+

 使用order by 对查询结果排序

1.按单一列名排序:

select * from table_name [where 字句] order by col_name [asc/desc] #col_name 需要排序的列名 asc升序排序 desc降序排序,不加asc或者desc,默认为asc

mysql> SELECT * FROM books WHERE age < 31 ORDER BY age ASC;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| qiu     | www.qiy.com     |   20 |
| qi      | www.qy.com      |   21 |
| qiu     | www.qiy.com     |   23 |
| qiu     | www.qiy.com     |   24 |
| qiu     | www.qiy.com     |   25 |
| qiyu    | www.qiyu.com    |   28 |
| feilong | www.feilong.com |   30 |
+---------+-----------------+------+

 

2.按多列排序:

select * from table_name [where 字节] order by col1 [asc/desc],col2 [asc/desc], ...

mysql> SELECT * FROM books WHERE age < 31 ORDER BY age DESC ,author ASC;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| qiyu    | www.qiyu.com    |   28 |
| qiu     | www.qiy.com     |   25 |
| qiu     | www.qiy.com     |   24 |
| qiu     | www.qiy.com     |   23 |
| qi      | www.qy.com      |   21 |
| qiu     | www.qiy.com     |   20 |
+---------+-----------------+------+

使用limit截取查询结果 

select * from table_name [where字句] limit [offset] rowCount;

offset:产线结果的起始位置,第一条记录的位置其实是0

rowCount: 从Offset位置开始,获取的记录条数

注意 limit rowCount = limit 0,rowCount  即offset可以省略

mysql> SELECT * FROM books WHERE age >22 limit 2,2;
+--------+-------------+------+
| author | site        | age  |
+--------+-------------+------+
| qiu    | www.qiy.com |   23 |
| qiu    | www.qiy.com |   24 |
+--------+-------------+------+

 

插入命令insert和查询命令select的组合使用

一般用法:

insert into [表名] values(值1,值2,...)

insert into [表名](列1, 列2...)values(值1 ,值2,...)

insert into 与select 的组合用法 

insert into [表名1] select 列1, 列2 from [表名2]

mysql> INSERT INTO books SELECT * FROM books WHERE age > 23 and age < 28;
Query OK, 2 rows affected (0.09 sec)

mysql> INSERT INTO books(author) SELECT site FROM books2 WHERE SITE IS NULL;
Query OK, 1 row affected (0.16 sec)

 

insert into [表名1] (列1 ,列2 ) select 列3 ,列4 from [表名2]

mysql> INSERT INTO books(author,site) SELECT site,age FROM books2 WHERE SITE IS NULL;
Query OK, 1 row affected (0.09 sec)

 insert into [表名] select * from 表2 #插入整张表2到表1

mysql> INSERT INTO books SELECT * FROM books2;
Query OK, 8 rows affected (0.11 sec)

 

复制表

只复制表结构到新表

create table 新表 select * from 旧表 where 1=2

或者

create table 新表 like 旧表 

mysql> CREATE TABLE books3 SELECT * FROM books WHERE age > 25;
Query OK, 2 rows affected (0.59 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> CREATE TABLE books4 LIKE books; Query OK, 0 rows affected (0.59 sec)

 

第二、复制表结构及数据到新表

create table新表 select * from 旧表 

mysql> CREATE TABLE books2 SELECT * FROM books;
Query OK, 8 rows affected (0.63 sec)
Records: 8  Duplicates: 0  Warnings: 0

 

更新表数据

update 语法

修改单列

update 表名 set 列名 =  xxx [where 字句]   

mysql> UPDATE books2 SET author = 'feilong';
Query OK, 8 rows affected (0.12 sec)
Rows matched: 8  Changed: 8  Warnings: 0

mysql> SELECT * FROM books2;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| feilong | NULL            | NULL |
| feilong | www.qiyu.com    |   28 |
| feilong | www.qiy.com     |   20 |
| feilong | www.qy.com      |   21 |
| feilong | www.qiy.com     |   23 |
| feilong | www.qiy.com     |   24 |
| feilong | www.qiy.com     |   25 |
+---------+-----------------+------+

mysql> UPDATE books SET author = 'long' WHERE author = '25';
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0

修改多列:

update 表名 set 列名1 = xxx, 列名2 = xxx...[where 字句]

mysql> UPDATE books SET author = 'long', site = 'www.w.com' WHERE author IS NULL AND site IS NULL;
Query OK, 2 rows affected (0.10 sec)
Rows matched: 2  Changed: 2  Warnings: 0

删除表数据

delete 语法

delete from 表名 [where字句]

 

mysql> DELETE FROM books WHERE author REGEXP '[0-9]+';#用正则时不要用* 会删除整张表
Query OK, 6 rows affected (0.14 sec)

 

where语句中in操作符使用

in 语法

select * from 表名 where 列名 in (value1, value2 ...)

mysql> SELECT * FROM books WHERE author in ('30','21');
+--------+------+------+
| author | site | age  |
+--------+------+------+
| 30     | NULL | NULL |
| 30     | NULL | NULL |
| 21     | NULL | NULL |
+--------+------+------+

 

select * from 表名 where 列名 in(select 列名 from 表名)

注解: 列名 in(value1 ,value2 ...) 等同 列名 = value1 or 列名 =value2...

mysql> SELECT * FROM books WHERE site IN (SELECT site FROM books2 WHERE age = 28 OR age = 24);

 

where 语句中between操作符使用

语法

select * from 表名 where 列名 between 值1 and 值2

mysql> SELECT * FROM books WHERE age BETWEEN 23 AND 25;
+--------+-------------+------+
| author | site        | age  |
+--------+-------------+------+
| qiu    | www.qiy.com |   23 |
| qiu    | www.qiy.com |   24 |
| qiu    | www.qiy.com |   25 |
| qiu    | www.qiy.com |   24 |
| qiu    | www.qiy.com |   25 |
+--------+-------------+------+

 

select * from 表名 where 列名 not between 值1 and 值2

 

mysql> SELECT * FROM books WHERE age NOT BETWEEN 23 AND 25;
+---------+-----------------+------+
| author  | site            | age  |
+---------+-----------------+------+
| feilong | www.feilong.com |   30 |
| qiyu    | www.qiyu.com    |   28 |
| qiu     | www.qiy.com     |   20 |
| qi      | www.qy.com      |   21 |
+---------+-----------------+------+

 

where语句中like操作符的使用:用于字符串的模糊匹配

语法

select * from 表名 where 表名 [not] like pattern 

pattern: 匹配模式(通配符), 比如 'abc' '%abc' , '%abc%'

mysql> SELECT * FROM books WHERE author like 'q%u';
+--------+--------------+------+
| author | site         | age  |
+--------+--------------+------+
| qiyu   | www.qiyu.com |   28 |
| qiu    | www.qiy.com  |   20 |
| qiu    | www.qiy.com  |   23 |
| qiu    | www.qiy.com  |   24 |
| qiu    | www.qiy.com  |   25 |
| qiu    | www.qiy.com  |   24 |
| qiu    | www.qiy.com  |   25 |
+--------+--------------+------+

I am a buttom line

posted @ 2019-03-18 00:12  江期玉  阅读(231)  评论(0编辑  收藏  举报