I want my table's primary key to start from the number 1000 instead of the default. I read around here and the answer is to declare the increment value when creating the table as follows:
AUTO_INCREMENT = 1000;
I tried it when I CREATE my table but it returns a syntax error. Of course I could make the changes on PhpMyAdmin but I want to do it on CREATE instead. Please advice what is wrong. Thanks.
CREATE TABLE IF NOT EXISTS Departments
(
Dept_ID int AUTO_INCREMENT=1000, -- the equals sign returns the syntax error.
Dept_Name varchar(255),
Dept_Address varchar(255),
Dept_Tel int,
PRIMARY KEY (Dept_ID)
);
The starting point must be specified as a table option:
CREATE TABLE IF NOT EXISTS Departments
(
Dept_ID int AUTO_INCREMENT,
Dept_Name varchar(255),
Dept_Address varchar(255),
Dept_Tel int,
PRIMARY KEY (Dept_ID)
) AUTO_INCREMENT=1000;
You can see the full syntax for creating a table here
Related
I have these two tables:
CREATE TABLE Collaborators (
CustomerID INT NOT NULL,
FirstName VARCHAR(25),
LastName VARCHAR(25),
Street VARCHAR(50),
City VARCHAR(50),
State VARCHAR(25),
ZipCode INT,
Telephone VARCHAR(15),
PRIMARY KEY(CustomerID));
CREATE TABLE Orders (
OrderID INT NOT NULL,
CustomerID INT NOT NULL,
SKU VARCHAR(20),
Description VARCHAR(50),
PRIMARY KEY(OrderID),
FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerID));
My goal is to change any instance of "Customer" to "Collaborator". I tried to go one at a time and use an alter table statement.
ALTER TABLE Collaborators CHANGE CustomerID CollaboratorID INT;
ALTER TABLE Orders CHANGE CustomerID CollaboratorID INT;
Here is the error code MySQL spits out at me:
ERROR 1025 (HY000): Error on rename of './QuantigrationUpdates/#sql-668_24' to './QuantigrationUpdates/Collaborators' (errno: 150)
Any help would be greatly appreciated. I've figured that I can't simply change the column names because they have key constraints, but I don't know how to work around that. Thanks.
Update: Am I able to use CREATE VIEW to work around the key constraints?
I'm porting some Postgres SQL to MySQL and am trying to set the starting values of three columns to specific values. The table is as follows:
CREATE TABLE ITEM (
ORDID NUMERIC(4) NOT NULL,
ITEMID NUMERIC(4) NOT NULL,
PRODID NUMERIC(6),
ACTUALPRICE NUMERIC(8,2),
QTY NUMERIC(8),
ITEMTOT NUMERIC(8,2),
CONSTRAINT ITEM_FOREIGN_KEY FOREIGN KEY (ORDID) REFERENCES ORD (ORDID),
CONSTRAINT ITEM_PRIMARY_KEY PRIMARY KEY (ORDID,ITEMID));
The code I'm trying to port is as follows:
CREATE SEQUENCE ORDID
INCREMENT BY 1
START WITH 622
;
CREATE SEQUENCE PRODID
INCREMENT BY 1
START WITH 200381
;
CREATE SEQUENCE CUSTID
INCREMENT BY 1
START WITH 109
;
However, when trying to run this I'm getting the error:
SQL query:
CREATE SEQUENCE ORDIDINCREMENT BY 1 START WITH 622 ;
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 'SEQUENCE ORDID
INCREMENT BY 1
START WITH 622' at line 1
I know that there is no direct equivalent to a SEQUENCE in MySQL but I can't figure out a reasonable way to achieve the same thing without it. Any ideas?
MySQL uses AUTO_INCREMENT for that purpose. Rather than making new sequence types, you apply it to an existing integer column.
Unfortunately you can only have one per table.
There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
And they must be integers, numeric doesn't work. This will probably improve your schema as 9999 orders and items seems very small.
AUTO_INCREMENT applies only to integer and floating-point types.
And if that wasn't enough, you can't have an AUTO_INCREMENT on a multi-key primary key. Only the vastly inferior MyISAM table format allows that.
So you cannot easily translate your PostgreSQL tables to MySQL verbatim.
You sure you want to convert to MySQL?
In your case, item.ordid is a reference so it will be incremented in its own table. item.prodid is probably also a reference and somebody forgot to declare it that. This leaves just item.itemid to be declared AUTO_INCREMENT, but it's part of the primary key. It probably doesn't need to be, it can just be unique.
In fact, the ITEM table seems more like it's tracking orders of products, not items... but then there's also a product ID? I don't know what an "item" is.
You wind up with something like this:
CREATE TABLE ITEM (
ITEMID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
PRODID INTEGER REFERENCES PROD(PRODID),
ORDID INTEGER NOT NULL REFERENCES ORD (ORDID),
ACTUALPRICE NUMERIC(8,2),
QTY NUMERIC(8),
ITEMTOT NUMERIC(8,2),
UNIQUE(ORDID, ITEMID)
)
CREATE TABLE ORD (
ORDID INTEGER PRIMARY KEY AUTO_INCREMENT,
...
) AUTO_INCREMENT = 622;
CREATE TABLE PROD (
PRODID INTEGER PRIMARY KEY AUTO_INCREMENT,
...
) AUTO_INCREMENT = 200381;
You can also set the AUTO_INCREMENT starting point after the fact with ALTER TABLE. Because it's a table attribute, not a column attribute, it happens on the table itself.
ALTER TABLE CUST AUTO_INCREMENT=109;
It's largely unnecessary to set the AUTO_INCREMENT starting point if you're importing an existing data set. AUTO_INCREMENT will always use MAX(column) and it cannot be set lower than this. It doesn't matter what you start it at if the table is already populated.
You can create a table with an AUTO_INCREMENT field and set its initial value.
create table myseq(
my_id int auto_increment primary key
) auto_increment=100;
Or use ALTER TABLE to reset the value whenever you want:
alter table myseq auto_increment = 100;
You can use table with AUTO_INCREMENT key to emulate sequences:
CREATE TABLE ORDID (id INT PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT = 622;
CREATE TABLE PRODID (id INT PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT = 200381;
CREATE TABLE CUSTID (id INT PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT = 109;
Each of the table represents a 'sequence'. To use one in your CREATE TABLE:
CREATE TABLE ITEM (
ORDID INT NOT NULL,
ITEMID NUMERIC(4) NOT NULL,
PRODID NUMERIC(6),
ACTUALPRICE NUMERIC(8,2),
QTY NUMERIC(8),
ITEMTOT NUMERIC(8,2),
CONSTRAINT ITEM_FOREIGN_KEY FOREIGN KEY (ORDID) REFERENCES ORDID (ID),
CONSTRAINT ITEM_PRIMARY_KEY PRIMARY KEY (ORDID,ITEMID));
You can then use INSERT to get a new value from your 'sequence':
INSERT INTO ordid VALUES (null);
SELECT LAST_INSERT_ID();
So I got used to using doctrine so much that I forgot the standard mysql syntax, which I need for an exam.
I want to create a test database called 'youtube'
with an app_users table and an app_videos table.
In "real life" this would be a oneToMany relationship, but I need to
study to create a manyToMany relationship.
so its
"many app_users" -> have "many app_videos"
and the other way around.
so first I created a database;
CREATE DATABASE youtube;
use youtube;
then I created the users and videos table
CREATE TABLE app_users(
id INT AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(40),
first_name VARCHAR(20),
last_name VARCHAR(20),
video_id INT,
PRIMARY KEY(id)
);
CREATE TABLE app_videos(
id INT AUTO_INCREMENT,
video_path VARCHAR(20),
description VARCHAR(20),
likes INT(20),
dislikes INT(20),
user_id INT,
PRIMARY KEY(id)
);
then I created a relationship table
CREATE TABLE vid_uid
(
id INT AUTO_INCREMENT,
video_id INT,
user_id INT,
PRIMARY KEY(id)
);
now what I need to do is somehow connect the columns to eachother but my syntax never works
I know I need to change this
ALTER TABLE app_users
ADD FOREIGN KEY(video_id)
REFERENCES app_videos(video_id);
to something that works :/
You need to change app_videos(video_id) to app_videos(id). Also you want to alter the table vid_uid (not app_users) since thats the one that as references to external keys:
ALTER TABLE vid_uid
ADD FOREIGN KEY(video_id)
REFERENCES app_videos(id);
You'll also need to do:
ALTER TABLE vid_uid
ADD FOREIGN KEY(user_id)
REFERENCES app_users(id);
I am a beginner at this but is it possible (in MySQL workbench) to create an attribute that is a set depended on an attribute from another table?
CREATE TABLE danes (
id INT PRIMARY KEY,
name VARCHAR(50),
nationality VARCHAR(20),
gender CHAR(1),
degree SET ?????????????????? from degree(level)
);
CREATE TABLE degree (
level VARCHAR(10),
subject VARCHAR(20),
institutionawarding VARCHAR(20),
yearawarded DATE,
PRIMARY KEY (level, subject)
);
never mind I got it
I am guessing that you want another table, a junction table:
CREATE TABLE DaneDegrees (
DanesId INT REFERENCES danes(id),
Level VARCHAR(10),
Subject VARCHAR(20),
FOREIGN KEY fk_level_subject(level, subject) REFERENCES Degree(level, Subject)
);
I would, however, have an INT AUTO_INCREMENT PRIMARY KEY in both Danes and Degrees.
apparently you cant just say
<attribute_name> set(get set from another tabe)
the set thing doesn't work that way
eventhough you can apparently update it so it actually can
update table_name
attribute = (select * from another table_name)
I want to create a table relationship with MYSQL PHPMYADMIN.
I have this Create table:
CREATE TABLE students(code_students int(8)not null AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null,
primary key(code_students, code_advisor)
);
and i want to make a create table named advise relationship between code_students, code_advisor.
Ok this is my tryout.
CREATE TABLE advise (
code_students int(8),
code_advisor int(8),
primary key(code_students, code_advisor),
foreign key(code_students)references students(code_students),
foreign key(code_advisor)references students(code_advisor)
);
mySQL says :
A FOREIGN KEY constraint that references a non-UNIQUE key is not standard SQL. It is an InnoDB extension to standard SQL
Try adding the UNIQUE keyword to your first table:
CREATE TABLE students(
code_students int(8)not null unique AUTO_INCREMENT,
name_students varchar(25),
age_students int(3),
degree_program varchar(25),
code_advisor int(8)not null unique,
primary key(code_students, code_advisor)
);
Check out this sqlFiddle and see how it works, then try removing the UNIQUE keywords and see that you get the same error that you mentioned. ( Hit the Build Schema button )
http://sqlfiddle.com/#!2/46b69