问题
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:
Right click the "Tables" folder under your database.
Choose "New Table".
Enter in a new column names and data types. You can choose properties of your columns from the properties window.
Click the save button or use Ctrl-S.
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:
Click the "New Query" button in the upper left.
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) )Select the text and press F5 to execute.
It should say: "Command(s) completed successfully."
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