sql server create語(yǔ)句實(shí)例
作者:雪山飛鵠
在SQL語(yǔ)言中,create語(yǔ)句即可以創(chuàng)建數(shù)據(jù)庫(kù),也是可以創(chuàng)建表,是SQL語(yǔ)句中最重要也是最常用的語(yǔ)句,下面就將為您介紹一個(gè)使用create語(yǔ)句創(chuàng)建數(shù)據(jù)庫(kù)的例子,供您參考。
sql server create語(yǔ)句用于創(chuàng)建數(shù)據(jù)庫(kù)和表,下面就將為您介紹使用sql server create語(yǔ)句創(chuàng)建數(shù)據(jù)庫(kù)的實(shí)例,供您參考,希望對(duì)您更深入了解sql server create語(yǔ)句有所幫助。
- use master --切換到master數(shù)據(jù)庫(kù)
- go
- --檢測(cè)是否存在同名的數(shù)據(jù)庫(kù)
- if exists(select 1 from sysdatabases where name = 'tour')
- begin
- drop database tour
- end
- go
- create database tour
- on --數(shù)據(jù)文件
- (
- name = 'tour_mdf', --數(shù)據(jù)文件邏輯名
- filename = 'D:\tour.mdf',--數(shù)據(jù)文件存放路徑
- size = 1MB,--初始大小
- maxsize = 10MB,--最大大小
- filegrowth = 1MB--增長(zhǎng)速度
- )
- log on --日志文件
- (
- name = 'tour_ldf', --日志文件邏輯名
- filename = 'D:\tour.ldf',--日志文件存放路徑
- size = 1MB,--初始大小
- maxsize = 10MB,--最大大小
- filegrowth = 1MB--增長(zhǎng)速度
- )
- go
- use tour
- go
- 創(chuàng)建數(shù)據(jù)庫(kù)表
- if exists(select * from sysobjects where name='stuInfo') drop table stuInfo
- create table stuInfo /*-創(chuàng)建學(xué)員信息表-*/
- (
- stuNo varchar(6) not null unique, --學(xué)號(hào),非空(必填)
- stuName varchar(20) not null , --姓名,非空(必填)
- stuAge int not null, --年齡,INT類型默認(rèn)為4個(gè)字節(jié)
- stuID NUMERIC(18,0), --身份證號(hào)
- stuSeat int IDENTITY (1,1), --座位號(hào),自動(dòng)編號(hào)
- stuAddress text --住址,允許為空,即可選輸入
- )
- go
- if exists(select * from sysobjects where name='stuMarks') drop table stuMarks
- create table stuMarks
- (
- ExamNo varchar(6) not null foreign key references stuInfo(stuNo) , --考號(hào)
- stuNo varchar(6) not null, --學(xué)號(hào)
- writtenExam int not null, --筆試成績(jī)
- LabExam int not null --機(jī)試成績(jī)
- )
- go
- if exists(select * from sysobjects where name='users') drop table users
- create table users
- (
- userID int not null primary key identity(1,1),
- userName varchar(255) not null unique,
- userPWD varchar(255) not null,
- userAge int,
- userBirthDay datetime,
- userEmail varchar(255)
- )
- go
【編輯推薦】
責(zé)任編輯:段燃
來(lái)源:
互聯(lián)網(wǎng)