zoukankan      html  css  js  c++  java
  • sql server 增删改(查太多了)

    表:

    学生(*学号,姓名,性别,年龄,专业)
    
    create table student(
    
       sno char(13) primary key,
    
       sname varchar(20) not null,
    
       ssex char(2),
    
       sage smallint,
    
       sdept varchar(30)
    
    );
    
     
    
     
    
    课程(*课程号,课程名,学分)
    
    create table course(
    
       cno char(4),
    
       cname varchar(40) not null,
    
       ccredit smallint not null,
    
       我们可以将字段的定义和主外键的定义分开
    
       primary key (cno)
    
    );
    
     
    
     
    
    选课(学号,课程号,分数)
    
    create table sc(
    
       sno char(13),
    
       cno char(4),
    
       grade smallint,
    
     
    
       primary key (sno,cno),--定义联合主键
    
       foreign key (sno) references student(sno),
    
       constraint FK_sc_cno foreign key (cno) references course(cno)
    
    );
    
     
    
    创建一个用户表
    
    create table tb_user(
    
       userid int identity(1,1),【设置整型字段自动增长】
    
       username varchar(20) not null,
    
       userpass varchar(16) not null,
    
       groupid int
    
    );
    
    创建用户组表
    
    create table tb_group(
    
       groupid int primary key identity(1001,1),
    
       groupname varchar(30) not null
    
    );

    insert(增加)

    使用 insert 语句向表中插入数据。
    
    insert into table [(column [, column...])]
    
    values (value [, value...]);
    
    插入的数据应与字段的数据类型相同。
    
     
    
    举例:
    
    方法一:不指定列,插入所有字段
    
    insert into student values('2010040','kangji','',22,'计算机科学学院');--SQLServer总是尝试转化为相同的类型
    
    insert into student values(20100402,'张三','',22,'计算机科学学院');
    
    方法二:指定列,插入部分字段
    
    insert into student (sno,sname) values('20100403','李四');
    
     
    
    注意:
    
    1)      数据的大小应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。
    
    2)      在values中列出的数据位置必须与被加入的列的排列位置相对应。
    
    3)      字符和日期型数据应包含在单引号中。
    
    4)      插入空值,不指定或insert into table value(null)
    
    注意:在SQLServer 中,''=null; ' '=null; '   '=null;
    
     
    
    批量插入数据
    
    insert into u(username,userpass) select sname,sno from student where ssex=''; 

    update(修改)

    使用 update语句修改表中数据。
    
     
    
    update 表名 set 列名=表达式[,列名=表达式 ...] [where where_definition]   
    
     
    
    update语法可以用新值更新原有表行中的各列。
    
    set子句指示要修改哪些列和要给予哪些值。
    
    update student set sname='康吉' where sno='20100401';
    
     
    
    update student set sname='康吉',sage=23 where sno='20100401';
    
     
    
    where子句指定应更新哪些行。如没有where子句,则更新所有的行。
    
     
    
    修改还有 null 值的数据 is null
    
    select * from student where ssex is null;
    

    delete(删除)

    使用 delete语句删除表中数据。

    delete from 表名 [where where_definition]

    如果不使用where子句,将删除表中所有数据。

    delete语句不能删除某一列的值(可使用update对值置null)

    使用delete语句仅删除记录,不删除表本身。如要删除表,使用【drop table表名】语句。

    同insert和update一样,从一个表中删除记录将引起其它表的参照完整性问题,在修改数据库数据时,头脑中应该始终不要忘记这个潜在的问题。

    删除表中全部数据

    delete table 表名; 

    删除表中指定数据

    delete from student where xh='A001';

    级联删除和更新

    create table class(
    
       id int primary key,
    
       name varchar(10)
    
    );
    
     
    
    create table student(
    
       id int primary key,
    
       class_id int references class(id) on delete/update cascade
    
    );
    
     
    
    alter table student add constraint FK_classid foreign key (class_id) references class(id) on update cascade on delete cascade
    
     
  • 相关阅读:
    java生成验证码
    SpringBoot定时任务
    事务管理
    Windows 2008 Server R2双网卡负载均衡
    HP Proliant DL580 gen9 阵列卡P440AR 高速缓存 被禁用
    Kali Debian 修改时区
    First Py From Py
    C++头文件#include<bits/stdc++.h>
    排序算法
    运算符优先级
  • 原文地址:https://www.cnblogs.com/yijieyufu/p/11985873.html
Copyright © 2011-2022 走看看