1.創(chuàng)建數(shù)據(jù)庫的基本語句
create database database_name
*注意:database_name是你要?jiǎng)?chuàng)建的數(shù)據(jù)庫的名稱
1.1刪除數(shù)據(jù)庫的語句
drop database database_name
2.創(chuàng)建數(shù)據(jù)表
create table table_name
*注意:table_name是你要?jiǎng)?chuàng)建的數(shù)據(jù)庫的名稱
2.1刪除數(shù)據(jù)表的語句
drop table database_name
下面給一個(gè)正規(guī)一點(diǎn)的例子:請看下面的代碼:
1 USE master --使用系統(tǒng)數(shù)據(jù)庫 2 GO 3 IF EXISTS(SELECT * FROM sysdatabases WHERE name=N'DB_MyStudentLife') 4 DROP DATABASE [DB_MyStudentLife]; --如果要?jiǎng)?chuàng)建的數(shù)據(jù)庫存在的話,就刪除 5 GO 6 CREATE DATABASE [DB_MyStudentLife] --創(chuàng)建數(shù)據(jù)庫 7 GO 8 USE [DB_MyStudentLife] --使用數(shù)據(jù)庫 9 GO10 IF EXISTS(SELECT * FROM sysobjects WHERE name=N'MyClass')11 DROP TABLE [MyClass] --如果要?jiǎng)?chuàng)建的數(shù)據(jù)表存在的話,就刪除(注意sysobjects,一定要全部是小寫的,不然有錯(cuò)誤,不能寫成大寫的。)12 GO13 CREATE TABLE MyClass --創(chuàng)建數(shù)據(jù)表14 (15 C_ID INT NOT NULL PRIMARY KEY IDENTITY(1,1), --班級編號16 C_Name NVARCHAR(200) not null, --班級名稱17 C_Descr nvarchar(max) not null --班級簡介18 19 );20 GO21 IF EXISTS(SELECT * FROM sysobjects WHERE name=N'MyStudent')22 DROP TABLE MyStudent23 GO24 CREATE TABLE MyStudent25 (26 S_ID int not null primary key identity(1,1), --學(xué)號27 S_Name nvarchar(50) not null, --姓名28 S_Gender char(2) not null, --性別29 S_Address nvarchar(max) not null , --地址30 S_Phone nvarchar(50)not null, --電話31 S_Age int not null, --年齡32 S_Birthday datetime not null, --生日33 S_CardID int not null, --身份證號碼34 S_CID int not null references MyClass(C_ID) --班級編號35 36 );
新聞熱點(diǎn)
疑難解答
圖片精選