1.創建數據庫
use master go if exists(select * from sysdatabases where name='Test') begin select '該數據庫已存在' drop database Test --如果該數據庫已經存在,那么就刪除它 end else begin create database Test on primary --表示屬於 primary 文件組 ( name='stuDB_data', -- 主數據文件的邏輯名稱 filename='D:\stuDB_data.mdf', -- 主數據文件的物理名稱 size=5mb, --主數據文件的初始大小 maxsize=100mb, -- 主數據文件增長的最大值 filegrowth=15% --主數據文件的增長率 ) log on ( name='stuDB_log', -- 日志文件的邏輯名稱 filename='D:\stuDB_log.ldf', -- 日志文件的物理名稱 size=2mb, --日志文件的初始大小 maxsize=20mb, --日志文件增長的最大值 filegrowth=1mb --日志文件的增長率 ) end
接下來是創建數據表的 SQL 語句:
use Test --表示設置為在該數據庫(Test)執行下面的SQL語句 go
可以先執行一下以上語句。
或者在這里選擇數據庫。
use Test --表示設置為在該數據庫(Test)執行下面的SQL語句 go if exists(select * from sysobjects where name='Student') begin select '該表已經存在' drop table Student --刪除表 end else begin create table Student ( S_Id int not null identity(1,1) primary key, --設置為主鍵和自增長列,起始值為1,每次自增1 S_StuNo varchar(50) not null, S_Name varchar(20) not null, S_Sex varchar(10) not null, S_Height varchar(10) null, S_BirthDate varchar(30) null ) end --添加約束 alter table Student add constraint UQ_S_StuNo --約束名 unique --約束類型(唯一約束) (S_StuNo) --列名 --刪除約束 alter table Student drop constraint UQ_S_StuNo --約束名
SQL語句創建表變量:
declare @Score table ( Id int not null, Name varchar(50) null ) insert into @Score select '1','劉邦' union select '2','項羽' select * from @Score
SQL語句創建臨時表:
-- ## 表示全局臨時表 create table ##temp ( Id int not null, Name varchar(10) null ) -- # 表示局部臨時表 create table #temp ( Id int not null, Name varchar(10) null )
SQL 語句創建表並設置主外鍵關系:
if exists(select * from sysObjects where name='Course') begin select '該表已經存在' drop table Course end else begin create table Course ( --列名 字段類型 是否為空 標識外鍵列(外鍵列名) 關聯表的表名(關聯的字段名) Stu_Id int null foreign key(Stu_Id) references Student(S_Id), C_Id int not null identity(1,1) Primary key, C_Name varchar(100) not null ) end
2.完整SQL Server代碼
--創建數據表 use stu_db --表示設置為在該數據庫(Test)執行下面的SQL語句 go if exists(select * from sysobjects where name='Students') begin select '該表已經存在' drop table Students --刪除表 end else begin create table Students ( stuID int not null identity(1,1) primary key, --設置為主鍵和自增長列,起始值為1,每次自增1 stuNumber nvarchar(10) not null, stuClass nvarchar(50) not null, stuName nvarchar(20) null, stuSex nvarchar(20) not null, stuAge nvarchar(20) not null, ) end select *from Students INSERT INTO Students VALUES ('001','軟件01', '小明','男','18') INSERT INTO Students VALUES ('002','軟件01', '小李','男','18') INSERT INTO Students VALUES ('003','軟件06', '小麗','女','25') INSERT INTO Students VALUES ('004','軟件01', '小米','男','30') select *from Students where stuNumber='008' --添加 INSERT INTO [stu_db].[dbo].[Students]([stuNumber],[stuClass],[stuName],[stuSex],[stuAge])VALUES('008','計算機111','小牧','男','20') --更改 UPDATE [stu_db].[dbo].[Students] SET [stuNumber] = '',[stuClass] = '',[stuName] = '',[stuSex] = '',[stuAge] = '' WHERE stuNumber='' AND stuClass='' --刪除 DELETE FROM [stu_db].[dbo].[Students]WHERE stuNumber='' AND stuClass='' --查詢 SELECT *FROM [stu_db].[dbo].[Students]WHERE stuNumber='' AND stuClass=''