
我们一般都是运用图形化工具创建数据库,那么如何运用SQL语言创建数据库呢?下面我给大家分享一下。
工具/材料SQL Server Managment
01首先打开SQL Server Managment,登录本地数据库管理平台,如下图所示
02然后点击顶部的新建查询按钮,如下图所示
03接下来在查询编辑界面输入create database语句,后面加上数据库名称,如下图所示
04最后我们就可以在数据库列表中看到创建的数据库了,如下图所示
SQL 数据库 实现递归查询的几种代码方法 表结构
ProductCategory
CategoryID Level ParentCategoryID
数据
T SQL
WITH CategoryTemp(CategoryID ParentCategoryID) 临时表用来保存查到的Category
(
SELECT CategoryID ParentCategoryID FROM ProductCategory WHERE ParentCategoryID<= 将所有的第一层查出来作为初始数据 需要查第几层或者哪个ParentCategoryID下面所有的 N层 把ParentCategoryID赋相关的值即可
UNION ALL 查询N层
SELECT pc CategoryID ParentCategoryID FROM ProductCategory pc
LEFT JOIN CategoryTemp ct ON pc ParentCategoryID=ct CategoryID
WHERE ParentCategoryID> 因为第一层前面已经查出来了 所以这里把第一层筛选掉
)
SELECT CategoryID ParentCategoryID FROM CategoryTemp
结果
如果把ParentCategoryID赋为 结果则为
实例
ID 是否为部门 部门名 上级ID y 部门 y 部门 n 张三 n 李二 y 部门 n 王五 y 部门3 n 小三 我想找询 ID 值为 下级的所有人员包括下级部门的所有人员
创建查询函数 create function f_id( @id int 要查询的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上级id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上级id=b id and b level=@l end return end go
调用函数进行查询 select a from 表 a join f_id( ) b on a id=b id
联合查询
测试数据 create table 表(ID int 是否为部门 char( ) 部门名 varchar( ) 上级ID int) insert 表 select y 部门 union all select y 部门 union all select n 张三 union all select n 李二 union all select y 部门 union all select n 王五 union all select y 部门 union all select n 小三 go
创建查询函数 create function f_id( @id int 要查询的id )returns @re table(id int level int) as begin declare @l int set @l= insert @re select id @l from 表 where 上级id=@id while @@rowcount> begin set @l=@l+ insert @re select a id @l from 表 a join @re b on a 上级id=b id and b level=@l end return end go
调用函数进行查询 select a from 表 a join f_id( ) b on a id=b id go
删除测试 drop table 表 drop function f_id
/ 测试结果
ID 是否为部门 部门名 上级ID n 小三
lishixinzhi/Article/program/MySQL/201311/29557
以上就是关于如何使用SQL代码创建数据库全部的内容,包括:如何使用SQL代码创建数据库、SQL数据库实现递归查询的几种代码方法、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)