MySQL 数据库 3

索引
 1、普通索引(MUL)
   2、唯一索引(UNI)
   3、主键索引(PRI)
  1、使用规则
    1、一个表中只能有一个主键(primary)字段
    2、对应字段的值不允许重复,且不能为空
    3、主键字段的key标志PRI
    4、把表中能够唯一标识一条记录的字段设置为主键,通常把表中记录编号的字段设置为主键
  2、创建主键(primary key)
    1、创建表时创建
      1、字段名 数据类型 primary key,
      2、primary key(字段名)
    2、在已有表中创建
      alter table 表名 add primary key(字段名);
    3、删除
      alter table 表名 drop primary key;

 mysql> use db3
Database changed
mysql> create table t1(
-> id int primary key,
-> name varchar(15) not null,
-> sex enum('boy','girl') default 'boy'
-> )default charset=utf8;
Query OK, 0 rows affected (1.44 sec) mysql> desc t1;
+-------+--------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(15) | NO | | NULL | |
| sex | enum('boy','girl') | YES | | boy | |
+-------+--------------------+------+-----+---------+-------+
3 rows in set (0.13 sec) mysql> insert into t1 values(1,'zhangsanfeng','boy');
Query OK, 1 row affected (0.12 sec) mysql> insert into t1 values(1,'zhangwuji','boy');
ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'
mysql> 以上第一种方式\c
mysql>
mysql>
mysql> create table t2(
-> id int,
-> name char(20),
-> likes set('boy','girl','study'),
-> primary key(id)
-> );
Query OK, 0 rows affected (0.22 sec) mysql> desc t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
| likes | set('boy','girl','study') | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
3 rows in set (0.05 sec) mysql> 以上第2种方式\c
mysql>
mysql> primary key 主键索引\c
mysql>
mysql> alter table t2 drop primary key;
Query OK, 0 rows affected (0.89 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(20) | YES | | NULL | |
| likes | set('boy','girl','study') | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
3 rows in set (0.08 sec) mysql> alter table t2 add primary key(id);
Query OK, 0 rows affected (0.52 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
| likes | set('boy','girl','study') | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql>

    4、自增长属性(auto_increment)
      1、作用:通常和主键字段一起配合使用
      2、创建
        1、创建表时创建
        字段名 数据类型 primary key auto_increment
        2、在已有表中添加自增长属性(modify)
        alter table 表名 modify 字段名 数据类型 primary key auto_increment
    5、删除主键及自增长属性 (注:先删除自增长属性再删除主键  )
      1、alter table 表名 modify 字段名 数据类型;
      2、alter table 表名 drop primay key;

 mysql>
mysql> create table t3(
-> id int primary key auto_increment,
-> name char(15),
-> age tinyint unsigned
-> );
Query OK, 0 rows affected (0.29 sec) mysql> desc t3;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(15) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.06 sec) mysql>
mysql>
mysql> insert into t3 values(0,'赵敏',30);
ERROR 1366 (HY000): Incorrect string value: '\xE8\xB5\xB5\xE6\x95\x8F' for column 'name' at row 1
mysql> insert into t3 values(0,'zhaomin',30);
Query OK, 1 row affected (0.04 sec) mysql> insert into t3 values(0,'xiaozhao',30)
-> ;
Query OK, 1 row affected (0.07 sec) mysql> insert into t3 values(0,'zhouziruo',25);
Query OK, 1 row affected (0.02 sec) mysql> select * from t3;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhaomin | 30 |
| 2 | xiaozhao | 30 |
| 3 | zhouziruo | 25 |
+----+-----------+------+
3 rows in set (0.00 sec) mysql> delect from t3 where id=3;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delect from t3 where id=3' at line 1
mysql> delete from t3 where id=3;
Query OK, 1 row affected (0.22 sec) mysql> select * from t3;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | zhaomin | 30 |
| 2 | xiaozhao | 30 |
+----+----------+------+
2 rows in set (0.00 sec) mysql> insert into t3 values(0,'zhangwuji',28);
Query OK, 1 row affected (0.00 sec) mysql> select * from t3;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhaomin | 30 |
| 2 | xiaozhao | 30 |
| 4 | zhangwuji | 28 |
+----+-----------+------+
3 rows in set (0.00 sec) mysql> desc t3;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(15) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) mysql>

4、外键索引(foreign key)
          1、定义
    让当前表的字段值在另一个表的范围内选择
    2、语法格式
    foreign key(参考字段名)
    references 被参考表名(被参考字段名)
    on delete 级联动作
    on update 级联动作
   3、案例
     表1:缴费信息表(财务)
  学号 姓名 班级 缴费金额
  1 唐伯虎 AID1712 28000
  2 点秋香 AID1712 20000
        表2:学生信息表(班主任)
  学号 姓名 缴费金额
  1 唐伯虎 28000

 mysql> use db3;
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>
mysql> create table jftab(
-> id int primary key,
-> name char(20),
-> class varchar(7),
-> money int
-> ) default charset=utf8;
Query OK, 0 rows affected (0.28 sec) mysql> desc jftab;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
| class | varchar(7) | YES | | NULL | |
| money | int(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
4 rows in set (0.05 sec) mysql> insert into jftab values
-> (1,'唐伯虎','AID1806',28000),
-> (2,'点秋香','AID1806',20000),
-> (3,'祝枝山','AID1806',25000);
Query OK, 3 rows affected (0.18 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 1 | 唐伯虎 | AID1806 | 28000 |
| 2 | 点秋香 | AID1806 | 20000 |
| 3 | 祝枝山 | AID1806 | 25000 |
+----+-----------+---------+-------+
3 rows in set (0.00 sec) mysql> create table bjtab(
-> stu_id int,
-> name varchar(20),
-> money int,
-> foreign key(stu_id) references jftab(id)
-> on delete cascade
-> on update cascade
-> )default charset=utf8;
Query OK, 0 rows affected (0.44 sec) mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 1 | 唐伯虎 | AID1806 | 28000 |
| 2 | 点秋香 | AID1806 | 20000 |
| 3 | 祝枝山 | AID1806 | 25000 |
+----+-----------+---------+-------+
3 rows in set (0.00 sec) mysql> desc bjtab;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | | NULL | |
| money | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.03 sec) mysql> insert into bjtab values
-> (1,'唐伯虎',28000),
-> (2,'点秋香',20000);
Query OK, 2 rows affected (0.10 sec)
Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 1 | 唐伯虎 | 28000 |
| 2 | 点秋香 | 20000 |
+--------+-----------+-------+
2 rows in set (0.00 sec) mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 1 | 唐伯虎 | AID1806 | 28000 |
| 2 | 点秋香 | AID1806 | 20000 |
| 3 | 祝枝山 | AID1806 | 25000 |
+----+-----------+---------+-------+
3 rows in set (0.00 sec) mysql> insert into bjtab values
-> (4,'文征明',23000);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db3`.`bjtab`, CONSTRAINT `bjtab_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `jftab` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
mysql>
mysql> delete from jftab where name='点秋香';
Query OK, 1 row affected (0.11 sec) mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 1 | 唐伯虎 | AID1806 | 28000 |
| 3 | 祝枝山 | AID1806 | 25000 |
+----+-----------+---------+-------+
2 rows in set (0.01 sec) mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 1 | 唐伯虎 | 28000 |
+--------+-----------+-------+
1 row in set (0.00 sec) mysql> update jftab set id=8 where id=1;
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 8 | 唐伯虎 | 28000 |
+--------+-----------+-------+
1 row in set (0.00 sec) mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 3 | 祝枝山 | AID1806 | 25000 |
| 8 | 唐伯虎 | AID1806 | 28000 |
+----+-----------+---------+-------+
2 rows in set (0.00 sec) mysql>

  4、删除外键
    1、语法格式
  alter table 表名 drop foreign key 外键名;
    2、注意
      1、外键名的查看方式
      show create table 表名;

 mysql>
mysql> show create table bjtab;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bjtab | CREATE TABLE `bjtab` (
`stu_id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`money` int(11) DEFAULT NULL,
KEY `stu_id` (`stu_id`),
CONSTRAINT `bjtab_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `jftab` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.05 sec) mysql> alter table bjtab drop foreign key bjtab_ibfk_1;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table bjtab;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bjtab | CREATE TABLE `bjtab` (
`stu_id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`money` int(11) DEFAULT NULL,
KEY `stu_id` (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> insert into bjtab values(10,'xiaoxiao',28);
Query OK, 1 row affected (0.00 sec) mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 8 | 唐伯虎 | 28000 |
| 10 | xiaoxiao | 28 |
+--------+-----------+-------+
2 rows in set (0.00 sec) mysql> delete from bjtab where stu_id=10;
Query OK, 1 row affected (0.00 sec) mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 8 | 唐伯虎 | 28000 |
+--------+-----------+-------+
1 row in set (0.00 sec) mysql>
mysql> show create table bjtab;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bjtab | CREATE TABLE `bjtab` (
`stu_id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`money` int(11) DEFAULT NULL,
KEY `stu_id` (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql>

  5、在已有表中添加外键
    1、语法格式
    alter table 表名 add
    foreign key(参考字段名) references
    被参考表名(被参考字段名)
    on delete 级联动作
    on update 级联动作
    2、注意
    在已有表中添加外键时,会受到表中原有数据的限制

  6、级联动作
    1、cascade :数据级联更新
      1、当主表删除记录时,如果从表有相关联记录则级联删除
      2、当主表更新被参考字段的值时,从表级联更新参考字段的值
    2、restrict(默认)
      1、当主表删除记录时,如果从表中有相关联记录则不允许主表删除
      2、update同 1
    3、set null
      1、当主表删除记录时,从表中相关联记录外键字段值变为null
      2、update 同 1
    4、no action
      同 restrict,都是立即检查外键限制

  7、使用规则
    1、两张表被参考字段和参考字段的数据类型要一致
    2、被参考字段必须是KEY的一种,通常是primary key

 mysql> use db3;
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> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| bjtab |
| jftab |
| t1 |
| t2 |
| t3 |
+---------------+
5 rows in set (0.02 sec) mysql> alter table bjtab add
-> foreign key(stu_id) references jftab(id)
-> ;
Query OK, 1 row affected (3.94 sec)
Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 8 | 唐伯虎 | 28000 |
+--------+-----------+-------+
1 row in set (0.00 sec) mysql> desc bjtab;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | | NULL | |
| money | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.09 sec) mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 3 | 祝枝山 | AID1806 | 25000 |
| 8 | 唐伯虎 | AID1806 | 28000 |
+----+-----------+---------+-------+
2 rows in set (0.00 sec) mysql> delete from jftab where name='唐伯虎';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`db3`.`bjtab`, CONSTRAINT `bjtab_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `jftab` (`id`))
mysql> update jftab set id=10 where name='唐伯虎';
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`db3`.`bjtab`, CONSTRAINT `bjtab_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `jftab` (`id`))
mysql>
mysql>
mysql> show create table bjtab;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bjtab | CREATE TABLE `bjtab` (
`stu_id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`money` int(11) DEFAULT NULL,
KEY `stu_id` (`stu_id`),
CONSTRAINT `bjtab_ibfk_1` FOREIGN KEY (`stu_id`) REFERENCES `jftab` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.05 sec) mysql> alter table bjtab drop foreign key bjtab_ibfk_1 ;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc bjtab;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | | NULL | |
| money | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.05 sec) mysql> desc jftab;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
| class | varchar(7) | YES | | NULL | |
| money | int(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> alter table bjtab
-> add foreign key(stu_id) references jftab(id)
-> on delete set null
-> on update set null
-> ;
Query OK, 1 row affected (0.30 sec)
Records: 1 Duplicates: 0 Warnings: 0 mysql> desc bjtab;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | | NULL | |
| money | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql>
mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 3 | 祝枝山 | AID1806 | 25000 |
| 8 | 唐伯虎 | AID1806 | 28000 |
+----+-----------+---------+-------+
2 rows in set (0.00 sec) mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| 8 | 唐伯虎 | 28000 |
+--------+-----------+-------+
1 row in set (0.00 sec) mysql> delete from jftab where name='唐伯虎';
Query OK, 1 row affected (0.04 sec) mysql> select * from jftab;
+----+-----------+---------+-------+
| id | name | class | money |
+----+-----------+---------+-------+
| 3 | 祝枝山 | AID1806 | 25000 |
+----+-----------+---------+-------+
1 row in set (0.01 sec) mysql> select * from bjtab;
+--------+-----------+-------+
| stu_id | name | money |
+--------+-----------+-------+
| NULL | 唐伯虎 | 28000 |
+--------+-----------+-------+
1 row in set (0.00 sec) mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| bjtab |
| jftab |
| t1 |
| t2 |
| t3 |
+---------------+
5 rows in set (0.00 sec) mysql> desc t3;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(15) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec) mysql> alter table t3 modify id int;
Query OK, 3 rows affected (0.90 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> desc t3;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(15) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql> alter table t3 drop primary key;
Query OK, 3 rows affected (0.64 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> desc t3;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(15) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.06 sec) mysql>

数据导入
  1、作用:将文件系统的内容导入到数据库中
  2、语法格式
     load data infile "文件名"
    into table 表名
    fields terminated by "分隔符"
    lines terminated by "分隔符"

tarena:x:1000:1000:tarena,,,:/home/tarena:/bin/bash
用户名:密码:UID:GID:描述:家目录:登录权限
  3、把/etc/passwd 导入到mysql数据库中
  4、操作步骤
    1、在数据库中创建对应的表
    2、查看数据库的默认搜索路径
      show variables like "secure_file_priv";
    3、将系统文件拷贝到数据库的默认搜索路径中
      sudo cp /etc/passwd /var/lib/mysql-files
      sudo -i
      cd /var/lib/mysql-files
      ls
      exit

 mysql>
mysql> create table userinfo(
-> username char(20),
-> password char(1),
-> uid int,
-> gid int,
-> comment varchar(50),
-> homedir varchar(50),
-> shell varchar(50)
-> );
Query OK, 0 rows affected (0.25 sec) mysql>
mysql> show variables like 'secure_file_priv';
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.65 sec) mysql>
mysql>
mysql>
mysql> sudo cp /etc/passwd /var/lib/mysql-files/ \c
mysql> sudo -i
-> \c
mysql> cd /var/lib/mysql-files/ \c
mysql> passwd \c
mysql>
mysql> load data infile '/var/lib/mysql-files/passwd'
-> into table userinfo
-> fields terminated by ':'
-> lines terminated by '\n'
-> ;
Query OK, 44 rows affected (0.11 sec)
Records: 44 Deleted: 0 Skipped: 0 Warnings: 0 mysql> selete * from userinfo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'selete * from userinfo' at line 1
mysql> select * from userinfo;
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
| username | password | uid | gid | comment | homedir | shell |
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
| root | x | 0 | 0 | root | /root | /bin/bash |
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| sys | x | 3 | 3 | sys | /dev | /usr/sbin/nologin |
| sync | x | 4 | 65534 | sync | /bin | /bin/sync |
| games | x | 5 | 60 | games | /usr/games | /usr/sbin/nologin |
| man | x | 6 | 12 | man | /var/cache/man | /usr/sbin/nologin |
| lp | x | 7 | 7 | lp | /var/spool/lpd | /usr/sbin/nologin |
| mail | x | 8 | 8 | mail | /var/mail | /usr/sbin/nologin |
| news | x | 9 | 9 | news | /var/spool/news | /usr/sbin/nologin |
| uucp | x | 10 | 10 | uucp | /var/spool/uucp | /usr/sbin/nologin |
| proxy | x | 13 | 13 | proxy | /bin | /usr/sbin/nologin |
| www-data | x | 33 | 33 | www-data | /var/www | /usr/sbin/nologin |
| backup | x | 34 | 34 | backup | /var/backups | /usr/sbin/nologin |
| list | x | 38 | 38 | Mailing List Manager | /var/list | /usr/sbin/nologin |
| irc | x | 39 | 39 | ircd | /var/run/ircd | /usr/sbin/nologin |
| gnats | x | 41 | 41 | Gnats Bug-Reporting System (admin) | /var/lib/gnats | /usr/sbin/nologin |
| nobody | x | 65534 | 65534 | nobody | /nonexistent | /usr/sbin/nologin |
| systemd-timesync | x | 100 | 102 | systemd Time Synchronization,,, | /run/systemd | /bin/false |
| systemd-network | x | 101 | 103 | systemd Network Management,,, | /run/systemd/netif | /bin/false |
| systemd-resolve | x | 102 | 104 | systemd Resolver,,, | /run/systemd/resolve | /bin/false |
| systemd-bus-proxy | x | 103 | 105 | systemd Bus Proxy,,, | /run/systemd | /bin/false |
| syslog | x | 104 | 108 | | /home/syslog | /bin/false |
| _apt | x | 105 | 65534 | | /nonexistent | /bin/false |
| messagebus | x | 106 | 110 | | /var/run/dbus | /bin/false |
| uuidd | x | 107 | 111 | | /run/uuidd | /bin/false |
| lightdm | x | 108 | 114 | Light Display Manager | /var/lib/lightdm | /bin/false |
| whoopsie | x | 109 | 116 | | /nonexistent | /bin/false |
| avahi-autoipd | x | 110 | 119 | Avahi autoip daemon,,, | /var/lib/avahi-autoipd | /bin/false |
| avahi | x | 111 | 120 | Avahi mDNS daemon,,, | /var/run/avahi-daemon | /bin/false |
| dnsmasq | x | 112 | 65534 | dnsmasq,,, | /var/lib/misc | /bin/false |
| colord | x | 113 | 123 | colord colour management daemon,,, | /var/lib/colord | /bin/false |
| speech-dispatcher | x | 114 | 29 | Speech Dispatcher,,, | /var/run/speech-dispatcher | /bin/false |
| hplip | x | 115 | 7 | HPLIP system user,,, | /var/run/hplip | /bin/false |
| kernoops | x | 116 | 65534 | Kernel Oops Tracking Daemon,,, | / | /bin/false |
| pulse | x | 117 | 124 | PulseAudio daemon,,, | /var/run/pulse | /bin/false |
| rtkit | x | 118 | 126 | RealtimeKit,,, | /proc | /bin/false |
| saned | x | 119 | 127 | | /var/lib/saned | /bin/false |
| usbmux | x | 120 | 46 | usbmux daemon,,, | /var/lib/usbmux | /bin/false |
| tarena | x | 1000 | 1000 | tarena,,, | /home/tarena | /bin/bash |
| sshd | x | 121 | 65534 | | /var/run/sshd | /usr/sbin/nologin |
| mysql | x | 122 | 129 | MySQL Server,,, | /nonexistent | /bin/false |
| mongodb | x | 123 | 65534 | | /var/lib/mongodb | /bin/false |
| redis | x | 124 | 131 | | /var/lib/redis | /bin/false |
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
44 rows in set (0.00 sec) mysql> select * from userinfo\G;
*************************** 1. row ***************************
username: root
password: x
uid: 0
gid: 0
comment: root
homedir: /root
shell: /bin/bash
*************************** 2. row ***************************
username: daemon
password: x
uid: 1
gid: 1
comment: daemon
homedir: /usr/sbin
shell: /usr/sbin/nologin
*************************** 3. row ***************************
username: bin
password: x
uid: 2
gid: 2
comment: bin
homedir: /bin
shell: /usr/sbin/nologin
*************************** 4. row ***************************
username: sys
password: x
uid: 3
gid: 3
comment: sys
homedir: /dev
shell: /usr/sbin/nologin
*************************** 5. row ***************************
username: sync
password: x
uid: 4
gid: 65534
comment: sync
homedir: /bin
shell: /bin/sync
*************************** 6. row ***************************
username: games
password: x
uid: 5
gid: 60
comment: games
homedir: /usr/games
shell: /usr/sbin/nologin
*************************** 7. row ***************************
username: man
password: x
uid: 6
gid: 12
comment: man
homedir: /var/cache/man
shell: /usr/sbin/nologin
*************************** 8. row ***************************
username: lp
password: x
uid: 7
gid: 7
comment: lp
homedir: /var/spool/lpd
shell: /usr/sbin/nologin
*************************** 9. row ***************************
username: mail
password: x
uid: 8
gid: 8
comment: mail
homedir: /var/mail
shell: /usr/sbin/nologin
*************************** 10. row ***************************
username: news
password: x
uid: 9
gid: 9
comment: news
homedir: /var/spool/news
shell: /usr/sbin/nologin
*************************** 11. row ***************************
username: uucp
password: x
uid: 10
gid: 10
comment: uucp
homedir: /var/spool/uucp
shell: /usr/sbin/nologin
*************************** 12. row ***************************
username: proxy
password: x
uid: 13
gid: 13
comment: proxy
homedir: /bin
shell: /usr/sbin/nologin
*************************** 13. row ***************************
username: www-data
password: x
uid: 33
gid: 33
comment: www-data
homedir: /var/www
shell: /usr/sbin/nologin
*************************** 14. row ***************************
username: backup
password: x
uid: 34
gid: 34
comment: backup
homedir: /var/backups
shell: /usr/sbin/nologin
*************************** 15. row ***************************
username: list
password: x
uid: 38
gid: 38
comment: Mailing List Manager
homedir: /var/list
shell: /usr/sbin/nologin
*************************** 16. row ***************************
username: irc
password: x
uid: 39
gid: 39
comment: ircd
homedir: /var/run/ircd
shell: /usr/sbin/nologin
*************************** 17. row ***************************
username: gnats
password: x
uid: 41
gid: 41
comment: Gnats Bug-Reporting System (admin)
homedir: /var/lib/gnats
shell: /usr/sbin/nologin
*************************** 18. row ***************************
username: nobody
password: x
uid: 65534
gid: 65534
comment: nobody
homedir: /nonexistent
shell: /usr/sbin/nologin
*************************** 19. row ***************************
username: systemd-timesync
password: x
uid: 100
gid: 102
comment: systemd Time Synchronization,,,
homedir: /run/systemd
shell: /bin/false
*************************** 20. row ***************************
username: systemd-network
password: x
uid: 101
gid: 103
comment: systemd Network Management,,,
homedir: /run/systemd/netif
shell: /bin/false
*************************** 21. row ***************************
username: systemd-resolve
password: x
uid: 102
gid: 104
comment: systemd Resolver,,,
homedir: /run/systemd/resolve
shell: /bin/false
*************************** 22. row ***************************
username: systemd-bus-proxy
password: x
uid: 103
gid: 105
comment: systemd Bus Proxy,,,
homedir: /run/systemd
shell: /bin/false
*************************** 23. row ***************************
username: syslog
password: x
uid: 104
gid: 108
comment:
homedir: /home/syslog
shell: /bin/false
*************************** 24. row ***************************
username: _apt
password: x
uid: 105
gid: 65534
comment:
homedir: /nonexistent
shell: /bin/false
*************************** 25. row ***************************
username: messagebus
password: x
uid: 106
gid: 110
comment:
homedir: /var/run/dbus
shell: /bin/false
*************************** 26. row ***************************
username: uuidd
password: x
uid: 107
gid: 111
comment:
homedir: /run/uuidd
shell: /bin/false
*************************** 27. row ***************************
username: lightdm
password: x
uid: 108
gid: 114
comment: Light Display Manager
homedir: /var/lib/lightdm
shell: /bin/false
*************************** 28. row ***************************
username: whoopsie
password: x
uid: 109
gid: 116
comment:
homedir: /nonexistent
shell: /bin/false
*************************** 29. row ***************************
username: avahi-autoipd
password: x
uid: 110
gid: 119
comment: Avahi autoip daemon,,,
homedir: /var/lib/avahi-autoipd
shell: /bin/false
*************************** 30. row ***************************
username: avahi
password: x
uid: 111
gid: 120
comment: Avahi mDNS daemon,,,
homedir: /var/run/avahi-daemon
shell: /bin/false
*************************** 31. row ***************************
username: dnsmasq
password: x
uid: 112
gid: 65534
comment: dnsmasq,,,
homedir: /var/lib/misc
shell: /bin/false
*************************** 32. row ***************************
username: colord
password: x
uid: 113
gid: 123
comment: colord colour management daemon,,,
homedir: /var/lib/colord
shell: /bin/false
*************************** 33. row ***************************
username: speech-dispatcher
password: x
uid: 114
gid: 29
comment: Speech Dispatcher,,,
homedir: /var/run/speech-dispatcher
shell: /bin/false
*************************** 34. row ***************************
username: hplip
password: x
uid: 115
gid: 7
comment: HPLIP system user,,,
homedir: /var/run/hplip
shell: /bin/false
*************************** 35. row ***************************
username: kernoops
password: x
uid: 116
gid: 65534
comment: Kernel Oops Tracking Daemon,,,
homedir: /
shell: /bin/false
*************************** 36. row ***************************
username: pulse
password: x
uid: 117
gid: 124
comment: PulseAudio daemon,,,
homedir: /var/run/pulse
shell: /bin/false
*************************** 37. row ***************************
username: rtkit
password: x
uid: 118
gid: 126
comment: RealtimeKit,,,
homedir: /proc
shell: /bin/false
*************************** 38. row ***************************
username: saned
password: x
uid: 119
gid: 127
comment:
homedir: /var/lib/saned
shell: /bin/false
*************************** 39. row ***************************
username: usbmux
password: x
uid: 120
gid: 46
comment: usbmux daemon,,,
homedir: /var/lib/usbmux
shell: /bin/false
*************************** 40. row ***************************
username: tarena
password: x
uid: 1000
gid: 1000
comment: tarena,,,
homedir: /home/tarena
shell: /bin/bash
*************************** 41. row ***************************
username: sshd
password: x
uid: 121
gid: 65534
comment:
homedir: /var/run/sshd
shell: /usr/sbin/nologin
*************************** 42. row ***************************
username: mysql
password: x
uid: 122
gid: 129
comment: MySQL Server,,,
homedir: /nonexistent
shell: /bin/false
*************************** 43. row ***************************
username: mongodb
password: x
uid: 123
gid: 65534
comment:
homedir: /var/lib/mongodb
shell: /bin/false
*************************** 44. row ***************************
username: redis
password: x
uid: 124
gid: 131
comment:
homedir: /var/lib/redis
shell: /bin/false
44 rows in set (0.00 sec) ERROR:
No query specified mysql> delect from userinfo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delect from userinfo' at line 1
mysql> delete from userinfo;
Query OK, 44 rows affected (0.05 sec) mysql> desc userinfo;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | char(20) | YES | | NULL | |
| password | char(1) | YES | | NULL | |
| uid | int(11) | YES | | NULL | |
| gid | int(11) | YES | | NULL | |
| comment | varchar(50) | YES | | NULL | |
| homedir | varchar(50) | YES | | NULL | |
| shell | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec) mysql> alter table userinfo add 'ziduan' 'shujuleixing' ater \c
mysql>
mysql> select * from userinfo;
Empty set (0.00 sec) mysql>
mysql> desc userinfo;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | char(20) | YES | | NULL | |
| password | char(1) | YES | | NULL | |
| uid | int(11) | YES | | NULL | |
| gid | int(11) | YES | | NULL | |
| comment | varchar(50) | YES | | NULL | |
| homedir | varchar(50) | YES | | NULL | |
| shell | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec) mysql> load date infile '/var/lib/mysql-files/passwd'
-> into table userinfo
-> fields terminated by ':'
-> lines terminated by '\n'
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'date infile '/var/lib/mysql-files/passwd'
into table userinfo
fields terminated ' at line 1
mysql> load data infile '/var/lib/mysql-files/passwd' into table userinfo fields terminated by ':' lines terminated by '\n';
Query OK, 44 rows affected (0.06 sec)
Records: 44 Deleted: 0 Skipped: 0 Warnings: 0 mysql>
mysql>
mysql> select * from userinfo;
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
| username | password | uid | gid | comment | homedir | shell |
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
| root | x | 0 | 0 | root | /root | /bin/bash |
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| sys | x | 3 | 3 | sys | /dev | /usr/sbin/nologin |
| sync | x | 4 | 65534 | sync | /bin | /bin/sync |
| games | x | 5 | 60 | games | /usr/games | /usr/sbin/nologin |
| man | x | 6 | 12 | man | /var/cache/man | /usr/sbin/nologin |
| lp | x | 7 | 7 | lp | /var/spool/lpd | /usr/sbin/nologin |
| mail | x | 8 | 8 | mail | /var/mail | /usr/sbin/nologin |
| news | x | 9 | 9 | news | /var/spool/news | /usr/sbin/nologin |
| uucp | x | 10 | 10 | uucp | /var/spool/uucp | /usr/sbin/nologin |
| proxy | x | 13 | 13 | proxy | /bin | /usr/sbin/nologin |
| www-data | x | 33 | 33 | www-data | /var/www | /usr/sbin/nologin |
| backup | x | 34 | 34 | backup | /var/backups | /usr/sbin/nologin |
| list | x | 38 | 38 | Mailing List Manager | /var/list | /usr/sbin/nologin |
| irc | x | 39 | 39 | ircd | /var/run/ircd | /usr/sbin/nologin |
| gnats | x | 41 | 41 | Gnats Bug-Reporting System (admin) | /var/lib/gnats | /usr/sbin/nologin |
| nobody | x | 65534 | 65534 | nobody | /nonexistent | /usr/sbin/nologin |
| systemd-timesync | x | 100 | 102 | systemd Time Synchronization,,, | /run/systemd | /bin/false |
| systemd-network | x | 101 | 103 | systemd Network Management,,, | /run/systemd/netif | /bin/false |
| systemd-resolve | x | 102 | 104 | systemd Resolver,,, | /run/systemd/resolve | /bin/false |
| systemd-bus-proxy | x | 103 | 105 | systemd Bus Proxy,,, | /run/systemd | /bin/false |
| syslog | x | 104 | 108 | | /home/syslog | /bin/false |
| _apt | x | 105 | 65534 | | /nonexistent | /bin/false |
| messagebus | x | 106 | 110 | | /var/run/dbus | /bin/false |
| uuidd | x | 107 | 111 | | /run/uuidd | /bin/false |
| lightdm | x | 108 | 114 | Light Display Manager | /var/lib/lightdm | /bin/false |
| whoopsie | x | 109 | 116 | | /nonexistent | /bin/false |
| avahi-autoipd | x | 110 | 119 | Avahi autoip daemon,,, | /var/lib/avahi-autoipd | /bin/false |
| avahi | x | 111 | 120 | Avahi mDNS daemon,,, | /var/run/avahi-daemon | /bin/false |
| dnsmasq | x | 112 | 65534 | dnsmasq,,, | /var/lib/misc | /bin/false |
| colord | x | 113 | 123 | colord colour management daemon,,, | /var/lib/colord | /bin/false |
| speech-dispatcher | x | 114 | 29 | Speech Dispatcher,,, | /var/run/speech-dispatcher | /bin/false |
| hplip | x | 115 | 7 | HPLIP system user,,, | /var/run/hplip | /bin/false |
| kernoops | x | 116 | 65534 | Kernel Oops Tracking Daemon,,, | / | /bin/false |
| pulse | x | 117 | 124 | PulseAudio daemon,,, | /var/run/pulse | /bin/false |
| rtkit | x | 118 | 126 | RealtimeKit,,, | /proc | /bin/false |
| saned | x | 119 | 127 | | /var/lib/saned | /bin/false |
| usbmux | x | 120 | 46 | usbmux daemon,,, | /var/lib/usbmux | /bin/false |
| tarena | x | 1000 | 1000 | tarena,,, | /home/tarena | /bin/bash |
| sshd | x | 121 | 65534 | | /var/run/sshd | /usr/sbin/nologin |
| mysql | x | 122 | 129 | MySQL Server,,, | /nonexistent | /bin/false |
| mongodb | x | 123 | 65534 | | /var/lib/mongodb | /bin/false |
| redis | x | 124 | 131 | | /var/lib/redis | /bin/false |
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
44 rows in set (0.00 sec) mysql>

数据导出
  1、作用
  将数据库中表的记录保存到系统文件里
  2、语法格式
    select ... from 表名
    into outfile "文件名"
    fields terminated by "分隔符"
    lines terminated by "分隔符"
  3、练习
1、把userinfo表中的用户名、密码和uid号三个字段导出到userinfo.txt中
2、将库名:mysql库中user表中的User、Host两个字段的值导出到 user2.txt
  4、注意
1、导出的内容由SQL查询语句决定
2、执行导出命令时路径必须指定在对应的数据库目录下
3、show variables like "secure_file_priv";
show variables like "%secure%";

 mysql> select username,password,uid from userinfo;
+-------------------+----------+-------+
| username | password | uid |
+-------------------+----------+-------+
| root | x | 0 |
| daemon | x | 1 |
| bin | x | 2 |
| sys | x | 3 |
| sync | x | 4 |
| games | x | 5 |
| man | x | 6 |
| lp | x | 7 |
| mail | x | 8 |
| news | x | 9 |
| uucp | x | 10 |
| proxy | x | 13 |
| www-data | x | 33 |
| backup | x | 34 |
| list | x | 38 |
| irc | x | 39 |
| gnats | x | 41 |
| nobody | x | 65534 |
| systemd-timesync | x | 100 |
| systemd-network | x | 101 |
| systemd-resolve | x | 102 |
| systemd-bus-proxy | x | 103 |
| syslog | x | 104 |
| _apt | x | 105 |
| messagebus | x | 106 |
| uuidd | x | 107 |
| lightdm | x | 108 |
| whoopsie | x | 109 |
| avahi-autoipd | x | 110 |
| avahi | x | 111 |
| dnsmasq | x | 112 |
| colord | x | 113 |
| speech-dispatcher | x | 114 |
| hplip | x | 115 |
| kernoops | x | 116 |
| pulse | x | 117 |
| rtkit | x | 118 |
| saned | x | 119 |
| usbmux | x | 120 |
| tarena | x | 1000 |
| sshd | x | 121 |
| mysql | x | 122 |
| mongodb | x | 123 |
| redis | x | 124 |
+-------------------+----------+-------+
44 rows in set (0.00 sec) mysql> select username,password,uid from userinfo
-> into outfile '/var/lib/mysql-file/userinfo.txt'
-> fields terminated by ' '
-> lines terminated by '\n'
-> ;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
mysql>
mysql>
mysql> select username,password,uid from userinfo
-> into outfile '/var/lib/mysql-files/userinfo.txt'
-> fields terminated by ' '
-> lines terminated by '\n'
-> ;
Query OK, 44 rows affected (0.04 sec) mysql>
root@tedu:/var/lib/mysql-files# ls
passwd userinfo.txt
root@tedu:/var/lib/mysql-files# cat userinfo.txt
root x 0
daemon x 1
bin x 2
sys x 3
sync x 4
games x 5
man x 6
lp x 7
mail x 8
news x 9
uucp x 10
proxy x 13
www-data x 33
backup x 34
list x 38
irc x 39
gnats x 41
nobody x 65534
systemd-timesync x 100
systemd-network x 101
systemd-resolve x 102
systemd-bus-proxy x 103
syslog x 104
_apt x 105
messagebus x 106
uuidd x 107
lightdm x 108
whoopsie x 109
avahi-autoipd x 110
avahi x 111
dnsmasq x 112
colord x 113
speech-dispatcher x 114
hplip x 115
kernoops x 116
。。。。。 ####
mysql> select database();
+------------+
| database() |
+------------+
| db3 |
+------------+
1 row in set (0.01 sec) mysql> select User,Host from mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.31 sec) mysql> select User,Host from mysql.user
-> into outfile '/var/lib/mysql-files/user2.txt'
-> fields terminated by ' '
-> lines terminated by '\n'
-> ;
Query OK, 4 rows affected (0.00 sec) mysql>
root@tedu:/var/lib/mysql-files# ls
passwd user2.txt userinfo.txt
root@tedu:/var/lib/mysql-files# cat user2.txt
debian-sys-maint localhost
mysql.session localhost
mysql.sys localhost
root localhost
mysql> show variables like '%secure%';
+--------------------------+-----------------------+
| Variable_name | Value |
+--------------------------+-----------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
3 rows in set (0.06 sec) mysql>
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec) mysql>

4、表的复制
  1、表的复制
    1、语法格式
    create table 表名 select 查询命令;
    2、练习
    1、复制userinfo表的前10行,userinfo3 \c
    2、复制userinfo表的用户名、密码、uid三个字 段的2-10条记录,userinfo4
  2、只复制表结构
    1、语法格式
    create table 表名 select ... where false;     (create table 表名 select ... where 0;     )
  3、注意
    1、复制表的时候不会把原有表的 键 属性复制过来

 mysql> create table userinfo2
-> select * from userinfo;
Query OK, 44 rows affected (0.58 sec)
Records: 44 Duplicates: 0 Warnings: 0 mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| bjtab |
| jftab |
| t1 |
| t2 |
| t3 |
| userinfo |
| userinfo2 |
+---------------+
7 rows in set (0.01 sec) mysql> select * from userinfo2;
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
| username | password | uid | gid | comment | homedir | shell |
+-------------------+----------+-------+-------+------------------------------------+----------------------------+-------------------+
| root | x | 0 | 0 | root | /root | /bin/bash |
| daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin |
| bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin |
| sys | x | 3 | 3 | sys | /dev | /usr/sbin/nologin |
| sync | x | 4 | 65534 | s
#######
mysql> create table userinfo3
-> select * from userinfo limit 10;
Query OK, 10 rows affected (25.42 sec)
Records: 10 Duplicates: 0 Warnings: 0 mysql> create table userinfo4
-> select * from userinof limit 1,9;
ERROR 1146 (42S02): Table 'db3.userinof' doesn't exist
mysql> create table userinfo4 select * from userinfo limit 1,9;
Query OK, 9 rows affected (0.37 sec)
Records: 9 Duplicates: 0 Warnings: 0 mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| bjtab |
| jftab |
| t1 |
| t2 |
| t3 |
| userinfo |
| userinfo2 |
| userinfo3 |
| userinfo4 |
+---------------+
9 rows in set (0.00 sec) mysql> desc t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
| likes | set('boy','girl','study') | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
3 rows in set (0.07 sec) mysql> create table new_t2 select * from t2 where false;
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc new_t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(20) | YES | | NULL | |
| likes | set('boy','girl','study') | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
3 rows in set (0.04 sec) mysql> create table new_t2_t2 select * from t2 where 0;
Query OK, 0 rows affected (0.48 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc new_t2_t2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(20) | YES | | NULL | |
| likes | set('boy','girl','study') | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql>

嵌套查询
  1、定义
  把内层的查询结果作为外层查询的条件
  2、语法格式
       select   查询语句   where     条件   (select   查询语句);
  3、练习
    1、把uid的值小于这个字段的平均值的用户名和uid显示出来
    2、查找userinfo表中用户名在 mysql库下的user表Host值为localhost并且User值是root 的用户名

 mysql> select avg(uid) from userinfo;
+-----------+
| avg(uid) |
+-----------+
| 1581.5227 |
+-----------+
1 row in set (0.11 sec) mysql> select username,uid from userinfo
-> where
-> uid < 1581.5227;
+-------------------+------+
| username | uid |
+-------------------+------+
| root | 0 |
| daemon | 1 |
| bin | 2 |
| sys | 3 |
| sync | 4 |
| games | 5 |
| man | 6 |
| lp | 7 |
| mail | 8 |
| news | 9 |
| uucp | 10 |
| proxy | 13 |
| www-data | 33 |
| backup | 34 |
| list | 38 |
| irc | 39 |
| gnats | 41 |
| systemd-timesync | 100 |
| systemd-network | 101 |
| systemd-resolve | 102 |
| systemd-bus-proxy | 103 |
| syslog | 104 |
| _apt | 105 |
| messagebus | 106 |
| uuidd | 107 |
| lightdm | 108 |
| whoopsie | 109 |
| avahi-autoipd | 110 |
| avahi | 111 |
| dnsmasq | 112 |
| colord | 113 |
| speech-dispatcher | 114 |
| hplip | 115 |
| kernoops | 116 |
| pulse | 117 |
| rtkit | 118 |
| saned | 119 |
| usbmux | 120 |
| tarena | 1000 |
| sshd | 121 |
| mysql | 122 |
| mongodb | 123 |
| redis | 124 |
+-------------------+------+
43 rows in set (0.00 sec) mysql>
mysql> select username,uid from userinfo
-> where
-> uid < (select avg(uid) from userinfo);
+-------------------+------+
| username | uid |
+-------------------+------+
| root | 0 |
| daemon | 1 |
| bin | 2 |
| sys | 3 |
| sync | 4 |
| games | 5 |
| man | 6 |
| lp | 7 |
| mail | 8 |
| news | 9 |
| uucp | 10 |
| proxy | 13 |
| www-data | 33 |
| backup | 34 |
| list | 38 |
| irc | 39 |
| gnats | 41 |
| systemd-timesync | 100 |
| systemd-network | 101 |
| systemd-resolve | 102 |
| systemd-bus-proxy | 103 |
| syslog | 104 |
| _apt | 105 |
| messagebus | 106 |
| uuidd | 107 |
| lightdm | 108 |
| whoopsie | 109 |
| avahi-autoipd | 110 |
| avahi | 111 |
| dnsmasq | 112 |
| colord | 113 |
| speech-dispatcher | 114 |
| hplip | 115 |
| kernoops | 116 |
| pulse | 117 |
| rtkit | 118 |
| saned | 119 |
| usbmux | 120 |
| tarena | 1000 |
| sshd | 121 |
| mysql | 122 |
| mongodb | 123 |
| redis | 124 |
+-------------------+------+
43 rows in set (0.08 sec) mysql>
mysql> select username from userinfo
-> where username in
-> (select User from mysql.user where Host='localhost' and User='root');
+----------+
| username |
+----------+
| root |
+----------+
1 row in set (0.08 sec) mysql>
test

多表查询
  1、两种方式
    1、select 字段名列表 from 表名列表;       #笛卡尔积
      select * from tt1,tt2;
    2、select 字段名列表 from 表名列表 where 条件;
  2、练习
    1、显示省和市的信息
    2、显示省、市、县的信息

 mysql> 1,user1 包含 username uid shell 前两条\c
mysql>
mysql> create table tt1
-> select username,uid,shell from userinfo
-> limit 2;
Query OK, 2 rows affected (0.46 sec)
Records: 2 Duplicates: 0 Warnings: 0 mysql> 2, tt2 包含 username uid gid 前3条\c
mysql> create table tt2
-> select username,uid,gid from userinfo
-> limit 3;
Query OK, 3 rows affected (0.69 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from tt1;
+----------+------+-------------------+
| username | uid | shell |
+----------+------+-------------------+
| root | 0 | /bin/bash |
| daemon | 1 | /usr/sbin/nologin |
+----------+------+-------------------+
2 rows in set (0.00 sec) mysql> select * from tt2;
+----------+------+------+
| username | uid | gid |
+----------+------+------+
| root | 0 | 0 |
| daemon | 1 | 1 |
| bin | 2 | 2 |
+----------+------+------+
3 rows in set (0.00 sec) mysql> select * from tt1,tt2;
+----------+------+-------------------+----------+------+------+
| username | uid | shell | username | uid | gid |
+----------+------+-------------------+----------+------+------+
| root | 0 | /bin/bash | root | 0 | 0 |
| daemon | 1 | /usr/sbin/nologin | root | 0 | 0 |
| root | 0 | /bin/bash | daemon | 1 | 1 |
| daemon | 1 | /usr/sbin/nologin | daemon | 1 | 1 |
| root | 0 | /bin/bash | bin | 2 | 2 |
| daemon | 1 | /usr/sbin/nologin | bin | 2 | 2 |
+----------+------+-------------------+----------+------+------+
6 rows in set (0.02 sec) mysql> 1,tt1 tt2 表中,uid号相同的信息\c
mysql> select * from tt1, tt2
-> where
-> tt1.uid = tt2,uid;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'uid' at line 3
mysql> select * from tt1, tt2 where tt1.uid = tt2.uid;
+----------+------+-------------------+----------+------+------+
| username | uid | shell | username | uid | gid |
+----------+------+-------------------+----------+------+------+
| root | 0 | /bin/bash | root | 0 | 0 |
| daemon | 1 | /usr/sbin/nologin | daemon | 1 | 1 |
+----------+------+-------------------+----------+------+------+
2 rows in set (0.03 sec) mysql>
mysql>
mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| bjtab |
| city |
| jftab |
| new_t2 |
| new_t2_t2 |
| sheng |
| t1 |
| t2 |
| t3 |
| tt1 |
| tt2 |
| userinfo |
| userinfo2 |
| userinfo3 |
| userinfo4 |
| xian |
+---------------+
16 rows in set (0.00 sec) mysql> select * from sheng;
+----+--------+--------------------+
| id | S_ID | S_name |
+----+--------+--------------------+
| 1 | 130000 | 河北省 |
| 2 | 140000 | 山西省 |
| 3 | 150000 | 内蒙古自治区 |
| 4 | 160000 | 辽宁省 |
| 5 | 170000 | 黑龙江省 |
+----+--------+--------------------+
5 rows in set (0.00 sec) mysql> select * from city;
+----+--------+-----------------+------------+
| id | C_ID | C_name | CFather_ID |
+----+--------+-----------------+------------+
| 1 | 131100 | 石家庄市 | 130000 |
| 2 | 131101 | 沧州市 | 130000 |
| 3 | 131102 | 廊坊市 | 130000 |
| 4 | 131103 | 衡水市 | 130000 |
| 5 | 131104 | 太原市 | 140000 |
| 6 | 131105 | 呼和浩特市 | 150000 |
| 7 | 131106 | 包头市 | 150000 |
| 8 | 131107 | 沈阳市 | 160000 |
| 9 | 131108 | 大连市 | 160000 |
| 10 | 131109 | 无锡市 | 320000 |
| 11 | 131110 | 徐州市 | 320000 |
| 12 | 131111 | 常州市 | 320000 |
+----+--------+-----------------+------------+
12 rows in set (0.01 sec) mysql> select * from xian;
+----+--------+-----------+------------+
| id | X_ID | X_name | XFather_ID |
+----+--------+-----------+------------+
| 1 | 132100 | 河东区 | 131100 |
| 2 | 132101 | 正定县 | 131100 |
| 3 | 132102 | 固安县 | 131102 |
| 4 | 132102 | 香河县 | 131102 |
| 5 | 132103 | 哈哈 | 131112 |
+----+--------+-----------+------------+
5 rows in set (0.00 sec) mysql> 1、显示省和市的信息\c
mysql>
mysql> select sheng.S_name,city.C_name
-> from sheng,city
-> where
-> sheng.S_ID=city.CFather_ID;
+--------------------+-----------------+
| S_name | C_name |
+--------------------+-----------------+
| 河北省 | 石家庄市 |
| 河北省 | 沧州市 |
| 河北省 | 廊坊市 |
| 河北省 | 衡水市 |
| 山西省 | 太原市 |
| 内蒙古自治区 | 呼和浩特市 |
| 内蒙古自治区 | 包头市 |
| 辽宁省 | 沈阳市 |
| 辽宁省 | 大连市 |
+--------------------+-----------------+
9 rows in set (0.00 sec) mysql> 2、显示省、市、县的信息\c
mysql> select sheng.S_name,city.C_name,X_name
-> from sheng,city,xian
-> where
-> sheng.S_ID=city.CFather_ID and city.C_ID=xian.XFather_ID;
+-----------+--------------+-----------+
| S_name | C_name | X_name |
+-----------+--------------+-----------+
| 河北省 | 石家庄市 | 河东区 |
| 河北省 | 石家庄市 | 正定县 |
| 河北省 | 廊坊市 | 固安县 |
| 河北省 | 廊坊市 | 香河县 |
+-----------+--------------+-----------+
4 rows in set (0.00 sec) mysql>

7、连接查询
  1、内连接
    1、定义
      从表中删除与其他被连接表中没有匹配到的行
    2、语法格式
      select 字段名列表 from 表1
      inner join 表2 on 条件;
    3、练习
      1、显示省市信息,没有匹配的不显示
      2、显示省市县的信息
  2、外连接
    1、左连接
      1、定义
      以左表为主显示查询结果
    2、语法
      slect 字段名列表 from 表1 left join 表2 on 条件;
    3、练习
      1、显示省市的信息,以左表为准
      2、显示省市的信息,以右表为准
      3、显示省市区的信息,要求市全部显示
  2、右连接

 mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| bjtab |
| city |
| jftab |
| new_t2 |
| new_t2_t2 |
| sheng |
| t1 |
| t2 |
| t3 |
| tt1 |
| tt2 |
| userinfo |
| userinfo2 |
| userinfo3 |
| userinfo4 |
| xian |
+---------------+
16 rows in set (0.00 sec) mysql> select * from sheng;
+----+--------+--------------------+
| id | S_ID | S_name |
+----+--------+--------------------+
| 1 | 130000 | 河北省 |
| 2 | 140000 | 山西省 |
| 3 | 150000 | 内蒙古自治区 |
| 4 | 160000 | 辽宁省 |
| 5 | 170000 | 黑龙江省 |
+----+--------+--------------------+
5 rows in set (0.00 sec) mysql> select * from city;
+----+--------+-----------------+------------+
| id | C_ID | C_name | CFather_ID |
+----+--------+-----------------+------------+
| 1 | 131100 | 石家庄市 | 130000 |
| 2 | 131101 | 沧州市 | 130000 |
| 3 | 131102 | 廊坊市 | 130000 |
| 4 | 131103 | 衡水市 | 130000 |
| 5 | 131104 | 太原市 | 140000 |
| 6 | 131105 | 呼和浩特市 | 150000 |
| 7 | 131106 | 包头市 | 150000 |
| 8 | 131107 | 沈阳市 | 160000 |
| 9 | 131108 | 大连市 | 160000 |
| 10 | 131109 | 无锡市 | 320000 |
| 11 | 131110 | 徐州市 | 320000 |
| 12 | 131111 | 常州市 | 320000 |
+----+--------+-----------------+------------+
12 rows in set (0.01 sec) mysql> select * from xian;
+----+--------+-----------+------------+
| id | X_ID | X_name | XFather_ID |
+----+--------+-----------+------------+
| 1 | 132100 | 河东区 | 131100 |
| 2 | 132101 | 正定县 | 131100 |
| 3 | 132102 | 固安县 | 131102 |
| 4 | 132102 | 香河县 | 131102 |
| 5 | 132103 | 哈哈 | 131112 |
+----+--------+-----------+------------+
5 rows in set (0.00 sec) mysql> 1、显示省和市的信息\c
mysql>
mysql> select sheng.S_name,city.C_name
-> from sheng,city
-> where
-> sheng.S_ID=city.CFather_ID;
+--------------------+-----------------+
| S_name | C_name |
+--------------------+-----------------+
| 河北省 | 石家庄市 |
| 河北省 | 沧州市 |
| 河北省 | 廊坊市 |
| 河北省 | 衡水市 |
| 山西省 | 太原市 |
| 内蒙古自治区 | 呼和浩特市 |
| 内蒙古自治区 | 包头市 |
| 辽宁省 | 沈阳市 |
| 辽宁省 | 大连市 |
+--------------------+-----------------+
9 rows in set (0.00 sec) mysql> 2、显示省、市、县的信息\c
mysql> select sheng.S_name,city.C_name,X_name
-> from sheng,city,xian
-> where
-> sheng.S_ID=city.CFather_ID and city.C_ID=xian.XFather_ID;
+-----------+--------------+-----------+
| S_name | C_name | X_name |
+-----------+--------------+-----------+
| 河北省 | 石家庄市 | 河东区 |
| 河北省 | 石家庄市 | 正定县 |
| 河北省 | 廊坊市 | 固安县 |
| 河北省 | 廊坊市 | 香河县 |
+-----------+--------------+-----------+
4 rows in set (0.00 sec) mysql> 内连接
-> 1、显示省市信息,没有匹配的不显示\c
mysql> select sheng.S_name,city.C_name
-> from
-> sheng
-> inner join city
-> on sheng.S_ID=city.CFather_ID;
+--------------------+-----------------+
| S_name | C_name |
+--------------------+-----------------+
| 河北省 | 石家庄市 |
| 河北省 | 沧州市 |
| 河北省 | 廊坊市 |
| 河北省 | 衡水市 |
| 山西省 | 太原市 |
| 内蒙古自治区 | 呼和浩特市 |
| 内蒙古自治区 | 包头市 |
| 辽宁省 | 沈阳市 |
| 辽宁省 | 大连市 |
+--------------------+-----------------+
9 rows in set (0.00 sec) mysql>
mysql> 2、显示省市县的信息\c
mysql> select sheng.S_name,city.C_name,xian.X_name
-> from sheng inner join city
-> on sheng.S_ID=city.CFather_ID
-> inner join xian on city.C_ID=xian.XFather_ID;
+-----------+--------------+-----------+
| S_name | C_name | X_name |
+-----------+--------------+-----------+
| 河北省 | 石家庄市 | 河东区 |
| 河北省 | 石家庄市 | 正定县 |
| 河北省 | 廊坊市 | 固安县 |
| 河北省 | 廊坊市 | 香河县 |
+-----------+--------------+-----------+
4 rows in set (0.00 sec) mysql> select sheng.S_name as Sheng,city.C_name as Shi,xian.X_name as Xian from sheng inne ID=xian.XFather_ID;
+-----------+--------------+-----------+
| Sheng | Shi | Xian |
+-----------+--------------+-----------+
| 河北省 | 石家庄市 | 河东区 |
| 河北省 | 石家庄市 | 正定县 |
| 河北省 | 廊坊市 | 固安县 |
| 河北省 | 廊坊市 | 香河县 |
+-----------+--------------+-----------+
4 rows in set (0.04 sec) mysql> select sheng.S_name as Sheng,city.C_name as Shi,xian.X_name as Xian from sheng inner join city on sheng.S_ID=city.CFather_ID inner join xian on city.C_ID=xian.XFather_ID;
+-----------+--------------+-----------+
| Sheng | Shi | Xian |
+-----------+--------------+-----------+
| 河北省 | 石家庄市 | 河东区 |
| 河北省 | 石家庄市 | 正定县 |
| 河北省 | 廊坊市 | 固安县 |
| 河北省 | 廊坊市 | 香河县 |
+-----------+--------------+-----------+
4 rows in set (0.00 sec) mysql> 外连接-左连接
-> 1、显示省市的信息,以左表为准\c
mysql> select sheng.S_name,city.C_name from sheng
-> left join city
-> on sheng.S_ID=city.CFather_ID;
+--------------------+-----------------+
| S_name | C_name |
+--------------------+-----------------+
| 河北省 | 石家庄市 |
| 河北省 | 沧州市 |
| 河北省 | 廊坊市 |
| 河北省 | 衡水市 |
| 山西省 | 太原市 |
| 内蒙古自治区 | 呼和浩特市 |
| 内蒙古自治区 | 包头市 |
| 辽宁省 | 沈阳市 |
| 辽宁省 | 大连市 |
| 黑龙江省 | NULL |
+--------------------+-----------------+
10 rows in set (0.00 sec) mysql> 外连接-右连接\c
mysql> select sheng.S_name,city.C_name from sheng
-> right join city
-> on sheng.S_ID=city.CFather_ID;
+--------------------+-----------------+
| S_name | C_name |
+--------------------+-----------------+
| 河北省 | 石家庄市 |
| 河北省 | 沧州市 |
| 河北省 | 廊坊市 |
| 河北省 | 衡水市 |
| 山西省 | 太原市 |
| 内蒙古自治区 | 呼和浩特市 |
| 内蒙古自治区 | 包头市 |
| 辽宁省 | 沈阳市 |
| 辽宁省 | 大连市 |
| NULL | 无锡市 |
| NULL | 徐州市 |
| NULL | 常州市 |
+--------------------+-----------------+
12 rows in set (0.00 sec) mysql> 3、显示省市区的信息,要求市全部显示\c
mysql> select sheng.S_name,city.C_name,xian.X_name from sheng
-> right join city on sheng.S_ID=city.CFather_ID
-> left join xian on city.C_ID=xian.XFather_ID;
+--------------------+-----------------+-----------+
| S_name | C_name | X_name |
+--------------------+-----------------+-----------+
| 河北省 | 石家庄市 | 河东区 |
| 河北省 | 石家庄市 | 正定县 |
| 河北省 | 廊坊市 | 固安县 |
| 河北省 | 廊坊市 | 香河县 |
| 河北省 | 沧州市 | NULL |
| 河北省 | 衡水市 | NULL |
| 山西省 | 太原市 | NULL |
| 内蒙古自治区 | 呼和浩特市 | NULL |
| 内蒙古自治区 | 包头市 | NULL |
| 辽宁省 | 沈阳市 | NULL |
| 辽宁省 | 大连市 | NULL |
| NULL | 无锡市 | NULL |
| NULL | 徐州市 | NULL |
| NULL | 常州市 | NULL |
+--------------------+-----------------+-----------+
14 rows in set (0.00 sec) mysql>

MySQL篇,第三章:数据库知识3的更多相关文章

  1. qlserver、Mysql、Oracle三种数据库的优缺点总结

    这篇文章主要介绍了sqlserver.Mysql.Oracle三种数据库的优缺点总结,需要的朋友可以参考下   一.sqlserver优点:易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能 ...

  2. MySQL - 常见的三种数据库存储引擎

    原文:MySQL - 常见的三种数据库存储引擎 数据库存储引擎:是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧. ...

  3. MySQL篇,第一章:数据库知识1

    MySQL 数据库 1 一.MySQL概述 1.什么是数据库       数据库是一个存储数据的仓库 2.哪些公司在用数据库       金融机构.购物网站.游戏网站.论坛网站... ... 3.提供 ...

  4. Java语言程序设计(基础篇) 第三章 选择

    第三章 选择 3.8 计算身体质量指数 package com.chapter3; import java.util.Scanner; public class ComputeAndInterpret ...

  5. 使用MYSQL数据库实现编程----第二章第三章课堂知识小总结

    第二章1:创建数据库create database myschool 2.数据类型  1.整型 int  2.小数 double  精确度要求高的 ----Decimal(18,4)  2222222 ...

  6. MySQL篇,第二章:数据库知识2

    MySQL 数据库 2 名词介绍 1.DB(Database) DB就是数据库,存储数据的仓库 2.DBMS(Database Management System) 数据库管理系统 管理数据库的软件, ...

  7. MySQL高级第三章——查询截取分析

    一.查询分析 1.永远小表驱动大表 使用小的数据集驱动大的数据集. //复习 EXISTS 的知识:SELECT ... FROM tb WHERE EXISTS (subquery) 是因为前后数据 ...

  8. [转载]sqlserver、Mysql、Oracle三种数据库的优缺点总结

    一.sqlserver优点:易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能.与许多其他服务器软件紧密关联的集成性.良好的性价比等:为数据管理与分析带来了灵活性,允许单位在快速变化的环境中 ...

  9. mySQL教程 第1章 数据库设计

    E-R设计 很多同学在学SQL语句时,觉得非常困难,那是因为你在学一个你根本不了解的数据库,数据库中的表不是你设计的,表与表之间的关系你不明白.因此在学SQL语句之前,先介绍一下数据库设计. 下面举例 ...

  10. 数据库之mysql篇(1)—— 数据库管理系统简介/mysql的安装、配置

    说mysql之前,还是先说说数据库. 什么是数据库: 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后 ...

随机推荐

  1. [codechef July Challenge 2017] Chef and Sign Sequences

    CHEFSIGN: 大厨与符号序列题目描述大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串.记字符串长度为 N,大厨想要在字符串的开头.结尾,和 ...

  2. 浏览器行为模拟之requests、selenium模块

    requests模块 前言: 通常我们利用Python写一些WEB程序.webAPI部署在服务端,让客户端request,我们作为服务器端response数据: 但也可以反主为客利用Python的re ...

  3. Eclipse导出WAR包

    参考: https://jingyan.baidu.com/article/ab0b56309110b4c15afa7de2.html

  4. Nop 4.1版本已经迁移到.net core2.1版本

    1. github 下载,4.1版本,运行, install时,会让你新增后台账户密码,sql服务器 2. 在Configuration 新增Language 3. 上传中文语言包 , 你也可以先导出 ...

  5. bzoj1010

    题解: 斜率优化dp f[i]=f[j]+(i-j+sum[i]-sum[j]-L)^2 然后斜率优化 代码: #include<bits/stdc++.h> typedef long l ...

  6. AttributeError: 'cx_Oracle.Cursor' object has no attribute 'numbersAsStrings'

    转载自:https://www.wengbi.com/thread_77579_1.html 最近在本地搭建Django开发环境,Django 1.11,python 2.7.11,数据库Oracle ...

  7. Java使用POI插件将数据以excel形式备份

    将数据以表格形式进行备份 (1)导入poi的jar包 放入lib下:  WebRoot\WEB-INF\lib\poi-3.2-FINAL-20081019.jar 下载链接:https://gith ...

  8. [Leetcode 100]判断二叉树相同 Same Tree

    [题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...

  9. SQL-27 给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。 提示:在sqlite中获取datetime时间对应的年份函数为strftime('%Y', to_date)

    题目描述 给出每个员工每年薪水涨幅超过5000的员工编号emp_no.薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列. 提示:在s ...

  10. html页面技巧

    Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...});   //为S ...