SQL 必知必会-- 第17课:创建和操作表

我这里用的是oracle 10g,PL/SQL来做的。

第17课  创建和操纵表  145
17.1  创建表  145
17.2  更新表  150
17.3  删除表  153
17.4  重命名表  153
17.5  小结  154

第17章 创建和操纵表
  17.1 创建表
            17.1.1 表创建:所有列放在小括号里面,各列之间用英文逗号隔开最后一列后面没有逗号,小括号后面以英文分号结束
                              create table vendors(
                                   vend_id         char(10) not null ,
                                   vend_name    char(50) not null ,
                                   vend_address char(50) null ,
                                   vend_city      char(50) null ,
                                   vend_state    char(5)  null ,
                                   vend_zip       char(10) null ,
                                   vend_country char(50) null
                              );  
            17.1.2 使用null值 :
                              a,注意null值表示没有值或者缺值。空字符串('')是一个有效的值,它不是无值。
                              b,表里面的每一列要么是null列,要是是非null列,这是在创建表的时候,表的定义规定的。默认的是null.
                              c,只用指定不允许为null的列才可用于主键。主键是一列(或一组列),其值能够唯一标识表中的每一列。
                          
            17.1.3 制定默认值:default
                              默认值常用于日期或者时间戳列,通常是把系统日期用作默认日期。
                              获得系统日期
                                MySQL用户:select sysdate();
                                Oracle用户: select sysdate from dual;
  17.2 更新表 :使用alter table时需要考虑的内容。
                    假如现有表student,
create table student(                       
  snum number not null,                       
  sname varchar2(100),                       
  age number                    
);
                    对现有表新增一列或者多列:
                    alter table student add sex char(1);
                    alter table student add (address varchar2(100), email varchar2(50));

                    对现有表新增加主键:alter table 表名 add constraint 约束名 primary key(列名);
                    alter table student add constraint pk_student primary key(snum);
    
                    对现有表删除其中的某一列:alter table 表名 drop column 列名;     alter table student drop column age;
                   
            复杂的表结构更改一般需要手动删除过程,它涉及以下步骤:
                 1、复制旧表到一个新表,create table new_table as select * from old_tables;
                 2、检验包含所需数据的新表
                 3、重命名旧表(如果确定,可以删除它)
                 4、用旧表原来的名字重新命名新表
                 5、根据需要,重新创建新表的触发器、存储过程、索引和外键。
 17.3 删除表 :删除整个表,而不是删除表里面的内容:drop table 表名;      drop table student;           
删除某列:alter table 表名 drop column 列名;     alter table student drop column age;  
 
17.4 重命名表名 : alert table 现表名 rename to 新表名;    alter table student rename to test123;          
          修改列名:alert table 表名 rename colunm 列名 to 新列名;   alter table student rename column name to sname;
*********************************************************************************************************
这一课练习所用到的sql有: 创建表 
create table student(
       id number not null,
       name varchar2(100),
       age number
);
 
alter table student add sex char(1);    新增一列
alter table student add (address varchar2(100), email varchar2(50));   新增多列
alter table student add constraint pk_student_snum primary key(id);   新增主键
alter table student rename column name to sname;    修改列名
alter table student rename to test123;   修改表名
alter table student drop column age;   删除某一列
drop table student;  删除表
原文地址:https://www.cnblogs.com/wxc-xiaohuang/p/3442171.html