SQL中几个常用的命令

来源:互联网 发布:广西南宁移动网络缴费 编辑:程序博客网 时间:2024/05/22 00:19
1. 复制表结构
create table new_table like old_table;
2. 复制表结构和内容:
create table new_table select * from old_table;
3. 备份表:
    1. 如果2个表结构一样的话:
            insert into class_backup select * from class;
    2. 如果有部分字段一样:

            insert into class_backup(c_id, c_name, t_id, time) select c_id,c_name, t_id, '2012-01-01' from class;


另外介绍一下内连接和外连接

例子:   
-------------------------------------------------
  a表     id   name     b表     id   job   parent_id   
              1   张3                   1     23     1   
              2   李四                 2     34     2   
              3   王武                 3     34     4       
  a.id同parent_id   存在关系   
--------------------------------------------------    
 1) 内连接   
  select   a.*,b.*   from   a   inner   join   b     on   a.id=b.parent_id       
  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   


  2)左连接   
  select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id       
  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   
  3   王武                  null   


 3) 右连接   
  select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id       
  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   
  null                       3     34     4   


 4) 完全连接   
  select   a.*,b.*   from   a   full   join   b     on   a.id=b.parent_id   
  结果是     
  1   张3                  1     23     1   
  2   李四                 2     34     2   
  null                   3     34     4   
  3   王武                 null


0 0
原创粉丝点击