Can't create new table - sql-server-2000

I'm new to SQL Server 2000 and face a problem. I want to make a new table but I encounter an error message with the following code:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10)not null,
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30) default,
Tahun_Terbit varchar(4) default,
Bahasa varchar(4) check,
Harga_Jual money,
)
and here's the error code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Buku'.

You have three problems:
You have multiple cases where you say default but don't specify anything
You have a check but don't specify anything
Your last column definition says money, with a trailing comma
Now, none of these lead to the exact error message you're getting, so maybe there is more you're not telling us (is there more code before the create table bit?), but these little syntax problems are far too localized to be useful in this Q & A format.
EDIT
I just ran this on a SQL Server 2000 instance and it worked just fine:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10) not null, -- added space here
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30), -- removed default here
Tahun_Terbit varchar(4), -- removed default here
Bahasa varchar(4), -- removed check here
Harga_Jual money -- removed comma here
)
So I'm not sure what you're doing differently, but I can't get the error message you are seeing with the information you've provided in the question. If you're still getting an error message with this code (and only this code), you'll need to provide more information, such as ##VERSION, what interface you're using to submit the create table statement to SQL Server, etc.

Related

New to SQL and already having an issue

I'm doing a group project to learn SQL, I'm using jdoodle as an online IDE for now and w3schools. I feel so weird for asking this because this is literally my first attempt but I get an error.
CREATE DATABASE turing;
CREATE TABLE Suppliers (
SupplierNumber int,
SupplierName varchar(255),
SupplierAddress varchar(255),
);
Error: near line 1: in prepare, near "DATABASE": syntax error (1)
Error: near line 2: in prepare, near ")": syntax error (1)
I'm just like copying exactly what w3schools taught me?
I don't see any errors in here, But these are some best practises,
RUN the SQL queries one by one in case if that's causing the error. First create the Database and then Create the Table. Do both seperately.
CREATE TABLE Suppliers (
supplierNumber int PRIMARY KEY NOT NULL,
supplierName varchar(255),
supplierAddress varchar(255)
);
Best practise is to have the column names in lowerCamelCase.
Normally we don't use comma for the last line. It is Unnecessary
Having a Primary key for every table is good. Make it PRIMARY KEY and NOT NULL at the same time to prevent some error in the future.

PostgreSQL syntax error at or near INT

I made a script to create a database with PostgreSQL.
So I copy in my script, click "Analyze & Explain" in pgAdmin4 and I have no clue why it says I have a syntax error at or near 'INT' on idSituationFamiliale.
I really can't see what's wrong...
--Personnes
--
CREATE TABLE SITUATION_FAMILIALE (
idSituationFamiliale INT NOT NULL,
intituleSituationFamiliale VARCHAR(50) NOT NULL,
PRIMARY KEY(idSituationFamiliale)
);
The query is fine if you RUN it. It is wrong if you EXPLAIN / ANALYZE it.
The doc says that you can explain a CREATE TABLE AS, not a pure CREATE TABLE statement. While the former contains a SELECT statement that can be explained/analyzed, the later has nothing to be explained/analyzed and fails on the 1st field, regardless of its name or type.
You should be using integer as opposed to int.
e.g
--Personnes
--
CREATE TABLE SITUATION_FAMILIALE (
idSituationFamiliale INTEGER NOT NULL,
intituleSituationFamiliale VARCHAR(50) NOT NULL,
PRIMARY KEY(idSituationFamiliale)
);

PHPmyadmin won't accept CREATE TABLE statement

I'm trying to use a simple CREATE TABLE statement to create a new table in my very first SQL-database. PHPmyadmin won't accept the code and gives me an error statement.
There doesn't appear to be anything wrong with the syntax of my SQL command. In fact, I receive the same error statement when I copy and past an example code from any internet tutorial to create a table.
this is my SQL command:
CREATE TABLE Guestbook(
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Name varchar(50) NOT NULL,
Message TEXT NOT NULL,
Date datetime,
Sport varchar(30),
Practicioner BOOLEAN default 0,
)
This is the error statement:
Static analysis:
3 errors were found during analysis.
A symbol name was expected! (near "Name" at position 74)
Unexpected beginning of statement. (near "50" at position 87)
Unrecognized statement type. (near "NOT NULL" at position 91)
SQL query:
CREATE TABLE Guestbook( ID int AUTO_INCREMENT PRIMARY KEY NOT NULL, Name varchar(50) NOT NULL, Message TEXT NOT NULL, Date datetime, Sport varchar(30), Practicioner BOOLEAN default 0, )
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 8
I can't imagine why this won't work. I'd like to be able to use to command line in phpMyadmin. and this seems pretty straigtht forward. Yet I've been fiddling around with it for ages and I can't figure out how to create even the simplest possible table.
Can anyone help me out?
You should remove the last comma in your CREATE statement:
CREATE TABLE Guestbook(
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Name varchar(50) NOT NULL,
Message TEXT NOT NULL,
Date datetime,
Sport varchar(30),
Practicioner BOOLEAN default 0
)

Sql server error : The INSERT statement conflicted with the CHECK constraint

The INSERT statement conflicted with the CHECK constraint "ck_str_member_no". The conflict occurred in database "C:\DOCUMENTS AND SETTINGS\KARTHIKEYAN\DESKTOP\KOK\DB\INFT3009_ASS1_C3104855.MDF", table "dbo.Members", column 'str_member_no'.
The statement has been terminated.
I am using .MDF file in my visual studio 2008 Express. How do I solve it?
My insert Procedure :
ALTER PROCEDURE [dbo].[AddNewAGCMember]
-- Add the parameters for the stored procedure here
#str_member_no varchar(6) = '',
#str_member_name varchar(50) = '',
#str_member_password varchar(10) = '',
#str_addr_apartment_no varchar(10) = NULL,
#str_addr_building_name varchar(50) = NULL,
#str_addr_street_name varchar(50) = NULL,
#int_postal_code int = NULL,
#str_country_name varchar(50) = NULL,
#int_contact_no int = NULL,
#str_email_addr varchar(100) = '',
#date_registration date = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO Members
(str_member_no,
str_member_name,
str_member_password,
str_addr_apartment_no,
str_addr_building_name,
str_addr_street_name,
int_postal_code,
str_country_name,
int_contact_no,
str_email_addr,
date_registration)
VALUES
(#str_member_no,
#str_member_name,
#str_member_password,
#str_addr_apartment_no,
#str_addr_building_name,
#str_addr_street_name,
#int_postal_code,
#str_country_name,
#int_contact_no,
#str_email_addr,
#date_registration);
END
Table structure :
str_member_no, varchar(6), Unchecked
str_member_name, varchar(50), Unchecked
str_member_password, varchar(10), Unchecked
str_addr_apartment_no, varchar(10), Checked
str_addr_building_name, varchar(50), Checked
str_addr_street_name, varchar(50), Checked
int_postal_code, int, Checked
str_country_name, varchar(50), Checked
int_contact_no, int, Checked
str_email_addr, varchar(100), Unchecked
date_registration, date, Unchecked
Unchecked
Obviously, your insert is violating a check constraint. This is a constraint in your database the performs a specific check - that a numeric value is in a particular range, that a string is at most n characters long, or whatever.
To find out what the check constraint is, try this:
SELECT
name, definition
FROM
sys.check_constraints
WHERE
name = 'ck_str_member_no'
This will give you the expression that is being checked in the "definition" column.
From that expression, you should be able to determine why your insert is being rejected. Fix the problem and insert again.
If you really cannot fix your data, and if you do not need / want that check constraint in place, you can drop it:
ALTER TABLE dbo.Members
DROP CONSTRAINT ck_str_member_no
Marc
Apparently the INSERT is supplying a value for column 'str_member_no' which SQL has been instructed to refuse given the CHECK constraint. A check constraint is simply a condition imposed on one or several values of fields, before they can be added or inserted. For example, maybe the CHECK requires that all str_member_no be more than 1500 (the lower values being used for, say, administators...) or some other odd "business rule".
By inspecting, or posting here, the DDL for the underlying table creation, as well as the INSERT statement in question, you could get specific understanding of what is wrong with the value (or with the check predicate).
Reading the added code in the question, the str_member_no doesn't seem to be checked, then it may just be that the value passed is too long or null or some other generic reason for refusing it; the error would typically be different but since there is a check, the error shown is based on the assumption that the [custom] check failed.
At any rate, you should check what values are passed to the AddNewAGCMember() StoredProcedure; the problem probably lies there, in a field value that is "bad".
Check the following MySQL bug that has not yet been solved-
MySQL CHECK constraint bug

auto_increment causing errors with CREATE TABLE

I have been using netbeans as a tool for my java, and i have a problem. I read this tutorial and then i tried to create a table using this SQL:
CREATE TABLE CUSTOMERS (
ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(30),
ADDRESS VARCHAR(30),
CITY VARCHAR(30),
STATE_ VARCHAR(30),
ZIP VARCHAR(15),
COUNTRY_ID INTEGER,
PHONE VARCHAR(15),
EMAIL_ADDRESS VARCHAR(50)
)ENGINE=INNODB;
When i tried to run it, I got this error message:
sql state 42X01 : Syntax error :
encountered "AUTO_INCREMENT" at line 2
column 29
and when i delete the AUTO_INCREMENT, another error:
detected ENGINE=INNODB;
can someone help me? Thanks.
You seem to be using MySQL syntax with another database engine. The parts it complained about are precisely the MySQL-specific ones.
my sugestion would be the following
CREATE TABLE CUSTOMERS
( ID INTEGER NOT NULL auto_increment,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(30),
ADDRESS VARCHAR(30),
CITY VARCHAR(30),
STATE_ VARCHAR(30),
ZIP VARCHAR(15),
COUNTRY_ID INTEGER,
PHONE VARCHAR(15),
EMAIL_ADDRESS VARCHAR(50),
PRIMARY KEY (ID));
Dunno what the engine=innodb is for, have you tried without it?
The "engine=innodb" part specifies the database engine that gets used in the database. With MySQL you can specify different engines like "InnoDB", "MyISAM", etc. They have different properties and features - some allow foreign indexes, some do not. Some have different locking mechanisms, some have different atomicity/rollback properties. I don't know the details but if you need a really high-performance database setup you should investigate which engine is best for each type of table you're creating. Also, all my database experience has been with MySQL and I'm not sure if that's what you're using.
Been a long time but if anybody else stumbles on this like I did, a solution that worked for me is instead of using auto_increment, describe the ID column as
ID INTEGER GENERATED ALWAYS AS IDENTITY, WHATEVER VARCHAR(20), ETC ETC...