数据库语句大全!!要举一反三!

2024-02-18 11:48

本文主要是介绍数据库语句大全!!要举一反三!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

                Sunny个人整理!(sunny我的英文名)

 

创建数据库

create database student

on

(name=student_data,

 filename='c:/data/student_data.mdf',

 size=3,

 filegrowth=1,

 )

log on

(name=stu_log,

 filename='c:/../stu_log.ldf',

 size=1,

 maxsize=20,

filegrowth=10%)

--创建数据库,及数据文件和日志文件

 

创建表

create table stu_grade

(stu_id  char(10) not null,

--stu_id  char(10) not null primary key,--其他表直接定义主键]

 

 course_id  char(3)  not null,

 grade  int,

 

 constraint     pk_no1    primary key(stu_id ,course_id),

 constraint  fk_stu_id     foreign key (stu_id ) references stu_info(stu_id ),

 constraint  fk_course_id  foreign key (course_id ) references course_info(course_id ),)

--创建表及主键外键

--修改表

alter table stu——info

add code char(18)

 

alter table stu——info

add constraint RM unique (code)--RM 是约束的名字

 

alter table stu——info

drop constraint RM

 

alter table stu——info

add constraint RM check (grade between 0 and 100)

 

alter table stu——info

drop constraint RM foreign key (course_id ) references course_info(course_id )

--删除表先删除约束

alter table stu_grade

drop constraint fk_stu_id

 

drop table stu_info

 

insert into stu_info

values('20081300408','张山','1988-01-26','f','河南郑州',.......

       '..',' ..', .....

       )

--插入数据

 

update stu_info

set sdept='会计学院'

where stu_id ='20081300408'

--更新表

 

 

delete from stu_info

where sex is null--必须是 is null 而不能是 =null

--删除数据

 

 

数据查询

select ....from  表名

where......

--简单查询

 

select s.stu_id ,name,c.course_name, g.grade

from stu_info s join stu_grade g on s.stu_id=g.stu_id

     join course_info c on c.course_id=g.course_id

--多表查询语句

 

select name from stu_info

where adddress like '%%'

--模糊查询

 

select stu_id, name,sdept from stu_info

where stu_id not in /in (select stu_id from stu_grade where course_id =001)

--查询没有选修001 这门课的学生

 

select avg(mark) from stu_info where sdept='信息学院'--平均成绩

 

select max(mark),min(mark) from stu_info--最大成绩,最小成绩

 

select sdept,sex ,count(*) from stu_info  where sdept='信息学院' and sex=''

group by sdept ,sex--信息学院女生人数

创建索引

create nonclustered index 索引名SY on course_infor (course_name)--非聚焦索引

 

create nonclustered index SY on stu_grade (stu_id,course_id )--创建复合索引

 

drop index stu_grade.SY--删除索引 (表名.索引名)!

 

create unique index SY on course_info(course_name)--修改 sy 为唯一索引

 

sp_helpindex course_info --查看 SY索引 信息

 

创建视图

create view view_name as select ....from.....where..group by..

--创建视图

 

create view view_name

as

select sdept ,count(*) '总人数' from stu_info

where sdept='信息学院'

group by sdept

--创建视图显示信息学院的总人数

 

alter view view_name with encrypion

as

select sdept ,count(*) '总人数' from stu_info

where sdept='信息学院'

group by sdept

--修改为加密视图

 

drop view view_name--删除视图

 

创建存储过程

create procedure pro_name @xh char(10)

as

select count(*) from stu_gread

where grade<60 and stu_id=@xh

--创建存储过程pro_name

 

drop procedure pro_name--删除存储过程

 

pro_name '200707001'--调用存储过程

触发器

create trigger tr1

on stu_info

after delete

as

delete from stu_grade where stu_id=(select stu_id from deleted)

--级联删除

 

create trigger tr2

on stu_info

after update

as

update stu_grade set stu_id=(select stu_id from insered )

where  stu_id=(select stu_id from deleted)

--级联更新

 

select * into xs2 from stu_info where 1=2--创建表xs2作为储存开除学生的表

 

create trigger tr3

on stu_grade

for update ,insert

as

declare @m int

select @m=count(*) from stu_grade

where grade<60 and stu_id =(select stu_id from inserted )

if (@m>4)

insert into xs2 select *  from stu_info

where stu_id =(select stu_id from inserted)

--forafter为后触发器)

 

drop trigger tr3--删除触发器

 

数据库安全

sp_addlogin 'login_name','密码','默认数据库'--创建登录账户

 

sp_droplogin 'login_name'--删除登录账户

 

sp_addrole 'role_name'--创建数据库角色

 

sp_droprole 'role_name'--删除角色

 

backup database stu_info to sale_student--备份数据库

 

restore database stu_info from sale_student--还原数据库

这篇关于数据库语句大全!!要举一反三!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/721055

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

查询SQL Server数据库服务器IP地址的多种有效方法

《查询SQLServer数据库服务器IP地址的多种有效方法》作为数据库管理员或开发人员,了解如何查询SQLServer数据库服务器的IP地址是一项重要技能,本文将介绍几种简单而有效的方法,帮助你轻松... 目录使用T-SQL查询方法1:使用系统函数方法2:使用系统视图使用SQL Server Configu

SQL Server数据库迁移到MySQL的完整指南

《SQLServer数据库迁移到MySQL的完整指南》在企业应用开发中,数据库迁移是一个常见的需求,随着业务的发展,企业可能会从SQLServer转向MySQL,原因可能是成本、性能、跨平台兼容性等... 目录一、迁移前的准备工作1.1 确定迁移范围1.2 评估兼容性1.3 备份数据二、迁移工具的选择2.1

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

Oracle数据库如何切换登录用户(system和sys)

《Oracle数据库如何切换登录用户(system和sys)》文章介绍了如何使用SQL*Plus工具登录Oracle数据库的system用户,包括打开登录入口、输入用户名和口令、以及切换到sys用户的... 目录打开登录入口登录system用户总结打开登录入口win+R打开运行对话框,输php入:sqlp

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php