Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. • Креирање на нова база
  2. create database DbTest
  3.  
  4.  
  5. • Команда за префрлање на соодветна база
  6. use DbTest
  7.  
  8.  
  9. • Креирање на табела
  10. create table Users
  11. (
  12. Id bigint primary key identity(1,1)
  13. ,Name nvarchar(200)
  14. ,UserName Varchar(30)
  15. ,[Password] Varchar(30)
  16. ,CashAccountId Bigint
  17. ,ClientId Bigint
  18. )
  19.  
  20. create table Clients
  21. (
  22. Id bigint primary key identity(1,1)
  23. ,Name Nvarchar(200)
  24. ,[Type] Bit
  25. ,IdentificationNumber Varchar(36)
  26. ,[Address] Varchar(100)
  27. ,Tel Varchar(30)
  28. ,Mob Varchar(30)
  29. ,Email Varchar(30)
  30. ,Web Varchar(100)
  31. )
  32.  
  33. • Измена на ClientId колоната од Users табелата да е надворешен клуч кон Clients табелата
  34. alter table Users add constraint clientId_foreignKey foreign key (ClientId) references Clients(Id)
  35.  
  36.  
  37. • Додавање на записи во Users табелата преку select команди
  38. insert into Users(Name,UserName,[Password],ClientId)
  39. select 'user3','username3','3df2432',null
  40.  
  41. • Додавање на записи во Users табелата преку values синтакса
  42. insert into Users(Name,UserName,[Password],ClientId)
  43. values('user1','username1','32432',null)
  44.  
  45.  
  46.  
  47. • Команди за работа со множество на податоци (унија, пресек, разлика)
  48. select 'user3','username3','3df2432',null
  49. union
  50. select 'user4','username4','3df2432',null
  51.  
  52. select 'user3','username3','3df2432',null
  53. intersect
  54. select 'user4','username4','3df2432',null
  55.  
  56. select 'user3','username3','3df2432',null
  57. except
  58. select 'user4','username4','3df2432',null
  59.  
  60.  
  61.  
  62.  
  63.  
  64. • Приказ на сите податоци од Users табелата
  65. select * from Users
  66.  
  67.  
  68. create table Currencies
  69. (
  70. Currency Varchar(3)
  71. ,Name Varchar(30)
  72. ,Symbol Nvarchar(5)
  73. ,[Image] Varbinary(8000)
  74. )
  75.  
  76.  
  77. • Прогласување на табела на колона за примарен клуч.
  78. - прво се додава constraint да не може колоната да прима NULL вредност
  79. alter table Currencies alter column Currency Varchar(3) NOT NULL
  80. alter table Currencies add Primary key (Currency)
  81.  
  82.  
  83. • Бришење на колона од табела
  84. alter table Currencies drop column Symbol
  85.  
  86.  
  87. • Додавање на колона во табела
  88. alter table Currencies add Symbol Nvarchar(5)
  89.  
  90. alter table Users add constraint clientId_foreignKey foreign key (ClientId) references Clients(Id)
  91.  
  92.  
  93. • Приказ на дефиниција на Currencies табела (колони, податочен тип на колона, identity колони, constraints, индекси...)
  94. sp_help 'dbo.Currencies'
  95.  
  96.  
  97. select * from Currencies
  98.  
  99. • Unique, Check constarint и calculated field
  100. create table AccountClasses
  101. (
  102. Id Bigint primary key identity(1,1)
  103. ,Name Varchar(30)
  104. ,[Description] Nvarchar(100)
  105. ,[Type] Int unique check([Type] in (1,2,3,4))
  106. ,IsCashAccount as case when [Type] = 1 then 1 else 0 end
  107. ,IsCurrentAccount as case when [Type] = 2 then 1 else 0 end
  108. ,IsDepositAccount as case when [Type] = 3 then 1 else 0 end
  109. ,IsCreditAccount as case when [Type] = 4 then 1 else 0 end
  110. )
  111.  
  112.  
  113. sp_help 'dbo.AccountClasses'
  114.  
  115. insert into AccountClasses(Name,[Description],[Type])
  116. values ('AccClass1','AccClass1Desc',2), ('AccClass2','AccClass2Desc',3)
  117.  
  118. select * from AccountClasses
  119.  
  120.  
  121. • Бришење на конкретен запис од табела
  122. delete from AccountClasses
  123. where Id = 2
  124.  
  125.  
  126. • Бришење на податоците од цела табела
  127. truncate table AccountClasses
  128.  
  129.  
  130. • Бришење на табела
  131. drop table AccountClasses
  132.  
  133.  
  134. create table AccountTypes
  135. (
  136. Id Bigint primary key identity(1,1)
  137. ,ClassId bigint foreign key references AccountClasses(Id)
  138. ,Name Varchar(30)
  139. ,[Description] Nvarchar(100)
  140. )
  141.  
  142.  
  143. create table Accounts
  144. (
  145. Id Bigint primary key identity(1,1)
  146. ,TypeId bigint foreign key references AccountTypes(Id)
  147. ,AccountNumber Varchar(30) unique
  148. ,Currency varchar(3) foreign key references Currencies(Currency)
  149. ,ClientId bigint foreign key references Clients(Id)
  150. ,[Status] Varchar(1) check ([Status] in ('A','O','C'))
  151. ,Blocked bit
  152. ,Balance Decimal(19,2)
  153. ,OverdraftLimit Decimal(19,2)
  154. ,AmountOnHold Decimal(19,2)
  155. ,AvailableBalance as Balance+OverdraftLimit-AmountOnHold
  156. )
  157.  
  158.  
  159. alter table Users add constraint cashAccountId_foreignKey foreign key (CashAccountId) references Accounts(Id)
  160.  
  161. • Користење на subquery
  162. select * from AccountTypes
  163. where ClassId in (select Id from AccountClasses where Name='')
  164.  
  165.  
  166.  
  167. • Примена на IN оператор (филтер по множество на податоци)
  168. select * from AccountTypes
  169. where ClassId in (1,5,4)
  170.  
  171.  
  172. • Примена на order by (подредување на податоци)
  173. select * from AccountClasses
  174. order by [Type] asc
  175.  
  176.  
  177. • Промена на запис во табела
  178. update AccountClasses
  179. set [Type] = 4
  180. where Id = 4
  181.  
  182. update Accounts
  183. set OverdraftLimit = OverdraftLimit+5
  184.  
  185.  
  186. • JOIN на табели (join, left join, right join, full join)
  187. select * from AccountClasses ac
  188. join AccountTypes at on ac.Id = at.ClassId
  189.  
  190.  
  191. • Копирање од една во друга табела
  192. select * into AccountClasses2 from AccountClasses
  193. where 1=0
  194.  
  195.  
  196. • Враќа првиот податок кој не е NULL (може да прима повеќе параметри)
  197. SELECT coalesce(Name,Username)
  198. from Users
  199.  
  200.  
  201. • Ако првиот параметар не е NULL, го враќа параметарот, во спротивно го враќа вториот параметар
  202. SELECT isnull(Name,'default')
  203. from Users
  204.  
  205.  
  206. • Враќа NULL ако првиот параметар е ист со вториот
  207. select nullif(Symbol,'s'),* from Currencies
  208.  
  209.  
  210. • Спојување на текстуални податоци
  211. select 'asdsad'+'fdas'
  212.  
  213.  
  214. • Групирање и филтрирање на податоци
  215. select Name, min([TYPE]), [Description] from AccountClasses
  216. group by Name,[Description]
  217. having min([TYPE]) >1
  218.  
  219.  
  220.  
  221.  
  222. • Користење на LIKE операторот
  223. select * from AccountClasses
  224. where Name like '%2%'
  225.  
  226. % _ []
  227.  
  228.  
  229. • Работа со datetime податочни типови
  230. declare @p1 datetime
  231.  
  232. set @p1 = GETDATE() --datetime
  233. select @p1
  234. select CAST(GETDATE() as date)
  235.  
  236. declare @p1 datetime
  237. set @p1 = GETDATE()
  238. select DATEPART(yyyy,@p1)
  239.  
  240.  
  241. insert into dbo.AccountTypes(ClassId, Name, [Description])
  242. values (1, 'type1', 'desc1'),(4, 'type2', 'desc2'),(1, 'type3' ,'desc3')
  243.  
  244. insert into dbo.Accounts(TypeId, AccountNumber, Currency, ClientId, [Status], Blocked,Balance, OverdraftLimit, AmountOnHold)
  245. values (null, '123abcww2r1', 'EUR', null, 'A',0,null, 150, 200)
  246.  
  247. update Currencies
  248. set Symbol = 'd'
  249. where Currency = 'CHF'
  250.  
  251. insert into Currencies
  252. values ('USD','Dolari',null,null)
  253.  
  254. select coalesce(Currency,'EUR'),* from Accounts
  255. select * from Currencies
  256.  
  257. select * from Accounts a
  258. join Currencies c on a.Currency = c.Currency
  259.  
  260. update Accounts
  261. set Currency = 'EUR'
  262. where Id = 3
  263.  
  264. create table TransactionTypes
  265. (
  266. Id Bigint primary key identity(1,1)
  267. ,Name Varchar(30)
  268. ,[Description] Nvarchar(100)
  269. ,AccountClassDebit bigint foreign key references AccountClasses(Id)
  270. ,AccountClassCredit bigint foreign key references AccountClasses(Id)
  271. ,ReflectsAmountOnHold bit default 0
  272. ,HasFee bit
  273. )
  274.  
  275.  
  276.  
  277.  
  278. create table Transactions
  279. (
  280. Id Bigint primary key identity(1,1)
  281. ,TransactionTypeId bigint foreign key references TransactionTypes(Id)
  282. ,UserId bigint foreign key references Users(Id)
  283. ,EntryDate Date default getdate()
  284. ,[Date] Date
  285. ,Currency varchar(3) foreign key references Currencies(Currency)
  286. ,AccountIdDebit Bigint foreign key references Accounts(Id)
  287. ,AccountIdCredit Bigint foreign key references Accounts(Id)
  288. ,Fee Decimal(19,2)
  289. ,Amount Decimal(19,2)
  290. ,[Description] Nvarchar(100)
  291. )
  292.  
  293. go
  294.  
  295. create table PostingEntries
  296. (
  297. Id Bigint primary key identity(1,1)
  298. ,TransactionId bigint foreign key references Transactions(Id)
  299. ,AccountId bigint foreign key references Accounts(Id)
  300. ,DebitCredit SmallInt check (DebitCredit in (-1,0,1))
  301. ,ContraAccountId bigint foreign key references Accounts(Id)
  302. ,Currency varchar(3) foreign key references Currencies(Currency)
  303. ,Amount Decimal(19,2)
  304. ,StatementNo int
  305. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement