I'm using sql server 2008 management studio. Im trying to create two seemingly simple tables but continue getting this error at execution. I have tried changing the names of these tables to many different variations. Nothing is helping. Here below is what I am typing.
USE POS410;
go
CREATE TABLE [empl]
(
employeeID INT NOT NULL,
lname VARCHAR(15) NOT NULL,
fname VARCHAR(15) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state VARCHAR(10) NOT NULL,
areacode INT NOT NULL,
tnumber INT NOT NULL,
EEO1classification VARCHAR(10) NOT NULL,
hiredate DATE NOT NULL,
salary INT NOT NULL,
gender VARCHAR(1) NOT NULL,
age INT NOT NULL,
clocknumb INT NOT NULL,
PRIMARY KEY(employeeID),
UNIQUE(employeeID),
UNIQUE(clocknumb),
FOREIGN KEY(clocknumb) REFERENCES [jb_ttl](Empnum),
);
go
CREATE TABLE [jb_ttl]
(
EEO1 VARCHAR(10) NOT NULL,
JobTitle VARCHAR(15) NOT NULL,
JobDscrpt TEXT NOT NULL,
ExemptNonExemptStatus VARCHAR NOT NULL,
Empnum INT NOT NULL,
PRIMARY KEY (Empnum),
);
go
Here is the error report:
Msg 2714, Level 16, State 6, Line 2 There is already an object named 'empl' in the database. Msg 2714, Level 16, State 6, Line 2 There is already an object named 'jb_ttl' in the database.
I would say that you've tried to run this script more than once. It would have failed the first time because you're trying to create the first table with a foreign key to the second, which didn't exist yet. I use the word "didn't" because if my guess is correct, it does now, and that's why you're getting the object already exists error.
As a side note, SQL has supported wrapping DDL (creating/modifying objects) at least going back to 2005. I normally wrap table changes in transactions, and don't commit the transaction until everything goes through with no error messages.
Give this a run to confirm if jb_ttl already exists:
select * from master.dbo.sysobjects where [name] = 'jb_ttl'
That will show you if anb object named jb_ttl already exists. Could it already exist as a procedure or trigger perhaps?
Related
I get this message after I dropped the table and the database completely
Msg 2714, Level 16, State 6, Line 12
There is already an object named 'Users' in the database
I tried to create and drop the same database but no use.
I also dropped the same table but when I want to create it, it shows the same error again.
I'll be glad for any help
CREATE DATABASE Music
GO
CREATE TABLE [Users](
[User_ID] int NOT NULL identity (1,1) Primary key,
[UserName] nvarchar (30) NOT NULL,
[UserEmail] nvarchar (30) NOT NULL,
[Password] nvarchar (30) NOT NULL
)
GO
CREATE DATABASE Music GO
USE [Music]
GO
CREATE TABLE [Users]( [User_ID] int NOT NULL identity (1,1) Primary key, [UserName]
nvarchar (30) NOT NULL, [UserEmail] nvarchar (30) NOT NULL, [Password] nvarchar (30) NOT NULL ) GO
you are creating the table in the database where you have the initial query to. See above to first switch to the new DB...
I have just started with IBM db2 and am running an sql query which I suppose is right. But nothing is happening when I hit the run button. Could anyone help?
Create table INSTRUCTOR
CREATE TABLE INSTRUCTOR
(ins_id INTEGER PRIMARY KEY NOT NULL,
lastname VARCHAR(15) NOT NULL,
firstname VARCHAR(15) NOT NULL,
city VARCHAR(15),
country CHAR(2)
);
Under the result area it is displaying "waiting" and nothing actually happens.
Try
CREATE TABLE INSTRUCTOR
( ins_id INTEGER PRIMARY KEY NOT NULL,
lastname VARCHAR(15) NOT NULL,
firstname VARCHAR(15) NOT NULL,
city VARCHAR(15),
country CHAR(2)
);
That should work for you assuming that you have ; set as the statement terminator in your query interface (it is the default in most SQL interfaces for Db2).
Alternatively, it might be that your CREATE TABLE is waiting for a lock on the system catalog. You can look at currently held lock with MON_GET_LOCKS https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0056428.html
I am simply trying to run a basic SQL script to recreate a database.
The database was initially created in SQLite, and I exported it using DB Browser for SQLite.
The start of the file looks like this:
BEGIN TRANSACTION;
CREATE TABLE "AspNetUsers"
(
`Id` varchar(128) NOT NULL,
`Email` varchar(256) DEFAULT NULL,
`EmailConfirmed` tinyint(1) NOT NULL,
`PasswordHash` longtext,
`SecurityStamp` longtext,
`PhoneNumber` longtext,
`PhoneNumberConfirmed` tinyint(1) NOT NULL,
`TwoFactorEnabled` tinyint(1) NOT NULL,
`LockoutEndDateUtc` datetime DEFAULT NULL,
`LockoutEnabled` tinyint(1) NOT NULL,
`AccessFailedCount` int(11) NOT NULL,
`UserName` varchar(256) NOT NULL,
`IsActivated` tinyint(1) NOT NULL DEFAULT (0),
`Organisation` TEXT NOT NULL,
PRIMARY KEY(`Id`)
);
I created a new db and when running the query in SSMS I get this annoying error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I tried deleting all the whitespace between the first ( and 'Id' but then I just get
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '`'.
I also tried replacing the `s with 's but with the same result....
I'm pretty sure the server I'm trying to execute this on is running SQL Server Express - not sure if that makes a difference
Why must life be so difficult?
The code is rather specific to SQLite in several respects:
The use of backticks is non-standard.
Having a length for integer columns in non-standard.
text and longtext are non-standard.
The equivalent create table statement in SQL Server would be:
CREATE TABLE AspNetUsers (
Id varchar(128) NOT NULL,
Email varchar(256) DEFAULT NULL,
EmailConfirmed tinyint NOT NULL,
PasswordHash varchar(max),
SecurityStamp varchar(max),
PhoneNumber varchar(max),
PhoneNumberConfirmed tinyint NOT NULL,
TwoFactorEnabled tinyint NOT NULL,
LockoutEndDateUtc datetime DEFAULT NULL,
LockoutEnabled tinyint NOT NULL,
AccessFailedCount int NOT NULL,
UserName varchar(256) NOT NULL,
IsActivated tinyint NOT NULL DEFAULT (0),
Organisation varchar(max) NOT NULL,
PRIMARY KEY (Id)
);
Except for the varchar(max), this would be pretty standard for any database.
Some notes:
You probably don't need varchar(max) for any of these fields. Although you can use it, it looks awkward to have a phone number that could occupy megabytes of data.
You could probably replace the tinyints with bits.
DEFAULT NULL is redundant.
I am doing school project and I have to create table and insert data by myself. The following is my SQL statements.
CREATE TABLE Package
(pkgcode char(4) primary key,
pkgname varchar(30) not null,
pkgdescription varchar(60) not null,
pickuplocation varchar(20) not null,
costperadult decimal (7,2) not null,
costperchild decimal (7,2) not null,
pkgremarks varchar(30) null)
INSERT INTO Package
VALUES
('K009',
'8D7N FUJIAN/MT.WUYI',
'A whole new experience: enjoy multi nation architecture in Gulangyu Island and UNESCO Mt.Wuyi',
'Jurong East',
700.00,
500.00,
'Hotel accommodation will be 4 stars.')
But I got the following error which is at the insert statement.
(Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.)
I would appreciate if anyone of you can help me. Thank you.
You're trying to insert a description that's too long for the column's specification. Just create the column with a larget size (e.g., here 100 characters should suffice) and you should be OK:
CREATE TABLE Package
(pkgcode char(4) primary key,
pkgname varchar(30) not null,
pkgdescription varchar(100) not null, -- Here!
pickuplocation varchar(20) not null,
costperadult decimal (7,2) not null,
costperchild decimal (7,2) not null,
pkgremarks varchar(30) null
)
This very simple query I am trying to execute in SQL Server Management Studio. But it gives error:
CREATE TABLE `contact` (
`contact_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`telephone` varchar(45) NOT NULL,
PRIMARY KEY (`contact_id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8
Error is:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I also tried by replacing with'` but still no help
The backtick (`) character is only used for MySql. It's not standard. Sql Server is a completely different animal. You need to use square-brackets ([ and ] -- also not standard) to enclose table and column names with Sql Server. Sql Server also supports double quotes ("), which are part of the SQL Standard, but for some reason less-common.
While I'm at it, the ENGINE and AUTO_INCREMENT, and CHARSET clauses are also MySql-specific.
Try this:
CREATE TABLE contact (
contact_id int IDENTITY(25,1) NOT NULL PRIMARY KEY,
[name] varchar(45) NOT NULL,
email varchar(45) NOT NULL,
"address" varchar(45) NOT NULL,
telephone varchar(45) NOT NULL
)
in tsql
CREATE TABLE contact (
contact_id int identity(0,25) NOT NULL,
name nvarchar(45) NOT NULL,
email nvarchar(45) NOT NULL,
address nvarchar(45) NOT NULL,
telephone nvarchar(45) NOT NULL
CONSTRAINT contact_PK PRIMARY KEY (contact_id)
)
You can't specify engine.
identity(0,25) means initial value = 0, increment = 25
You don't specify character set for the table, you can declare individual columns as varchar or nvcarchar -- the n stands for unicode. You can also specify a collation sequence for each column.
If you want a column name that is irregular (keyword, embedded space, etc.) you quote the column name as [column name] -- don't use irregular column names if you have a choice, it just makes things a pain to use.
Try like this:
CREATE TABLE contact (
contact_id[int] IDENTITY(1,1) NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
address varchar(45) NOT NULL,
telephone varchar(45) NOT NULL,
PRIMARY KEY (contact_id)
)
Hope this may help you.