Database Advanced 3

index

1、普通索引(index)
2、唯一索引(unique)
    1、使用规则
        1、一个表中可以有多个unique字段
        2、unique字段的值不允许重复,但可以为空值
        3、unique的key标志是UNI
    2、创建唯一索引
        1、创建表时创建
            1、方式一
                create table t1(
                id int(3) zerofill,
                name char(20) unique
                );
            2、方式二
                create table t2(
                id int,
                name char(20),
                age tinyint,
                unique(name),
                unique(age)
                );
        2、在已有表中创建
            create unique index 索引名 on 表名(字段名);
    3、删除唯一索引(unique)
        drop index 索引名 on 表名;
        删除普通索引、唯一索引只能一个一个删

primary key

    1、使用规则
        1、一个表中只能有一个主键(primary)字段
        2、对应字段的值不允许重复,且不能为空值
        3、主键字段的KEY标志为PRI
        4、把表中能够唯一标识一条记录的字段设置为主键字段
    2、创建主键(primary key)
        1、在创建表时创建
            1、方式一:字段名 数据类型 primary key,
                create table t3(
                id int primary key,
                name char(20)
                );
            2、方式二:
                create table t4(
                id int,
                name char(20),
                primary key(id)
                );
        2、在已有表中创建主键
            alter table 表名 add primary key(字段名);
        3、删除主键
            如果主键带自增长属性,则先要删除自增长属性,再删除主键
            alter table 表名 modify 字段名 数据类型;
            alter table 表名 drop primary key;
    3、自增长属性(auto_increment)
        1、作用:通常和主键一起配合使用
        2、创建表时添加自增长属性
            字段名 数据类型 primary key auto_increment
            create table t5(
            id int primary key auto_increment,
            name char(15)
            );
        3、在已有表中添加自增长属性
            alter table 表名 modify 字段名 数据类型 primary key auto_increment;

2. Data import
1. Function
Import the contents of the file system into the database
2. Syntax
load data infile "file name"
into table table name
fields terminated by "separator"
lines terminated by "\n"
3. Exercise
/etc The content in the /passwd file is imported into the userinfo table under the library day03

    tarena : x  :  1000  :  1000  :  tarena,,,
    用户名  密码   UID      GID           用户描述
    :/home/tarena  :  /bin/bash
     主目录                        登录权限
4、操作步骤
    1、在数据中创建对应的表
    2、将要导入的文件拷贝到数据库的默认搜索路径中
    3、将系统文件导入到创建的表中

    1、创建表
        create table userinfo(
        username char(20),
        password char(1),
        uid int,
        gid int,
        comment varchar(50),
        homedir varchar(50),
        shell varchar(50)
        );
    2、将要导入的文件拷贝到数据库的默认搜索路径中
        1、如何查看数据库的默认搜索路径
            show variables like "secure_file_priv";
        2、sudo cp /etc/passwd /var/lib/mysql-files/
    3、执行数据导入语句
    load data infile "/var/lib/mysql-files/passwd"
    into table userinfo
    fields terminated by ":"
    lines terminated by "\n"

    4、在userinfo表中第一列添加一个id字段,类型为int,设置为主键带自增长属性
        alter table userinfo add id int primary key auto_increment first
5、练习2
    导入AID1709.csv成绩表
    1、创建对应的表
        create table AID1709(
        id int,
        name varchar(15),
        score float(5,2),
        phone char(11),
        class char(7)
        )default charset=utf8;
    2、拷贝到数据库目录下
        sudo cp /home/tarena/AID1709.csv /var/lib/mysql-files/
    3、执行数据导入语句
        load data infile "/var/lib/mysql-files/AID1709.csv"
        into table AID1709
        fields terminated by ","
        lines terminated by "\n"
6、注意Ubuntu中文件的权限问题
    1、sudo -i
    2、cd /var/lib/mysql-files
    3、ls -l AID1709.csv
    4、chmod 644 AID1709.csv
    5、ls -l AID1709.csv ## 有了r权限
    6、执行导入语句
        load data infile "/var/lib/mysql-files/AID1709.csv"
        into table AID1709
        fields terminated by ","
        lines terminated by "\n";

4. Data export
1. The function
saves the records in the database table to the system file
2. The syntax format
select ... from table name
into outfile "file name"
fields terminated by "separator"
lines terminated by "\n"
3 , Exercise
1, export the three fields of username, password and uid in the userinfo table, the file name is user.txt
select username,password,uid from userinfo
into outfile "/var/lib/mysql-files/user.txt "
fields terminated by " "
lines terminated by "\n"
2. Export the values ​​of the user and host fields in the user table under the mysql library to user2.txt, and store them in the database search path
select user, host from mysql.user
into outfile "/var/lib/mysql-files/user2.txt"
fields terminated by " "
lines terminated by "\n";
4. Note
1. The exported content is completely determined by the SQL query statement
. 2. The path must be specified in the corresponding database search path when executing the export command.
show variables like "secure_file_priv";

copy of table

1、表的复制
    1、语法
        create table 表名 select 查询语句;
    2、练习
        1、复制userinfo表中全部记录和字段,userinfo2
            create table userinfo2 select * from userinfo;
        2、复制userinfo表的前10条记录,userinfo3
            create table userinfo3 select * from userinfo limit 10;
        3、复制userinfo表的username,password,uid三个字段的第2-10条记录,userinfo4
            create table userinfo4 select username,password,uid from userinfo limit 1,9;
2、复制表结构
    1、语法格式
        create table 表名 select 查询语句 where false;
    2、注意
        复制表的时候不会把原表的 key 属性复制过来
3、练习
    1、创建user1表,包含userinfo表中username,uid两个字段的前2条记录
        create table user1 select username,uid from userinfo limit 2;
    2、创建user2表,包含userinfo表中gid,homedir两个字段的前3条记录
        create table user2 select gid,homedir from userinfo limit 3;

6. Nested query 1. Define
the query
result of the inner layer as the query condition of the outer
layer The username and uid less than the average value of this field are displayed select username,uid from userinfo where uid < (select avg(uid) from userinfo); 7. Multi-table query 1, two ways 1, select field name list from table name List; Cartesian product select * from user1,user2; 2. Select field name list from table name list where condition; 3. Exercise 1. Display detailed information of provinces and cities select sheng.s_name,city.c_name from sheng,city where sheng. s_id=city.cfather_id; 2. Display detailed information of provinces, cities and counties -> select sheng.s_name,city.c_name,xian.x_name -> from sheng,city,xian -> where -> sheng.s_id=city.cfather_id and - > city.c_id=xian.xfather_id; 8. Connection query



















1. Inner join
1. Definition to delete all rows from the table that
do not match other joined
tables It does not display when arriving -> 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;






outer join

    1、左连接
        1、定义
            以左表为主显示查询结果
        2、语法
            select 字段名列表 from 表1 
            left join 表2 on 条件;
        3、练习
            1、显示省市详细信息,以省表为主显示
                select sheng.s_name,city.c_name from sheng left join city on sheng.s_id=city.cfather_id;
            2、显示省市县详细信息,要求市全部显示
                -> 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;
    2、右连接
        以右表为准显示查询结果,用法同左连接

ER Model & ER Diagram

1、定义
    ER模型即实体-关系模型,ER图即实体-关系图
2、三个概念
    1、实体
        1、定义:现实世界中任何可以被认知、区分的事物
        2、示例
            1、学校 :学生、教师、课程、班主任
            2、企业 :职工、产品
    2、属性
        1、定义:实体所具有的特性
        2、示例
            1、学生属性:学号、姓名、年龄、性别
            2、产品属性:产睥睨编号、名称、规格
    3、关系
        1、定义:实体之间的联系
        2、分类
            一对一关系(1:1) 班级和班长
            一对多关系(1:n) 公司和职工
            多对多关系(m:n) 学生和课程
    4、ER图的绘制
        1、矩形框代表实体,菱形框代表关系,椭圆形代表属性

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324578082&siteId=291194637