zoukankan      html  css  js  c++  java
  • mysql DDL数据定义语言

    DDL数据定义语言

    本节涉及MySQL关键字:create、alter(rename,add,chang,modify,drop)、drop、delete、truncate等。

    -- 创建表
    -- 数据类型:int,date,varchar(size),[ decimal(5,2),小数点后两位]
    格式:create table tableName
                   (
                      columnName datatype,
                      columnName datatype,
                      .......
    );

    拷贝表格

    格式 :  create table tableName
               as
               subquery;或者在subquery后加(where 1=2),表示只保留格式

    创建表实例: create table supermarket
                             (
                                market_id int,
                                market_name varchar(50),
                                address varchar(200),
                                contact varchar(15),
                                manager int
                              );

    拷贝表实例:create table supermarkets_copy
                     as
                     select *
                     from supermarkets;

    -- 修改表:alter
           -- 修改表名
                 格式:  alter table tableName
                                 rename to new_tableName;

                    实例:   alter table supermarket 

                                    rename to supermarkets;


           -- 添加列
                格式 : alter table tableName
                                 add columnName defined [ FIRST | AFTER column_name ];
                           alter table tableName
                                 add columnName defined [ FIRST | AFTER column_name ],
                                 add columnName defined [ FIRST | AFTER column_name ],
                                 ...;

                   实例:  alter table supermarkets

                                   add kind int;

     

          -- 修改列
            -- 修改列名
                格式:  alter table tableName
                                change column old_name new_name defined;

                   实例:  alter table supermarkets
                                     change kind category varchar(10);


         -- 修改列定义
                格式:  alter table tableName
                                  modify columnName new_defined [ FIRST | AFTER column_name ];
                          alter table tableName
                                  modify columnName new_defined [ FIRST | AFTER column_name ],
                                  modify columnName new_defined [ FIRST | AFTER column_name ],
                                  ....;

                       实例: alter table supermarkets

                                         modify category int;

       -- 删除列

               格式;   alter table tableName
                                  drop column columnName;

                       实例: alter table supermarkets

                                           drop column category;

    -- 删除表
               格式: drop table tableName;

          --删除表的内容,保留格式:          

               格式:delete from tableName;  后面可以加限制条件where1=2或1=1。     

    -- truncate: 清空表(删除后无法返回上一步)
                      格式:truncate table tableName;
                                     select * from emp;

    -- 创建表上的约束
    -- 主键约束:标志行的唯一性,只能有一个,而且不能为空
    -- 外键约束:建立两张表的关联关系
    -- 唯一约束:标志其唯一性,但数量不限的,可以为空
    -- 非空约束:必填项

    -- 默认

    如果创建表的时候添加约束,可直接去掉alter语句

    创建表上的约束, (主键,外键,唯一)约束用add constraint,

                          (非空,默认)约束用modify.

                  格式: alter table tableName
                                       add constraint_defination;
                         实例: alter table supermarkets
                                         drop primary key;

       主键约束:  primary key          

                         实例:alter table supermarkets
                                      add constraint pk_market_id primary key(market_id);

       唯一约束: unique  

                         实例: alter table supermarkets
                                      add constraint uq_market_name unique(market_name);

             

       外键约束:foreign key
                         实例:  alter table emp
                                        add constraint fk_market_id foreign key (market_id)
                                        references supermarkets(market_id);

          两个外键的约束实例:  add constraint fk_market_id_customer_id foreign key (market_id,customer_id)
                                        references supermarkets(market_id),customer(customer_id);

                       delete from supermarkets where market_id = 1; -- 维护数据的完整性

        非空约束not null

                         实例: alter table supermarkets
                                         modify market_name varchar(50) not null,
                                         modify address varchar(200) not null;

        默认值:

                        实例:  alter table supermarkets
                                         modify market_name varchar(50) not null,
                                         modify address varchar(200) not null default '苏州店';

           

    -- 视图
         -- 创建或者更新视图
                 格式:-- create or replace view viewName
                         -- as
                         -- subquery;

                    实例: create view cust_view
                             as
                             select customer_id,customer_name,address,province,city,sale_employee_id,credit_limit
                             from customer;

                             select *
                             from cust_view;

                   实例: create or replace view purchase_view
                            as
                            select `order`.order_number,customer.customer_name,product.product_name,buy_number,format(order_detail.price,2)                                   fmt_price,format(order_detail.total_money,2) fmt_total_money
                            from `order`
                            left join order_detail on `order`.order_id = order_detail.order_id
                            left join customer on `order`.customer_id = customer.customer_id
                            left join product on order_detail.product_id = product.product_id;

                            select *
                            from purchase_view
                            where order_number = '321154103';


         -- 删除视图
                 格式:-- drop view [if exists] view_name;

                     实例drop view if exists cust_view;

    -- 现有一个商店的数据库,记录客户及其购物情况,由下面三个表组成:
    -- 商品goods(商品号goodsId,商品名goodsName,单价unitprice,商品类别category,供应商provider);
    -- 客户customer(客户号customerId,姓名name,地址address,电邮email,性别sex,身份证cardId);
    -- 购买purchase(客户号customerId,商品号goodsId,购买数量nums);
    -- 请用SQL语言完成下列功能:
    -- 1. 建表,在定义中要求声明:
    -- (1). 每个表的主外键;
    -- (2). 客户的姓名不能为空值;
    -- (3). 电邮不能够重复;
    -- (4). 客户的性别默认是男;
    create table goods
    (
          goodsId int not null,
          goodsName varchar(50),
          unitprice decimal(5,2),
          category varchar(30),
          provider varchar(30),
          constraint pk_goodsId primary key(goodsId)
    );

    create table customer
    (
          customerId int not null,
          name varchar(50),
          address varchar(200),
          email varchar(20),
          sex varchar(6),
          cardId varchar(20),
          constraint pk_customerId primary key(customerId)
    );

    create table purchase
    (
          customerId int not null,
          goodsId int not null,
          nums int,
          constraint pk_customerId_goodsId primary key(goodsId,customerId),
          constraint fk_customerId foreign key(customerId)
          references customer(customerId),
          constraint fk_goodsId foreign key(goodsId)
          references goods(goodsId)
    );

        alter table customer
        modify name varchar(50) not null;

    -- 约束
    -- 主键:primary key
    -- 外键:foreign key
    -- 唯一:unique
    -- 非空:not null

    alter table customer
    add constraint uq_email unique(email);

    alter table customer
    modify sex varchar(6) default '男';

  • 相关阅读:
    js 对象合并
    python3 TypeError: 'str' does not support the buffer interface in python
    django rest framework 再撸体验
    linux shell输入重定向
    httpie 取代 curl
    wget 断点续传 & nginx文件服务器
    select2 demo
    vmare centos 6.8 minimal 无法上网
    protocol http not supported or disabled in libcurl apt-get
    python3 -pip
  • 原文地址:https://www.cnblogs.com/wanglisong/p/6903162.html
Copyright © 2011-2022 走看看