简体   繁体   中英

How do I create a table in SQL Server 2005?

I want to create a table called quiz_mailing_list in my database in SQL Server 2005.

With Fields:

id              auto-increment primary key
email           varchar(256)
optIn           tinyint
referringEmail  varchar(256)

Here is what I tried:

CREATE TABLE quiz_mailing_list(
        id int identity(1,1) primary key,
        email varchar(256),
        optIn bit
        referringEmail varchar(256))

I get this error:

System.Data.SqlClient.SqlException: Incorrect syntax near 'referringEmail'

How do I create a table in SQL Server 2005?

USE YourDatabaseName
GO

CREATE TABLE quiz_mailing_list (
    id int identity(1,1) primary key,
    email varchar(256),
    optIn bit,
    referringEmail varchar(256))

This should do the trick...

USE [whatever_db]
GO
/****** Object:  Table [dbo].[quiz_mailing_list]    Script Date: 09/11/2009 17:06:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[quiz_mailing_list](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [email] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [optin] [bit] NOT NULL,
    [referringEmail] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Sir you are missing a comma ( ,) after optIn bit . Try below code

CREATE TABLE quiz_mailing_list(
    id int identity(1,1) primary key,
    email varchar(256),
    optIn bit,
    referringEmail varchar(256))
use MyDatabase
go
create table Quiz_Mailing_List
(
   ID int identity(1,1) primary key clustered,
   Email varchar(256),
   Size tinyint,
   OptIn bit,
   ReferringEmail varchar(256)
)

MSDN documentation on CREATE TABLE .

How to create a new table in SQL Server 2005 with the GUI:

If you don't understand SQL, you can use the Graphical User Interface walk you through creating a new table this way:

  1. Right click the "Tables" folder under your database.

  2. Choose "New Table".

  3. Enter in a new column names and data types. You can choose properties of your columns from the properties window.

  4. Click the save button or use Ctrl-S.

  5. Left click the "Tables" button in object explorer under the database you created it in, and you should see your table.

How to create a new table in SQL Server 2005 using SQL:

  1. Click the "New Query" button in the upper left.

  2. Add this code to the query window:

     use yourdatabase go create table Quiz_Mailing_List ( ID int identity(1,1) primary key clustered, Email varchar(256), Size tinyint, OptIn bit, ReferringEmail varchar(256) ) 
  3. Select the text and press F5 to execute.

  4. It should say: "Command(s) completed successfully."

  5. Left click "Tables" on the Object Explorer pane to see your created table.

在T-SQL脚本中“使用数据库”的命令是

USE DatabaseName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM