在SqlServer中如果存在数据库,则删除再建,如果存在表则删除再建.

  
--注意:使用该sql语句生成数据库和表时,必须先在E盘下创建名为teaching的文件夹;  
  
USE master  
GO  

if exists(select * from dbo.sysdatabases where name=N'teaching')  
    drop database teaching
GO  
CREATE DATABASE teaching
ON   
(   
   NAME = N'teaching_data',  
   FILENAME = N'E:\teaching\teaching.mdf',  --绝对路径
   SIZE = 10,  
   MAXSIZE = 50,  
   FILEGROWTH = 5   
)  
LOG ON(   
    NAME = 'teaching_log',  
    FILENAME = 'E:\teaching\teaching.ldf',  
    SIZE = 5MB,  
    MAXSIZE = 25MB,  
    FILEGROWTH = 5MB   
)  
GO

在teaching数据库中创建学生表student

USE teaching 
GO

IF EXISTS (
  SELECT * FROM sys.objects 
  WHERE name = N'student'
)
DROP TABLE student
CREATE TABLE student(
	studentno nchar(10) NOT NULL,
	sname nchar(8) NOT NULL,
	sex nchar(1) NOT NULL,
	birthday datetime NOT NULL,
	classno nchar(6) NOT NULL,
    	point smallint NOT NULL,
    	phone nchar(12) NOT NULL,
    	Email nchar(20) NOT NULL,
	CONSTRAINT CK_sex CHECK (sex='男' OR sex='女'),
    	CONSTRAINT PK_student PRIMARY KEY CLUSTERED (studentno  ASC)
) 
GO


你可能感兴趣的:(SQL,Server)