How do I create a table in SQL Server 2005?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 04:22:43

问题


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?


回答1:


USE YourDatabaseName
GO

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



回答2:


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



回答3:


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))



回答4:


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.




回答5:


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.




回答6:


The command to "use a database" in a T-SQL script is

USE DatabaseName


来源:https://stackoverflow.com/questions/1413244/how-do-i-create-a-table-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!