I have this
CREATE TABLE TEST_CAR (
CARID CHAR(36) NOT NULL,
DATE_NEW TIMESTAMP,
DATE_EDIT TIMESTAMP,
USER_NEW VARCHAR(63),
USER_EDIT VARCHAR(63),
MANUFACT VARCHAR(50),
MODEL VARCHAR(50),
MILEAGE INTEGER,
PURCHDATE TIMESTAMP,
BATCH VARCHAR(50),
FUELTYPE INTEGER,
PRIMARY KEY (CARDID));
and it still returns
Error code 30000, SQL state 42X93: Table 'TEST_CAR' contains a
constraint definition with column 'CARDID' which is not in the table.
Line 1, column 1
CREATE TABLE TEST_LIST (
LISTID CHAR (36) NOT NULL,
CAR_ID CHAR (36) NOT NULL,
DRIVER_ID CHAR (36) NOT NULL,
DATE_EDIT TIMESTAMP,
DATE_NEW TIMESTAMP,
USER_EDIT VARCHAR (63),
USER_NEW VARCHAR (63),
F_FROM VARCHAR (50),
T_TO VARCHAR (50),
KM INTEGER,
DESCRIPTION VARCHAR (50),
DATE_FROM TIMESTAMP,
DATE_TO TIMESTAMP,
PRIMARY KEY (ID));
CREATE INDEX ON TEST_LIST (CAR_ID ASC);
Error code 30000, SQL state 42X93: Table 'TEST_LIST' contains a
constraint definition with column 'ID' which is not in the table. Line
1, column 1 Error code 30000, SQL state 42X01: Syntax error:
Encountered "ON" at line 1, column 14. Line 17, column 1
You are setting CARDID as primary key, whilst you named your column CARID.
Change
PRIMARY KEY (CARDID));
to
PRIMARY KEY (CARID));
Solution to your second problem
You define ID as primary key even though ID is not in your table
Related
im making a database system where i want to make a maximum limit on how many seats are avalible. For each movie there should be only 100 seats. What should i do?
create table customer
(p_No int not null,
name varchar (30),
lastname varchar (30),
constraint p_No_pk primary key(p_No))
create table movie
(title varchar (500),
movie_No int not null,
seats int check(seats < 100),
date datetime,
primary key(movie_No))
create table ticket
(ticket_No int identity (1,1) not null,
movie_No int not null,
p_No int not null,
primary key(ticket_No),
foreign key(movie_No)
references movie (movie_No),
foreign key(p_No)
references customer(p_No))
create a check contraint as below.... SQL SERVER
create table movie
(title varchar (500),
movie_No int not null,
seats int ,
date datetime,
primary key(movie_No)
CONSTRAINT CHK_seats
CHECK (seats <100))
Use a CHECK constraint. E.g.:
CREATE TABLE t1 (x TINYINT NOT NULL UNIQUE CHECK (x BETWEEN 1 AND 100));
Or you can create an AFTER INSERT trigger on the table.
create trigger LimitTable
on YourTableToLimit
after insert
as
declare #tableCount int
select #tableCount = Count(*)
from YourTableToLimit
if #tableCount > 100
begin
rollback
end
go
I recently bought this book called "SQL Queries for Mere Mortals (3rd Edition" to study SQL. It came with MySQL scripts that they said I could run and have example databases to work with and follow along with the book. However, some of the scripts are resulting in an error message. Here is one example script that will not work:
CREATE DATABASE EntertainmentAgencyModify;
USE EntertainmentAgencyModify;
CREATE TABLE Agents (
AgentID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
AgtFirstName nvarchar (25) NULL ,
AgtLastName nvarchar (25) NULL ,
AgtStreetAddress nvarchar (50) NULL ,
AgtCity nvarchar (30) NULL ,
AgtState nvarchar (2) NULL ,
AgtZipCode nvarchar (10) NULL ,
AgtPhoneNumber nvarchar (15) NULL ,
DateHired date NULL ,
Salary decimal(15, 2) NULL DEFAULT 0 ,
CommissionRate float(24) NULL
);
CREATE TABLE Customers (
CustomerID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustFirstName nvarchar (25) NULL ,
CustLastName nvarchar (25) NULL ,
CustStreetAddress nvarchar (50) NULL ,
CustCity nvarchar (30) NULL ,
CustState nvarchar (2) NULL ,
CustZipCode nvarchar (10) NULL ,
CustPhoneNumber nvarchar (15) NULL
);
CREATE TABLE Engagements (
EngagementNumber int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StartDate date NULL ,
EndDate date NULL ,
StartTime time NULL ,
StopTime time NULL ,
ContractPrice decimal(15,2) NULL DEFAULT 0 ,
CustomerID int NULL DEFAULT 0 ,
AgentID int NULL DEFAULT 0 ,
EntertainerID int NULL DEFAULT 0
);
CREATE TABLE Engagements_Archive (
EngagementNumber int NOT NULL ,
StartDate date NULL ,
EndDate date NULL ,
StartTime time NULL ,
StopTime time NULL ,
ContractPrice decimal(15,2) NULL ,
CustomerID int NULL ,
AgentID int NULL ,
EntertainerID int NULL
);
CREATE TABLE Entertainer_Members (
EntertainerID int NOT NULL ,
MemberID int NOT NULL DEFAULT 0 ,
Status smallint NULL DEFAULT 0
);
CREATE TABLE Entertainer_Styles (
EntertainerID int NOT NULL ,
StyleID int NOT NULL DEFAULT 0
);
CREATE TABLE Entertainers (
EntertainerID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
EntStageName nvarchar (50) NULL ,
EntSSN nvarchar (12) NULL ,
EntStreetAddress nvarchar (50) NULL ,
EntCity nvarchar (30) NULL ,
EntState nvarchar (2) NULL ,
EntZipCode nvarchar (10) NULL ,
EntPhoneNumber nvarchar (15) NULL ,
EntWebPage nvarchar (50) NULL ,
EntEMailAddress nvarchar (50) NULL ,
DateEntered date NULL ,
EntPricePerDay decimal(15,2) NULL
);
CREATE TABLE Members (
MemberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
MbrFirstName nvarchar (25) NULL ,
MbrLastName nvarchar (25) NULL ,
MbrPhoneNumber nvarchar (15) NULL ,
Gender nvarchar (2) NULL
);
CREATE TABLE Musical_Preferences (
CustomerID int NOT NULL DEFAULT 0 ,
StyleID int NOT NULL DEFAULT 0
);
CREATE TABLE Musical_Styles (
StyleID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StyleName nvarchar (75) NULL
);
CREATE INDEX AgtZipCode ON Agents(AgtZipCode);
CREATE INDEX CustZipCode ON Customers(CustZipCode);
CREATE INDEX AgentsEngagements ON Engagements(AgentID);
CREATE INDEX CustomerID ON Engagements(CustomerID);
CREATE INDEX EmployeeID ON Engagements(AgentID);
CREATE INDEX EntertainerID ON Engagements(EntertainerID);
ALTER TABLE Engagements_Archive
ADD CONSTRAINT Engagements_Archive_PK PRIMARY KEY
(
EngagementNumber
);
CREATE INDEX CustomerID ON Engagements_Archive(CustomerID);
CREATE INDEX EmployeeID ON Engagements_Archive(AgentID);
CREATE INDEX EntertainerID ON Engagements_Archive(EntertainerID);
ALTER TABLE Entertainer_Members
ADD CONSTRAINT Entertainer_Members_PK PRIMARY KEY
(
EntertainerID,
MemberID
);
CREATE INDEX EntertainersEntertainer_Members ON Entertainer_Members(EntertainerID);
CREATE INDEX MembersEntertainer_Members ON Entertainer_Members(MemberID);
ALTER TABLE Entertainer_Styles
ADD CONSTRAINT Entertainer_Styles_PK PRIMARY KEY
(
EntertainerID,
StyleID
);
CREATE INDEX EntertainersEntertainer_Styles ON Entertainer_Styles(EntertainerID);
CREATE INDEX Musical_StylesEntertainer_Styles ON Entertainer_Styles(StyleID);
CREATE INDEX EntZipCode ON Entertainers(EntZipCode);
ALTER TABLE Musical_Preferences
ADD CONSTRAINT Musical_Preferences_PK PRIMARY KEY
(
CustomerID,
StyleID
);
CREATE INDEX CustomerID ON Musical_Preferences(CustomerID);
CREATE INDEX StyleID ON Musical_Preferences(StyleID);
ALTER TABLE Engagements
ADD CONSTRAINT Engagements_FK00 FOREIGN KEY
(
AgentID
) REFERENCES Agents (
AgentID
),
ADD CONSTRAINT Engagements_FK01 FOREIGN KEY
(
CustomerID
) REFERENCES Customers (
CustomerID
),
ADD CONSTRAINT Engagements_FK02 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
);
ALTER TABLE Entertainer_Members
ADD CONSTRAINT Entertainer_Members_FK00 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
),
ADD CONSTRAINT Entertainer_Members_FK01 FOREIGN KEY
(
MemberID
) REFERENCES Members (
MemberID
);
ALTER TABLE Entertainer_Styles
ADD CONSTRAINT Entertainer_Styles_FK00 FOREIGN KEY
(
EntertainerID
) REFERENCES Entertainers (
EntertainerID
) ON DELETE CASCADE,
ADD CONSTRAINT Entertainer_Styles_FK01 FOREIGN KEY
(
StyleID
) REFERENCES Musical_Styles
(
StyleID
);
ALTER TABLE Musical_Preferences
ADD CONSTRAINT Musical_Preferences_FK00 FOREIGN KEY
(
CustomerID
) REFERENCES Customers (
CustomerID
) ON DELETE CASCADE,
ADD CONSTRAINT Musical_Preferences_FK01 FOREIGN KEY
(
StyleID
) REFERENCES Musical_Styles (
StyleID
);
Running this script results in the following error message:
16:33:35 CREATE DATABASE EntertainmentAgencyModify Error Code: 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 'CREATE DATABASE EntertainmentAgencyModify' at line 1 0.00041
sec
Screenshot
I have tried running other scripts that came with the book that also starts with "CREATE DATABASE" and some of them ran smoothly without any errors, so I'm confused as to why I'm getting this error message. Any help would be greatly appreciated! Thanks all!
Note that since this script has a "CREATE DATABASE" at the top it will fail if that database already exists. That also means that if it works partially and you run it again, it;ll fail completely the next time. "CREATE DATABASE" is a pretty big hammer, so you probably don't want to get into the habit of doing it a lot. But, having said that, here's an even bigger hammer; add
DROP DATABASE IF EXISTS EntertainmentAgencyModify;
in front of the CREATE and similarly for the others. When you have real data, you, of course, never want to use either of these.
Can you try running the following just by typing it in your mysql console:
CREATE DATABASE EntertainmentAgencyModify;
It should simply work, or at least tell you something more then a syntax error.
The code executes fine. May be its a problem with your MySql.
Try to install a new MySql. :)
https://www.mysql.com/downloads/
Thanks everyone! I have no idea why this works, but I found a solution. I had to simply delete "CREATE " from the first line of the script and re-type it. Then it worked. If anyone has any idea why this seemingly irrelevant solution worked, I'd love to know. Thanks everyone for your help!
So I´ve looked trough the web in search for basic date help in sql and nobody seems to be able to help my codes goes like this
create table Hotel
(
id int not null primary key auto_increment,
Name varchar(255)
);
create table Gestur
(
id int not null primary key auto_increment,
nafn varchar(255),
heimili varchar(255),
simi char(7),
netfang varchar(255)
);
create table Bokun
(
id int not null primary key auto_increment,
ID_hotel_fk int references Hotel(id),
ID_gestur_fk int references Gestur(id),
dags_inn date null,
dags_ut date null,
tegund_herbergis char(1)
);
and I can´t seem to get this part right
insert into Bokun
(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values
(1,3, 2015-10-25,2016-12-26,"1"),
(2,5, 2015-04-01, 2016-8-24,"3"),
(3,4, 2014-02-24, 2016-12-08,"1"),
(4,2, 2015-04-26, 2016-12-24,"2"),
(5,4, 2015-07-14, 2016-04-23,"1"),
(6,2, 2015-12-12, 2016-09-12,"3"),
(7,3, 2015-12-26, 2016-05-03,"2"),
(8,2, 2013-09-12, 2014-06-10,"1"),
(9,1, 2015-05-26, 2016-12-28,"1"),
(10,5, 2015-03-30, 2016-06-07,"4");
I only get the error
1292 - Incorrect date value: '1980' for column 'dags_inn' at row 1
You need to quote dates with ' and qoute "1" as '1':
insert into Bokun(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values(1,3, '2015-10-25','2016-12-26','1');
2015-10-25 is treated as 1980 (aritmetic operation substraction)
SqlFiddleDemo
insert into Bokun
(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values
(1,3, '2015-10-25','2016-12-26',"1"),
(2,5, '2015-04-01', '2016-8-24',"3"),
(3,4, '2014-02-24', '2016-12-08',"1"),
(4,2, '2015-04-26', '2016-12-24',"2"),
(5,4, '2015-07-14', '2016-04-23',"1"),
(6,2, '2015-12-12', '2016-09-12',"3"),
(7,3, '2015-12-26', '2016-05-03',"2"),
(8,2, '2013-09-12', '2014-06-10',"1"),
(9,1, '2015-05-26', '2016-12-28',"1"),
(10,5, '2015-03-30', '2016-06-07',"4");
you missed '(quotes).Thanks this helps..
I need to auto_increment the primary key in a mysql database using a trigger. Unfortunately, I am not quite sure how to do this. In the sample code I have provided, I need the employee table primary key to auto_increment beginning with an empty table and a starting value of 200. Then, I need each new insert to increment by 1.Thanks for looking and I hope you are able to help me.
CREATE TABLE department (
dept_name VARCHAR(50) NOT NULL Primary Key
);
CREATE TABLE employee (
emp_id INT(6) unsigned Default 0 Not NULL
, last_name VARCHAR(25) NOT NULL
, first_name VARCHAR(40) NOT NULL
, dept_name VARCHAR(50) NOT NULL
, PRIMARY KEY(emp_id, dept_name)
, FOREIGN KEY(dept_name) REFERENCES department (dept_name)
);
There are several things you need to do:
Declare the emp_id column as AUTO_INCREMENT;
Set the value of AUTO_INCREMENT property of the table to 200;
Do not provide any value for column emp_id when you INSERT rows in table employee.
Change the table creation as below:
CREATE TABLE employee (
emp_id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
last_name VARCHAR(25) NOT NULL,
first_name VARCHAR(40) NOT NULL,
dept_name varchar(50) NOT NULL
PRIMARY KEY(emp_id),
FOREIGN KEY(dept_name) REFERENCES department_tbl(dept_name)
) AUTO_INCREMENT=200;
If the table has an AUTO_INCREMENT column then it must be the PRIMARY KEY of the table. I removed dept_name from the definition of the PK above. I also removed the default value 0 from the emp_id column. It's default value is generated by MySQL using the AUTO_INCREMENT policy.
When you INSERT a new record into the employee table all you have to do is to not provide any value for the emp_id column:
INSERT INTO employee (last_name, first_name, dept_name)
VALUES ('Doe', 'John', 'accounting');
Then use the LAST_INSERT_ID() MySQL function to retrieve the value of the emp_id generated on insertion.
The language or the library you use to develop the client application probably has a function that wraps LAST_INSERT_ID() and returns its value.
For the following SQL I get the error:
Data truncation occurred on a write of column 0Data was 0 bytes long and 0 bytes were transferred.
Code:
CREATE TABLE faculty_members
(
fid INTEGER AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
rank VARCHAR(25) NOT NULL,
office CHAR(8),
phone CHAR(12),
dob DATE NOT NULL,
salary DECIMAL(8,2),
CONSTRAINT faculty_members_pk PRIMARY KEY(fid),
CONSTRAINT faculty_members_ck UNIQUE(first_name,last_name,dob),
CONSTRAINT valid_salary CHECK(salary > 0)
);
And these are the INSERT statements
INSERT INTO faculty_members(first_name,last_name,rank,office,phone,dob,salary) VALUES
('Charles', 'Xavier', 'Professor', 'HSC 641', '563-555-6020', '11/09/1942', 125000),
('Bruce', 'Banner', 'Associate Professor', 'FO1 120', '563-555-8212', '06/20/1969', 87000),
('Hank', 'McCoy', 'Professor', 'FO1 120', '563-555-8212', '06/20/1972', 95000),
('Jeane', 'Grey', 'Assistant Professor', 'ECS 547', '563-555-8239', '03/19/1975', 95000),
('Erik', 'Lehnsherr', 'Professor', 'HSC 641', '563-555-6020', '11/09/1940', 115000),
('Diana', 'Prince', 'Associate Professor', 'HSC 400', '563-555-8212', '07/28/1967', 105100),
('Logan', 'Wolverine', 'Assistant Professor', 'ECS 540', '563-555-8100', '02/27/1964', 82000),
('Ororo', 'Storm', 'Assistant Professor', 'ECS 540', '563-555-8101', '05/01/1973', 82500);
Your dob values are incorrect. MySQL's date format is yyyy-mm-dd. Most like you're getting the default 0000-00-00 stored and the data truncation error from that.
In our case, we were adding a value exceeding the column size.