1. 程式人生 > >sqlserver庫相關-表相關-3

sqlserver庫相關-表相關-3

tps 數據庫文件 where .cn cts ddr 是否 html ins

原文:

https://www.cnblogs.com/wlx520/p/4684441.html

庫相關

建庫

--創建School數據庫之前:首先判斷數據庫是否存在,若存在則刪除後再創建,若不存在則創建--
--exists關鍵字:括號裏邊能查詢到數據則返回‘true’ 否則返回‘false’
if exists(select * from sysdatabases where name = ‘School‘)
--exists返回‘true’則執行刪除數據庫操作--
drop database School

--exists返回‘false’則表明數據庫不存在,直接創建

create database School
on primary
(
--主數據庫文件--
name = ‘School‘, --主數據文件邏輯名
fileName = ‘D:\project\School.mdf‘, --主數據文件物理邏輯名
size = 5MB, --初始值大小
maxsize = 100MB, --最大大小
filegrowth = 15% --數據文件增長量
)
log on
(
--日誌文件--
name = ‘School_log‘,
filename = ‘D:\project\School_log.ldf‘,
size = 2MB,
filegrowth = 1MB
)
go

查看有哪些庫

select * from sysdatabases

刪除庫

drop database School

表相關

創建表

--1、選擇操作的數據庫--
use School
go

--判斷表是否存在--
if exists(select * from sysobjects where name = ‘Student‘)
drop table Student

--2、創建表---
create table Student
(
--具體的列名 數據類型 列的特征(是否為空)--
StudentNo int identity(2,1) not null,
LoginPwd nvarchar(20) not null,
StudentName nvarchar(20) not null,

Sex int not null,
GradeId int not null,
phone nvarchar(50) not null,
BornDate datetime not null,
Address nvarchar(255),
Email nvarchar(50),
IDENTITYcard varchar(18)
)
go

查看有哪些表

---查看所有數據庫對象(數據庫表)---
select * from sysobjects

寫入表數據

insert into student(name) values(‘s1‘);

刪除表

drop table Student

sqlserver庫相關-表相關-3