简体   繁体   English

如何在SQL Server 2005中创建表?

[英]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. 我想在SQL Server 2005的数据库中创建一个名为quiz_mailing_list的表。

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? 如何在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 . 先生,您在optIn位之后缺少逗号(,)。 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 . 有关CREATE TABLE的MSDN文档

How to create a new table in SQL Server 2005 with the GUI: 如何使用GUI在SQL Server 2005中创建新表:

If you don't understand SQL, you can use the Graphical User Interface walk you through creating a new table this way: 如果您不了解SQL,则可以使用图形用户界面通过以下方式逐步创建新表:

  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. 单击保存按钮或使用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: 如何使用SQL在SQL Server 2005中创建新表:

  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. 选择文本,然后按F5键执行。

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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