SQL你必须知道的-增删改查与约束
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你必须知道的-增删改查与约束的更多相关文章
- MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存
二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...
- Android SQL语句实现数据库的增删改查
本文介绍android中的数据库的增删改查 复习sql语法: * 增 insert into info (name,phone) values ('wuyudong','111') * 删 delet ...
- Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)
day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库: 简称:DataBase ---->DB 数据库即存放数据的仓库, ...
- Sql Server数据库之四个增删改查
一.数据库的增删改查 1.新建数据库 create database students on primary ( name="students_data",--主数据文件的逻辑名 ...
- Oracle学习总结_day01_day02_表的创建_增删改查_约束
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 更新: SELECT * FROM (SELECT R ...
- SQL的分类使用(增删改查)
1.SQL的分类使用(*代表重点的程度) DDL ** (Data Definition Language)数据库定义语言 用来定义数据库对象: 库 表 列 等 DCL (D ...
- SQL介绍、语句之增删改查大全
数据库概念 文件作为数据库,数据格式千差万别 将保存数据的地方统一起来 MySQL一款应用软件 用来帮你操作文件的 只要是基于网络通信,底层都是socket!!! 服务端 -socket通信 -收发消 ...
- SQL 的简单命令(增删改查)
数据库操作的资料: 链接: https://pan.baidu.com/s/1dFl3q6X 密码: nvy7-- 增:insert into 表名 (列名) values (值) ,'dew') - ...
随机推荐
- MQTT客户端与服务代理的案列
服务端,采用 Mosquitto 来转发分发消息. 客户端自己写. 服务端 启动 mosquitto (底下的命令是我自己放到环境变量里面的,通过alias 运行mosquitto) Ishallbe ...
- SaaS系列介绍之十四: SaaS软件开发分析
1 引言 真正的问题,不是电脑是否具备思考能力,而是人类是否具备这种能力 ________B.F.Skinner<计算机科学> SaaS模式不同于传 ...
- erp中三大订单CO、PO、MO各是代表什么?
ERP即 企业资源计划 (Enterprise Resource Planning),由美国 Gartner Group 公司于1990年提出. ERP系统是指建立在信息技术基础上,以系统化的管理思想 ...
- java:装饰者模式,节点流和处理流
接口: interface Worker { public void dowork(); } 清洁工:被装饰者,节点流 class Clean implements Worker { public v ...
- 基于Struts2框架实现登录案例 之 程序国际化
国际化牵涉的知识非常多,这里只能简单的介绍,程序国际化的一般做法是:在jsp页面时, 不是直接输出信息,而是输出一个key值,该key值在不同语言环境下找到对应资源文件下的 对应信息,因此首先要创建满 ...
- PHP优化杂烩
讲 PHP 优化的文章往往都是教大家如何编写高效的代码,本文打算从另一个角度来讨论问题,教大家如何配置高效的环境,如此同样能够达到优化的目的. pool 一个让人沮丧的消息是绝大多数 PHP 程序员都 ...
- IOSTimer的例子留个备注
1.创建一个定时器 ,以下是便利构造器方法,+ scheduledTimerWithTimeInterval:invocation:repeats:+ scheduledTimerWithTimeIn ...
- C++ string类的学习
string类对于处理字符串的一些应用非常的方便,我个人感觉,string和字符数组const char *很像,而且又比字符数组用起来方便的多. 注意其删除,取子串,插入等函数里面都有一个重载版本是 ...
- Codeforces_GYM Flight Boarding Optimization
(ACM ICPC 2013–2014, NEERC, Northern Subregional Contest) Flight Boarding OptimizationInput file: fl ...
- bzoj3632
裸的最大团,随机化大法好 多次随机出一个选择顺序然后贪心即可 ..,..] of boolean; a:..] of longint; v:..] of boolean; n,m,i,j,x,y,an ...