SQL你必须知道的-增删改查与约束  
-- 插入数据   
--Insert 语句可以省略表名后的列名,但是不推荐   
insert into Class values ('' 高一一班 '', '' 快班'' )   
--Insert  [into]  表 (字段名 , 字段名) values( 值,值 )   
insert Class values( '' 高二二班'' , ''中班 '' )   
-- 如果插入的行中有些字段的值不确定,那么 Insert 的时候不指定那些列即可。   
insert into Class( cName ) values ('' 高三三班 '')   
select *   from Class   
-- 如果想student 这样的表含有很多的列的时候我们可以直接将列文件拖拽到 student 后面自动生成   
-- 日期如果不加引号会当做数值处理添加入错误的数据所以   
insert into Student( sName , sAge , sNum , sBirthday , cNum ,sSex ) values( '' 王浩'' , 12, 321324199101100136 ,''1992-01-10'' , 3, '' 男'' )   
insert into Student( sName , sAge , sNum , sBirthday , cNum ,sSex ) values( '' 张宇'' , 14, 321324199101100136 ,''1992-01-10'' , 3, '' 男'' )   
select * from student   
delete from Student   
-- 修改数据   
update Student set sSex ='' 女 ''  
-- 修改多个列只需用逗号隔开   
update Student set sSex ='' 女 '', sAge =20   
-- 更新一部分数据用 where语句表示只更新 sName 是’张宇’的行,注意 SQL中等于判断用单个 = ,而不是== 。   
update Student set sSex ='' 女 ''where sName= '' 张宇''  
--Where 中可以使用的其他逻辑运算符: or 、and 、 not、 < 、> 、 >=、 <= 、!= (或 <>)等   
update Student set cNum =2 where sAge = 50 or sAge>= 19 and sAge <=20   
-- 所有学生的英语成绩加   
update Score set english =english + 1;   
select * from Score   
-- 删除表数据   
--Delete 也可以带where 子句来删除一部分数据   
delete from student where sName = ''张飞 ''  
--truncate 会连id 号彻底的清除 这是和delete 不同的一点   
truncate table Student   
-- 或者说Truncate 清空表中的数据没有条件,和 delete 的区别不存日志,清空自动编号   
truncate table score   
-- 注意应该在每一条 ddl语句以及 dml 语句之后添加 go 否则编译运行此文件时是不能连续的执行其中的语句导致错误   
------------------- 插入测试数据 --------------------------   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''刘备 '' ,20 , ''男 '' ,123456789012345678 , ''1987-5-6'', 1 )   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''关羽 '' ,19 , ''男 '' ,123456789012345671 , ''1988-8-6'', 1 )   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''张飞 '' ,18 , ''男 '' ,123456789012345672 , ''1989-5-19'', 1 )   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''曹操 '' ,22 , ''男 '' ,123456789012345673 , ''1985-12-6'', 1 )   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''夏侯惇 '' ,22 , ''男 '' ,123456789012345674 , ''1985-3-6'', 1 )   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''华佗 '' ,50 , ''男 '' ,12345678901234565 , ''1957-1-16'', 1 )   
insert into Student ( sName, sAge ,sSex , sNum, sBirthday ,cnum ) values ( ''甄姬 '' ,18 , ''女 '' ,12345678901234565 , ''1989-8-8'', 1 )   
insert into Score ( studentId, english ) values (1 , 90)   
insert into Score ( studentId, english ) values (2 , 90)   
insert into Score ( studentId, english ) values (3 , 59)   
insert into Score ( studentId, english ) values (4 , 100)   
insert into Score ( studentId, english ) values (5 , 60)   
insert into Score ( studentId, english ) values (6 , 0)   
insert into Score ( studentId, english ) values (7 , 80)   
-- 练习   
-- 插入几条老师信息和成绩   
insert into teacher( tName , tSex , tAge , tSalary , tBirthday ) values ('' 朱德军 '', '' 女'' , 18, 3000 ,''1989-2-3'' )   
insert into teacher( tName , tSex , tAge , tSalary , tBirthday ) values ('' 徐连龙 '', '' 男'' , 18, 3000 ,''1984-2-3'' )   
insert into teacher( tName , tSex , tAge , tSalary , tBirthday ) values ('' 杨会玉 '', '' 女'' , 18, 3000 ,''1988-2-3'' )   
insert into Score ( studentId, english, math) values (1 , 29, 34 )   
-- 练习:给刘备的英语成绩加分   
select sId   from Student where sname= '' 刘备''  
update Score set english = english+ 10 where studentId =(select sId  from Student where sname= '' 刘备'' )   
select * from score   
-- 练习:考试题偏难,所有人的成绩加分   
update Score set math = math+ 5   
-- 练习:所有女学生的成绩加分   
update Score set english = english+ 5 where studentId = (select sid from Student where ssex= '' 女'' )   
-- 删除工资大于的老师   
delete from Teacher where tSalary >2000   
-- 删除所有老师   
delete from Teacher   
-- 删除数据时候 把自增长列的值还原成种子   
truncate table teacher   
-- 约束constraint   
-- 数据库约束是为了保证数据的完整性 ( 正确性) 而实现的一套机制   
-- 非空约束   
-- 主键约束(PK) primary key constraint 唯一且不为空   
-- 唯一约束(UQ)unique constraint 唯一,允许为空,但只能出现一次   
-- 默认约束(DF)default constraint 默认值   
-- 检查约束(CK)check constraint 范围以及格式限制   
-- 外键约束(FK)foreign key constraint 表关系   
  
-- 主键约束   
-- 假设Student 表创建时没有设置主键现在就需要添加 Primary Key 的约束 constraint中文为约束的意思   
alter table Student   
add constraint PK_Student primary key ( sId)   
-- 如果存在多个主键的话 primary key (sId,****) 逗号分隔   
-- 唯一约束   
-- 如果表中的 sNum列已经存在重复的值了那么这里始终会报错 需要删除重复数据再来执行此操作   
alter table Student   
add constraint UQ_Student_sNum unique (sNum )   
-- 默认约束   
alter table Student   
add constraint DF_Student_Sex default ('' 男 '') for sSex   
-- 注意For 后面的字段名称是不要括号的   
--default 默认值   
insert into Student ( sName, sAge ,sSex , cNum, sNum )values ( ''王浩 '' ,default , default, 0900261 ,52 )   
-- 检查约束   
-- 如果事先库中已有不符合约束的数值那么执行是不通过的需要修改之后   
alter table Student   
add constraint CK_Student_sSex check (sSex = ''男 '' or sSex ='' 女 '')   
-- 约定年龄大于 如果插入数据小于则会不通过   
alter table Student   
add constraint CK_Student_sAge check (sAge >= 18)   
-- 外键约束Student 的 cNum与 Class 中的CId 建立外键关系如果 Cid 没有设置成为主键那么是不通过的   
alter table Student   
add constraint FK_Student_cNum foreign key ( cNum) references Class (cId )   
-- 删除约束   
alter table Student   
drop constraint CK_Student   
-- 级联删除一般少用   
--on delete cascade   
-- 级联更新   
--on update cascade   
--cascade 级联删除的意思 删除主表数据时会首先删除子表对应的数据   
-- 添加外键约束之后容易出现的问题   
-- 在子表Student 中添加的 Cnum必须在 class 表中出现过否则不通过   
insert into Student ( sName, cNum )values ( ''wnaghao'', 100 )   
-- 删除主表中的数据时 必须先删除子表中对应的数据 解决方法在建立外键约束时添加 on delete cascade 级联删除会首先删除子表对应数据后再删除父表数据   
alter table Student   
add constraint FK_Student_cNum foreign key ( cNum) references Class (cId )   
on delete cascade  
-- 约束练习   
--tSex  控制只能是男女,默认男   
--tAge  在之间 默认   
--tName 唯一   
alter table Teacher   
add constraint CK_Teacher_tSex check (tSex = ''男 '' or tSex ='' 女 ''),   
  constraint DF_Teacher_tSex default ( ''男 '' ) for tSex ,   
  constraint CK_Teacher_tAge check ( tAge>= 30 and tAge <=40 ),   
  constraint DF_Teacher_tAge default ( 30) for tAge ,   
  constraint UQ_Teacher_tName unique ( tName)   
--Score 表中   
--studentId 是外键   先要把 Student表中的 sId 设置为主键   
alter table Score   
add constraint Fk_Score_studentId foreign key ( studentId) references Student (sId )   
-- 在学生表中删除有成绩的学生   
-- 成绩表中添加学生表中没有的学生   
-- 在创建表的过程中就创建约束   
create table Student1   
(   
       sId int identity( 1 ,1 ) primary key,   
       sName nvarchar ( 20) not null,   
       sAge int constraint CK_Student1_sAge check ( sAge>= 18 ) constraint DF_Student1_sAge default ( 18),   
       scId int constraint Fk_Student1_scid foreign key ( scId ) references Class (cId )   
)   
-- 但是虽然可以简化代码但是修改查看比较的麻烦一般建议还是分开写   
-- 这里添加都用 add 修改都用 alter 删除都用drop   
-- 注意删除列要有 column   
alter table Student1   
drop column scId

SQL你必须知道的-增删改查与约束的更多相关文章

  1. MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存

    二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...

  2. MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存

    目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...

  3. Android SQL语句实现数据库的增删改查

    本文介绍android中的数据库的增删改查 复习sql语法: * 增 insert into info (name,phone) values ('wuyudong','111') * 删 delet ...

  4. Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)

    day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库:    简称:DataBase ---->DB    数据库即存放数据的仓库, ...

  5. Sql Server数据库之四个增删改查

    一.数据库的增删改查 1.新建数据库 create database students on primary ( name="students_data",--主数据文件的逻辑名 ...

  6. Oracle学习总结_day01_day02_表的创建_增删改查_约束

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 更新: SELECT * FROM (SELECT R ...

  7. SQL的分类使用(增删改查)

    1.SQL的分类使用(*代表重点的程度)    DDL ** (Data Definition Language)数据库定义语言        用来定义数据库对象: 库 表 列 等    DCL (D ...

  8. SQL介绍、语句之增删改查大全

    数据库概念 文件作为数据库,数据格式千差万别 将保存数据的地方统一起来 MySQL一款应用软件 用来帮你操作文件的 只要是基于网络通信,底层都是socket!!! 服务端 -socket通信 -收发消 ...

  9. SQL 的简单命令(增删改查)

    数据库操作的资料: 链接: https://pan.baidu.com/s/1dFl3q6X 密码: nvy7-- 增:insert into 表名 (列名) values (值) ,'dew') - ...

随机推荐

  1. iOS开发--多线程

    前面在<Bison眼中的iOS开发多线程是这样的(二)>一文中讲完了多线程的NSThread,不难发现这种方式的多线程实现起来非常的复杂,为了简化多线程的开发,iOS提供了GCD来实现多线 ...

  2. Photoshop:制作方块背景

    1.填充背景色 2.滤镜->杂色->添加杂色 3.滤镜->像素化->马赛克 4.添加横线,明度-10 最终效果 附: 利用:查找边缘.最小值,还可以做这样的效果

  3. C++:默认的构造函数

    注意:如果类中用户没有定义构造函数,系统会自动提供一个函数体为空的默认构造函数. 但是,只要类中定义了一个构造函数(不一定无参构造函数),系统将不再给它提供 默认的构造函数.因为,默认的构造函数被类中 ...

  4. Linux 查看版本详情

    内核版本的信 uname -a -a选项表示察看所有的信息,但是从输出信息可以看出来,uname看到的版本信息,只是内核版本的信息,而不是发行版的版本信息 查看发行版信息 $cat /etc/issu ...

  5. 关于imx6核心板qt系统U盘挂载

    在使用imx6核心板开发的时候,程序写到U盘,想通过U盘在板子上运行程序,U盘插到板子上后在minicom中有信息显示,但是无法找到挂载文件,/dev和/mnt下都没有找到相应的文件.后来百度后发现U ...

  6. 高难度(3)RenderScript

    RenderScript RenderScript is a framework for running computationally intensive tasks at high perform ...

  7. [Codeforces137A]Postcards and photos(模拟)

    题目链接:http://codeforces.com/contest/137/problem/A 题意:一个人搬东西,每次只能搬相同的东西,最多只能搬相同的东西不超过5个.问最少搬多少次. 模拟就行了 ...

  8. 函数ut_2_log

    计算某个数的对数(最大的) 例如 16 计算后为 4 2的4次方为16 例如15 计算后为3 2的3次方为8 /******************************************** ...

  9. Mysql分支

    MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这么重要 ...

  10. atoi&itoa

    char* itoa(int num,char*str,int radix) {/*索引表*/ char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVW ...