SQL Server 資料庫基礎程式設計
Ø 建立、刪除資料庫
方法1、
--判斷是否存在該資料庫,存在就刪除
if (exists (select * from sys.databases where name = 'testHome'))
drop database testHome
go
--建立資料庫,設定資料庫檔案、日誌檔案儲存目錄
create database testHome
on(
name = 'testHome',
filename = 'c:\data\students.mdf'
)
log on(
name = 'testHome_log',
filename = 'c:\data\testHome_log.ldf'
)
go
方法2(設定檔案大小)、
if (exists (select * from sys.databases where name = 'testHome'))
drop database testHome
go
create database testHome
--預設就屬於primary主檔案組,可省略
on primary (
--資料檔案的具體描述
name = 'testHome_data', --主資料檔案的邏輯名
fileName = 'c:\testHome_data.mdf', --主資料檔案的物理名
size = 3MB, --主資料檔案的初始大小
maxSize = 50MB, --主資料檔案增長的最大值
fileGrowth = 10% --主資料檔案的增長率
)
--日誌檔案的具體描述,各引數含義同上
log on (
name = 'testHome_log',
fileName = 'c:\testHome_log.ldf',
size = 1MB,
fileGrowth = 1MB
)
go
方法3(設定次資料檔案)、
if (exists (select * from sys.databases where name = 'testHome'))
drop database testHome
go
create database testHome
--預設就屬於primary主檔案組,可省略
on primary (
--資料檔案的具體描述
name = 'testHome_data', --主資料檔案的邏輯名
fileName = 'c:\testHome_data.mdf', --主資料檔案的物理名
size = 3MB, --主資料檔案的初始大小
maxSize = 50MB, --主資料檔案增長的最大值
fileGrowth = 10% --主資料檔案的增長率
),
--次資料檔案的具體描述
(
--資料檔案的具體描述
name = 'testHome2_data', --主資料檔案的邏輯名
fileName = 'c:\testHome2_data.mdf', --主資料檔案的物理名
size = 2MB, --主資料檔案的初始大小
maxSize = 50MB, --主資料檔案增長的最大值
fileGrowth = 10% --主資料檔案的增長率
)
--日誌檔案的具體描述,各引數含義同上
log on (
name = 'testHome_log',
fileName = 'c:\testHome_log.ldf',
size = 1MB,
fileGrowth = 1MB
),
(
name = 'testHome2_log',
fileName = 'c:\testHome2_log.ldf',
size = 1MB,
fileGrowth = 1MB
)
go
Ø 基本資料型別
精確數字型別
型別 |
描述 |
bigint |
bigint 資料型別用於整數值可能超過 int 資料型別支援範圍的情況,範圍:-2^63 到 2^63-1,儲存空間8位元組 |
int |
整數資料型別,範圍在-2^31 到 2^31-1,儲存空間4位元組 |
smallint |
整數,範圍在-2^15 到 2^15-1,儲存空間2位元組 |
tinyint |
範圍在0 到 255,儲存空間1位元組 |
bit |
可以取值為 1、0 或 NULL 的整數資料型別,每8個bit佔一個位元組,16bit就2個位元組,24bit就3個位元組 |
decimal |
帶固定精度和小數位數的數值資料型別,有效值從 - 10^38 +1 到 10^38 - 1 |
numeric |
同上 |
money |
貨幣或貨幣值的資料型別,範圍在-922,337,203,685,477.5808 到 922,337,203,685,477.5807 |
smallmoney |
貨幣型別,-214,748.3648 到 214,748.3647 |
近似數字型別
型別 |
描述 |
float |
表示浮點數值資料的大致數值資料型別。浮點資料為近似值;範圍-1.79E + 308 至 -2.23E - 308、0 以及 2.23E - 308 至 1.79E + 308 |
real |
real 的 SQL-92 同義詞為 float(24),範圍在-3.40E + 38 至 -1.18E - 38、0 以及 1.18E - 38 至 3.40E + 38 |
日期時間型別
型別 |
描述 |
datetime |
表示某天的日期和時間的資料型別,範圍在1753 年 1 月 1 日到 9999 年 12 月 31 日 |
smalldatetime |
範圍在1900 年 1 月 1 日到 2079 年 6 月 6 日 |
字串型別
型別 |
描述 |
char |
固定長度或可變長度的字元資料型別,範圍在範圍為 1 至 8,000位元組 |
text |
最大長度為 2^31-1 |
varchar |
固定長度或可變長度的字元資料型別,最大儲存大小是 2^31-1 個位元組 |
Unicode字串型別
型別 |
描述 |
nchar |
字元資料型別,長度固定,在必須在 1 到 4,000 之間 |
nvarchar |
可變長度 Unicode 字元資料。最大儲存大小為 2^31-1 位元組 |
ntext |
長度可變的 Unicode 資料,最大長度為 2^30 - 1 (1,073,741,823) 個字元 |
二進位制字串型別
型別 |
描述 |
binary |
長度為 n 位元組的固定長度二進位制資料,範圍從 1 到 8,000 的值。儲存大小為 n 位元組。 |
varbinary |
可變長度二進位制資料。n 可以取從 1 到 8,000 的值。最大的儲存大小為 2^31-1 位元組 |
image |
長度可變的二進位制資料,從 0 到 2^31-1 (2,147,483,647) 個位元組 |
Ø 判斷表或其他物件及列是否存在
--判斷某個表或物件是否存在
if (exists (select * from sys.objects where name = 'classes'))
print '存在';
go
if (exists (select * from sys.objects where object_id = object_id('student')))
print '存在';
go
if (object_id('student', 'U') is not null)
print '存在';
go
--判斷該列名是否存在,如果存在就刪除
if (exists (select * from sys.columns where object_id = object_id('student') and name = 'idCard'))
alter table student drop column idCard
go
if (exists (select * from information_schema.columns where table_name = 'student' and column_name = 'tel'))
alter table student drop column tel
go
Ø 建立、刪除表
--判斷是否存在當前table
if (exists (select * from sys.objects where name = 'classes'))
drop table classes
go
create table classes(
id int primary key identity(1, 2),
name varchar(22) not null,
createDate datetime default getDate()
)
go
if (exists (select * from sys.objects where object_id = object_id('student')))
drop table student
go
--建立table
create table student(
id int identity(1, 1) not null,
name varchar(20),
age int,
sex bit,
cid int
)
go
Ø 給表新增欄位、修改欄位、刪除欄位
--新增欄位
alter table student add address varchar(50) not null;
--修改欄位
alter table student alter column address varchar(20);
--刪除欄位
alter table student drop column number;
--新增多個欄位
alter table student
add address varchar(22),
tel varchar(11),
idCard varchar(3);
--判斷該列名是否存在,如果存在就刪除
if (exists (select * from sys.columns where object_id = object_id('student') and name = 'idCard'))
alter table student drop column idCard
go
if (exists (select * from information_schema.columns where table_name = 'student' and column_name = 'tel'))
alter table student drop column tel
go
Ø 新增、刪除約束
--新增新列、約束
alter table student
add number varchar(20) null constraint no_uk unique;
--增加主鍵
alter table student
add constraint pk_id primary key(id);
--新增外來鍵約束
alter table student
add constraint fk_cid foreign key (cid) references classes(id)
go
--新增唯一約束
alter table student
add constraint name_uk unique(name);
--新增check約束
alter table student with nocheck
add constraint check_age check (age > 1);
alter table student
add constraint ck_age check (age >= 15 and age <= 50)
--新增預設約束
alter table student
add constraint sex_def default 1 for sex;
--新增一個包含預設值可以為空的列
alter table student
add createDate smalldatetime null
constraint createDate_def default getDate() with values;
----- 多個列、約束一起建立--------
alter table student add
/*新增id主鍵、自增*/
id int identity constraint id primary key,
/* 新增外來鍵約束*/
number int null
constraint uNumber references classes(number),
/*預設約束*/
createDate decimal(3, 3)
constraint createDate default 2010-6-1
go
--刪除約束
alter table student drop constraint no_uk;
Ø 插入資料
insert into classes(name) values('1班');
insert into classes values('2班', '2011-06-15');
insert into classes(name) values('3班');
insert into classes values('4班', default);
insert into student values('zhangsan', 22, 1, 1);
insert into student values('lisi', 25, 0, 1);
insert into student values('wangwu', 24, 1, 3);
insert into student values('zhaoliu', 23, 0, 3);
insert into student values('mazi', 21, 1, 5);
insert into student values('wangmazi', 28, 0, 5);
insert into student values('jason', null, 0, 5);
insert into student values(null, null, 0, 5);
insert into student
select 'bulise' name, age, sex, cid
from student
相關推薦
SQL Server 資料庫基礎程式設計
Ø 建立、刪除資料庫 方法1、 --判斷是否存在該資料庫,存在就刪除 if (exists (select * from sys.databases where name = 'testHome')) drop database testHome go --建
SQL Server資料庫基礎
建庫建表: if DB_id('TicketManager ')is not null --判斷資料庫是否已經存在 drop database TicketManager --移除已經存在的資料庫 go create database TicketManager --新建資料庫 on (
SQL Server資料庫基礎知識歸納總結
1、資料庫的“特點”: 資料共享、資料完整性、資料獨立性高、 2、SQL的四個“系統資料庫”: master、model、msdb、tempdb簡稱為“三M一T”; 【master】: master資料庫是SQL Server中最重要的資料庫,記錄了SQL Se
SQL Server資料庫基礎(一),資料庫表、時間、建立約束
管理器: 系統資料庫有四個: master:系統資訊,要經常備份 model:模板資料庫 tempdb:臨時資料庫 msdb:代理服務資料庫 資料庫的轉移: 直接找到資料庫的位置是不能剪下複製的,要先通過分離,斷開資料庫與SQL Server的連線
SQL server 2000資料庫基礎程式設計(一)
以下所有內容適用於SQL server 2000,其他版本或型別資料庫可能會有部分不同,語句都差不多,請隨機應變,例: sysdatabases <–> sys.databases
【學習記錄】第一章 資料庫設計-《SQL Server資料庫設計和開發基礎篇視訊課程》
一、課程筆記 1.1 軟體開發週期 (1)需求分析階段 分析客戶的業務和資料處理需求。 (2)概要設計階段 設計資料庫的E-R模型圖,確認需求資訊的正確和完整。 /* E-R圖:實體-關係圖(Entity Relationship Diagram),提供了
SQL Server資料庫開發(2.T-Sql程式設計)
一,批處理(GO) --可以使不在同一批處理中的sql語句相互之間不受影響 --把相互聯絡的放在同一批次,沒聯絡的放在不同批次
Revit中Dynamo程式設計——在Python Script中結合sql server資料庫來儲存材料的外觀屬性
我們的Dynamo的Python Script環境其實也是 IronPython的一種,結合以上部落格可以很好的結合資料庫。 我的這篇讀取材料的外觀屬性,並存儲到sql server資料庫中。今天我來修改一下,把其中的的讀取過程做成dll檔案,然後在Pyt
c#操作sql server資料庫(ADO.net基礎)
ado.net提供了豐富的資料庫操作,這些操作可以分為三個步驟: 第一,使用SqlConnection物件連線資料庫; 第二,建立SqlCommand物件,負責SQL語句的執行和儲存過程的呼叫;
MFC ADO方法實現SQL Server資料庫程式設計(轉載)
本文是轉載,感謝原創作者(xielechuan)的分享。之前在做資料庫方面的程式設計學習的過程中,發現很多的知識細節不是很清楚,通過查了很多的資料,也走了不少的彎路。覺得有必要整理一下這方面的知識,一方面,可以為自己的學習做記錄;另一方面,可以給初步學習MFC資料庫程式設計的同學們一點參考。由於很多知識匱乏可
Sql server資料庫連線Oracle庫的步驟
本地使用環境 作業系統: win10 64 ,SQL Server 2012 ,Oracle Server 11g 第一步:安裝好oracle客戶端,並配置好TNS資訊 ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = T
SQL Server資料庫mdf檔案中了勒索病毒.sicck。副檔名變為sicck
SQL Server資料庫mdf檔案中了勒索病毒sicck。副檔名變為sicck SQL,資料庫,勒索病毒,mdf檔案中毒,sicck 副檔名如下: [[email protected]]***.mdf.sicck [[email protected]]***.ldf.sicck
Sql Server資料庫資料恢復成功案例
故障描述: 5塊2T硬碟組建RAID5,劃分LUN供windows伺服器使用。在windows伺服器內裝有Sql Server2008資料庫。儲存空間內共有三個邏輯分割槽,大小分別為500G、800G、2.3T。資料庫檔案丟失,主要涉及五個資料庫,表個數約為6000個左右。丟失原因未知,且不能確
SQL Server資料庫mdf檔案中了勒索病毒Goat4444。副檔名變為Goat4444
SQL,資料庫,勒索病毒,mdf檔案中毒,Rooster4444 *SQL Server資料庫mdf檔案中了勒索病毒.mdf.Rooster4444。副檔名變為Rooster4444 SQL Server資料庫mdf檔案中了勒索病毒Rooster4444。副檔名變為Rooster4444 常見的副檔名如
SQL Server資料庫mdf檔案中了勒索病毒no_more_ransom。副檔名變為no_more_ransom
SQL,資料庫,勒索病毒,mdf檔案中毒,no_more_ransom SQL Server資料庫mdf檔案中了勒索病毒.mdf.no_more_ransom。副檔名變為no_more_ransom SQL Server資料庫mdf檔案中了勒索病毒no_more_ransom。副
SQL Server資料庫mdf檔案中了勒索病毒[email p
SQL,資料庫,勒索病毒,mdf檔案中毒,[email protected]_email *SQL Server資料庫mdf檔案中了勒索病毒[email protected]_email。副檔名變為[email protected]_email SQL Serv
SQL Server資料庫mdf檔案中了勒索病毒Rat4444 。副檔名變為Rat4444
SQL,資料庫,勒索病毒,mdf檔案中毒,Rat4444 *SQL Server資料庫mdf檔案中了勒索病毒.mdf.Rat4444。副檔名變為Rat4444 SQL Server資料庫mdf檔案中了勒索病毒Rat4444。副檔名變為Rat4444 常見的副檔名如下:.ALCO .BIP .COMBO
SQL Server資料庫log shipping 災備
https://www.cnblogs.com/Aldj/p/8605163.html SQL Server資料庫log shipping 災備(Part1 ) 1.概述Log Shipping為SQL Server提供的資料庫備份過程。它可以將資料庫整個複製到另一臺伺服器上。在這種情況下,交易日誌也會定
SQL Server資料庫級別觸發器
禁止修改表結構和加表 CREATE TRIGGER [Object_Change_Trigger_DDL] ON DATABASE FOR ALTER_TABLE,DROP_TABLE,CREATE_TABLE,CREATE_INDEX,ALTER_INDEX
Matlab連線Sql server資料庫
作業系統:64位win7.0 軟體版本:Matlab R2016a SQL Sever 2012 一、建立資料庫和相應的表 建立好資料庫和其中的表,步驟不做詳細介紹。 二、配置ODBC ODBC(Open Database Conne