zoukankan      html  css  js  c++  java
  • sql server案例总结

    --通过insert select 插入数据到已存在的表中
    create table grade1 
    (
    id int ,
    name varchar(50)
    )
    
    insert into grade1 select * from grade 
    
    select * from grade1
    
    --通过select into 语句将现有表中的数据添加到新表中,执行两边的话(数据库中已存在名为 'Adress' 的对象。
    select gradeid,gradename into Adress from grade 
    
    select * from Adress
    
    --插入多行数据,通过union进行插入数据
    --union all一次插入多行数据
    --union 一次只插入一条数据
    insert into grade (gradename)
    select 'Y2'union all
    select 'Y2'union all
    select 'Y2'union all
    select 'Y2'union all
    select 'Y2'
    
    -------5行受影响
    insert into grade (gradename)
    select 'Y21'union 
    select 'Y22'union 
    select 'Y23'union 
    select 'Y24'union 
    select 'Y25'
    
    --一行受影响,因为值是重复的,union all直接进行插入,不管你是不是重复的
    insert into grade (gradename)
    select 'Y2'union all
    select 'Y2'union all
    select 'Y2'union all
    select 'Y2'union all
    select 'Y2'
    
    select * from grade
    
    --默认值的插入方法,邮箱、手机号、性别都是默认值
    --注意,前面没有列名的话所对应的默认值必须要加default,否则会报错的
    select * from student
    insert into student values('12452','123456','于谦',1,default,'山东济南','1996-06-09',default,26)
    --下面的这句话就可以
    insert into student(studentno,loginpwd,studentname,gradeid,phone,address,borndate,email,age)  values('124521','123456','于谦',1,default,'山东济南','1996-06-09',default,26)
    
    --修改数据
    update grade set gradename = 'hs' where gradeid = 12
    --无法更新标识列
    update grade set  gradeid = 20, gradename = 'hs' where gradeid = 12
    
    --删除数据
    select * from student
    --删除不了,DELETE 语句与 REFERENCE 约束"FK_result_student"冲突。该冲突发生于数据库"myschool",表"dbo.result", column 'studentno'。因为成绩表里面有该学生的成绩
    delete from student where studentno = '3'
    select * from result
    --先删除字表的数据
    delete from result where studentno = '3'
    
    --使用truncate删除表中的数据,注意标识列会重新开始
    --只能全表删除。这个不是简单的删除记录,这个命令叫删减表,内部机制与delete是不一样的。
    --truncate table student
    
    select * from grade
    
    select * from student
    
    

  • 相关阅读:
    关于Linux启动时挂载rootfs的几种方式【转】
    文件子系统-(rootfs)根文件系统挂载流程03【转】
    linux根文件系统挂载过程【转】
    linux的initrd机制和initramfs机制之根文件挂载流程:代码分析【转】
    ramdisk配置、解压、创建rootfs、启动简单分析【转】
    linux文件系统初始化过程(4)---加载initrd(中)【转】
    Linux内核学习:EXT4 文件系统在 Linux 内核系统中的读写过程【转】
    Flyway数据库版本管理工具的使用
    RabbitMQ消息发送与接收
    RabbitMQ简介以及安装
  • 原文地址:https://www.cnblogs.com/a1111/p/12816188.html
Copyright © 2011-2022 走看看