SQL Server database to create complete code

 

1. Create a database

Master use 
Go 

IF EXISTS (the SELECT * from sysdatabases the WHERE name = 'the Test') 
the begin 
    the SELECT 'already exists in the database' 
    drop the Test Database - If the database already exists, then delete it 
End 
the else 
the begin 
    the Create Database the Test 
    ON Primary - represents a group belonging to the primary file 
    ( 
        name = 'stuDB_data', - logical name of the main data file 
        filename = 'D: \ stuDB_data.mdf' , - physical name of the primary data file 
        size = 5mb, - initial main data file size 
        maxsize = 100mb, - the maximum value of the main data file growth 
        filegrowth = 15% - growth of primary data file 
    ) 
    log ON 
    ( 
        name = 'stuDB_log', - logical name of the log file
        filename = 'D: \ stuDB_log.ldf' , - physical name of the log file 
        size = 2mb, - the initial size of the log file
        maxsize = 20mb, - the maximum log file growth 
        filegrowth = 1mb - growth of the log file 
    ) 
End

  Next is to create a SQL statement data sheet:

use Test - expressed to execute the following SQL statement in the database (the Test) 
Go

  You can first execute the above statement.

 

 

 Or select the database here.

 

use Test - indicates to execute the following SQL statement in the database (the Test) 
Go 

IF EXISTS (SELECT * from the sysobjects WHERE name = 'Student') 
the begin 
    SELECT 'already exists in the table' 
    drop Table Student - remove a table 
End 
the else 
the begin 
    Create Table Student 
    ( 
        s_id int Not null Identity (1,1) Primary key, - the primary key column and growth from the start value of 1, each increment 1 
        S_StuNo VARCHAR (50) Not null, 
        s_name VARCHAR ( 20 is) Not null, 
        S_Sex VARCHAR (10) Not null, 
        S_Height VARCHAR (10) null, 
        S_BirthDate VARCHAR (30) null 
    ) 
End 

- Add constraint                        
the Table the Add constraint A Student the ALTER 
UQ_S_StuNo - constraint name 
unique - type of constraint (unique constraint) 
(S_StuNo) - column name 

- dropping a constraint 
the ALTER the Table Student drop constraint A 
UQ_S_StuNo - constraint name

  

SQL statement creates a table variable:

declare @Score table
(
    Id        int        not null,
    Name    varchar(50)  null
) 

insert into @Score
select '1','刘邦' union
select '2','项羽'

select * from @Score

  

SQL statement creates a temporary table:

- ## indicates the global temporary table 
Create Table ## TEMP 
( 
    Id int Not null, 
    the Name VARCHAR (10) null 
) 

- a partial temporary table # 
Create Table #temp 
( 
    Id int Not null, 
    the Name VARCHAR (10) null 
)

  

SQL statement to create a table and set the primary foreign key relationships:

EXISTS IF (SELECT * from sysobjects WHERE name = 'Course,') 
the begin 
    SELECT 'already exists in the table' 
    drop Table Course, 
End 
the else 
the begin 
    Create Table Course, 
    ( 
      - Column Name field identifies whether the type of the foreign key column is empty (the foreign key column name ) table name associated table (associated field names) 
         Stu_Id int null Foreign Key (Stu_Id) the References Student (s_id), 
         C_Id int not null the Identity (1, 1) Primary Key, 
         c_name VARCHAR (100) not null 
     ) 
End

  

2. The complete SQL Server code

- create a data table 

use stu_db - indicates to execute the following SQL statement in the database (the Test) 
Go 

IF EXISTS (SELECT * from the sysobjects WHERE name = 'Students.') 
The begin 
    SELECT 'already exists in the table' 
    drop Students. Table - - delete table 
End 
the else 
the begin 
    the Create the table Students 
    ( 
        stuID int not null the Identity (1, 1) Primary key, - the primary key column and self-growth, a starting value of 1, each increment 1 
        stuNumber nvarchar (10) not null, 
        stuClass nvarchar (50) Not null, 
        stuname nvarchar (20 is) null, 
        stuSex nvarchar (20 is) Not null, 
        stuAge nvarchar (20 is) Not null, 
    ) 
End

* Students from the SELECT
--查询


INSERT INTO Students VALUES ( '001' , ' software 01', 'Bob', 'M', '18 ') 
INSERT INTO Students VALUES (' 002 ',' software 01 ',' Mike ',' male ',' 18 ') 
INSERT INTO Students VALUES (' 003 ',' software 06 ',' Mary ',' female ', '25') 
INSERT INTO Students VALUES ( '004', 'software 01', 'millet', 'male ', '30') 

SELECT * WHERE stuNumber from Students. = '008' 



- Add 

INSERT INTO [stu_db] [dbo] [Students] ([stuNumber], [stuClass], [stuName], [stuSex], [.. stuAge]) VALUES ( '008' , ' 111 computer', 'Komaki', 'M', '20') 

- for changes 

.. 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=''

  

Guess you like

Origin www.cnblogs.com/domefy/p/12173464.html