alter table 'xyztaable' auto_increment=201; not working in mysql. why? - mysql

Dear MySQL pros out there: I wonder what I am doing wrong. My code is like:
use testdb;
drop table testtable;
create table testtable (
ID int NOT NULL,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20));
Select * from testtable;
alter table testtable auto_increment = 7001;
insert into testtable (lastn,firstn) values('kim','jeff');
Select * from testtable;
insert into testtable (lastn,firstn) values('Lee','jim');
Select * from testtable;
The table generated as follows: (no effect from "alter" statement)
# ID, lastn, firstn
'0', 'kim', 'jeff'
'0', 'Lee', 'jim'

Either change your CREATE TABLE command to set the ID field to auto increment and initialise it like this:
create table testtable (
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20))
AUTO_INCREMENT = 7001;
or alter the table afterwards:
create table testtable (
ID int NOT NULL,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20));
ALTER TABLE testtable MODIFY COLUMN ID INT PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE testtable AUTO_INCREMENT = 7001;

ID needs to be both AUTO_INCREMENT and the PRIMARY KEY. (Those are "sufficient" but not completely "necessary".)

Related

How do I add data from different tables into one new table in mySQL?

CREATE TABLE CU_ORDER
( cordernumber INT PRIMARY KEY,
fname_lname VARCHAR(50) NOT NULL,
product_name VARCHAR(100) NOT NULL,
);
CREATE TABLE TRACKING_NUMBER
( trnumber INT PRIMARY KEY
);
INSERT INTO CU_ORDER VALUES(456, 'John Doe' , Table);
INSERT INTO TRACKING_NUMBER(276734673);
I am trying to created a table called Package and in the table it will have all the items from cu_order and all the items from tracking_number. How will I add all of the attributes of this table to one table. What am I doing wrong?
CREATE TABLE PACKAGE
( orderno INT PRIMARY KEY,
fname VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
trno INT PRIMARY KEY);
INSERT INTO PACKAGE (........
The two tables do not seem to have a relation, so, presumably, you want a cartesian product of both tables. If so, you can use the insert ... select ... syntax with a cross join:
insert into package(orderno, fname, name, trno)
select
co.cordernumber,
co.fname_lname,
co.product_name,
tn.trnumber
from cu_order co
cross join tracking_number tn
This inserts all possible combinations of rows from both source tables in the target table.
You should also fix the declaration of the package table: yours has two primary keys, which is not allowed. Instead, you probably want a compound primary key made of both columns:
create table package (
orderno int,
fname varchar(50) not null,
name varchar(100) not null,
trno int,
primary key(orderno, trno)
);
You can create a new table from the data of another table (or several tables) by appending a SELECT statement to the CREATE TABLE statement.
However, your two source tables are missing the 1:1 relation allowing this to work, which I assume is the cordernumber of CU_ORDER. It appears the table TRACKING_NUMBER is missing a 'cordernumber' column.
CREATE TABLE TRACKING_NUMBER (
trnumber INT PRIMARY KEY,
cordernumber INT
);
After you added the column 'cordernumber' to TRACKING_NUMBER, you can create the new table PACKAGE with:
CREATE TABLE PACKAGE (
orderno INT PRIMARY KEY,
fname VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
trno INT PRIMARY KEY
)
SELECT
CU_ORDER.cordernumber AS orderno,
CU_ORDER.fname_lname AS fname,
CU_ORDER.product_name AS name,
TRACKING_NUMBER.trnumber AS trno
FROM CU_ORDER, TRACKING_NUMBER
WHERE CU_ORDER.cordernumber=TRACKING_NUMBER.cordernumber;

JOIN table, receiving NULL answers

I have a problem on a script I'm writing and I don't know where I'm wrong.
I have multiple tables that are being filled from some txt files.
Afterwards I ALTER a table adding a column that must be filled from the result of a JOIN with two other tables.
More specific in my current db I'm trying to insert in table 2 the id of the doctors that are currently in table 1 and 2. One table contains the id and name, and the 2nd the name.
Running this script:
insert into Intermediara.id_provizoriumed
select Medici.id_medic is not null
FROM Medici
LEFT Join Intermediara
ON Medici.Nume COLLATE utf8_romanian_ci =Intermediara.NumeMedic;
or
Insert into Intermediara(id_provizoriumed)
Select DISTINCT Pacienti.Id_pacient
FROM Pacienti
left JOIN Intermediara
ON Pacienti.NumePacient COLLATE utf8_romanian_ci = Intermediara.NumePacient;
will result me in NULL answers;
Any ideas, please? :<
*More info on the project intself:
I'm using a procedure to create the tables:
delimiter $$
CREATE PROCEDURE init()
begin
CREATE TABLE Medici (
Nume VARCHAR(50),
Prenume VARCHAR(225),
Statut ENUM ('primar', 'specialist'),
Specialitate VARCHAR(20),
UNIQUE (Nume, Prenume)) DEFAULT CHARACTER SET utf8 COLLATE utf8_romanian_ci;
ALTER TABLE Medici ADD COLUMN Id_medic int AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE Medici ADD COLUMN Tip ENUM ('primar', 'specialist') AFTER Prenume;
ALTER TABLE Medici DROP COLUMN Statut;
DESCRIBE Medici;
CREATE TABLE Pacienti (
Id_pacient int AUTO_INCREMENT NOT NULL PRIMARY KEY,
NumePacient VARCHAR(50),
PrenumePacient VARCHAR(100),
UNIQUE (NumePacient, PrenumePacient))DEFAULT CHARACTER SET utf8 COLLATE utf8_romanian_ci;
DESCRIBE Pacienti;
CREATE TABLE Cabinete (
Id_cabinet int AUTO_INCREMENT NOT NULL PRIMARY KEY,
Denumire VARCHAR(50),
UNIQUE (Denumire));
DESCRIBE Cabinete;
CREATE TABLE Vizite (
IntervalData DATETIME,
id_m int,
id_p int,
id_Cab int,
FOREIGN KEY (id_m) REFERENCES Medici(Id_medic),
FOREIGN KEY (id_p) REFERENCES Pacienti(Id_pacient),
FOREIGN KEY (id_Cab) REFERENCES Cabinete(Id_cabinet)
);
DESCRIBE Vizite;
CREATE TABLE Intermediara (
DataVizita VARCHAR(50),
OraIntrare TIME,
NumePacient VARCHAR(50),
PrenumePacient VARCHAR(50),
NumeMedic VARCHAR(50),
PrenumeMedic VARCHAR(50),
Cabinet VARCHAR(50)) DEFAULT CHARACTER SET utf8 COLLATE utf8_romanian_ci;
DESCRIBE Intermediara;
end$$
delimiter ;
Afterwards I'm loading the data in the tables Medici and Intermediara;
Then adding the info I need in:
INSERT IGNORE INTO Pacienti(NumePacient, PrenumePacient) SELECT NumePacient, PrenumePacient FROM Intermediara;
INSERT IGNORE INTO Cabinete(Denumire) SELECT Cabinet FROM intermediara;
And then what I'm trying to do is to add in Intermediara after adding:
ALTER TABLE Intermediara add column id_provizoriumed int collate utf8_romanian_ci;
ALTER TABLE Intermediara add column id_provizoriupac int collate utf8_romanian_ci;
ALTER TABLE Intermediara add column id_provizoriucab int collate utf8_romanian_ci;
to add the id from Medici (id_medic) in Intermediara by doing a JOIN having Medici.Nume and Intermediara.NumeMedic in the tables, the id must be distinct;

How to generate sequence in MySQL 5.1community edition

Can somebody please show me how generate a sequence in MySQL 5.1?
create table users
(
user_id int unsigned not null auto_increment primary key,
username varbinary(32) unique not null
...
)
engine=innodb;
you could also do
drop table if exists users_seq;
create table users_seq
(
next_val int unsigned not null default 0
)
engine = innodb;
insert into users_seq values (0);
drop table if exists users;
create table users
(
user_id int unsigned not null primary key,
username varbinary(32) unique not null
)
engine=innodb;
delimiter #
create trigger users_before_ins_trig before insert on users
for each row
begin
declare v_id int unsigned default 0;
select next_val+1 into v_id from users_seq;
set new.user_id = v_id;
update users_seq set next_val = v_id;
end#
delimiter ;
insert into users (username) values ('f00'),('foo'),('bar');
select * from users;
select next_val from users_seq;
MySQL doesn't have sequences, however it does have the auto-increment feature.
http://www.experts123.com/q/does-mysql-5.1-have-sequences.html
CREATE TABLE test (
customer_id bigint(21) NOT NULL PRIMARY KEY AUTO_INCREMENT
);

how to design a table in mysql with two auto_increment fields

For a project we do the database design at the moment. We think we should use two auto_increment fields in one table.
table master:
`pid` int(10) NOT NULL auto_increment,
`iid` int(10) NOT NULL auto_increment,
...
To start with a alternate auto_incremet you can use ALTER TABLE tbl AUTO_INCREMENT = 100000; This will work only for the whole table 'tbl'.
auto_increment for pid should be 50000000 and auto_increment for iid should be 80000000
We want to avoid splitting it into 3 tables with relations master -> table.pid and master -> table.iid.
altering the table is not working cause
/* SQL Error (1075): Incorrect table definition; there can be only one auto column and it must be defined as a key */
Is it possible or what alternative do you recommend?
If you cannot use two auto columns I think you must redesign your database. What do you need exactly?
I dont fully understand your question but you can use triggers to maintain key values like the following:
drop table if exists grid;
create table grid
(
grid_id int unsigned not null auto_increment primary key,
name varchar(255) not null,
next_token_id int unsigned not null default 0,
next_node_id int unsigned not null default 0
)
engine = innodb;
drop table if exists grid_token;
create table grid_token
(
grid_id int unsigned not null,
token_id int unsigned not null,
name varchar(255) not null,
primary key (grid_id, token_id) -- note clustered PK order (innodb only)
)
engine = innodb;
drop table if exists grid_node;
create table grid_node
(
grid_id int unsigned not null,
node_id int unsigned not null,
name varchar(255) not null,
primary key (grid_id, node_id) -- note clustered PK order (innodb only)
)
engine = innodb;
-- TRIGGERS
delimiter #
create trigger grid_token_before_ins_trig before insert on grid_token
for each row
begin
declare tid int unsigned default 0;
select next_token_id + 1 into tid from grid where grid_id = new.grid_id;
set new.token_id = tid;
update grid set next_token_id = tid where grid_id = new.grid_id;
end#
create trigger grid_node_before_ins_trig before insert on grid_node
for each row
begin
declare nid int unsigned default 0;
select next_node_id + 1 into nid from grid where grid_id = new.grid_id;
set new.node_id = nid;
update grid set next_node_id = nid where grid_id = new.grid_id;
end#
delimiter ;
-- TEST DATA
insert into grid (name) values ('g1'),('g2'),('g3');
insert into grid_token (grid_id, name) values
(1,'g1 t1'),(1,'g1 t2'),(1,'g1 t3'),
(2,'g2 t1'),
(3,'g3 t1');
insert into grid_node (grid_id, name) values
(1,'g1 n1'),(1,'g1 n2'),
(2,'g2 n1'),
(3,'g3 n1'),(3,'g3 n2');
select * from grid;
select * from grid_token;
select * from grid_node;

Mysql - second auto_increment col with diff behaviour

I have a proposals table like this
- ID (auto_increment)
- proposal_id
- client_id
There a way in sql that the proposal_id increments just for each client_id
example:
ID proposal_id client_id
1 1 1
2 1 2
3 2 1
4 3 1
5 2 2
6 3 2
i know i can get the last poposal_id and +1 and i add the new entry... but i dont want to do a sql instruction just to get this value... instead i want to use in a sql!
Tkz
Roberto
As I understand you wish to have proposal_id as a sequence in a continuos manner per client_id. Either you should normalize the table to split into per-client-table [tricky and not advisable] to do this or write a SELECT
I think this is what you want if using innodb (recommended) although you can simplify this with myisam
delimiter ;
drop table if exists customer;
create table customer(
cust_id int unsigned not null auto_increment primary key,
name varchar(255) unique not null,
next_proposal_id smallint unsigned not null default 0
)engine = innodb;
insert into customer (name) values ('c1'),('c2'),('c3');
drop table if exists proposal;
create table proposal(
cust_id int unsigned not null,
proposal_id smallint unsigned not null,
proposal_date datetime not null,
primary key (cust_id, proposal_id) -- composite clustered primary key
)engine=innodb;
delimiter #
create trigger proposal_before_ins_trig before insert on proposal for each row
begin
declare new_proposal_id smallint unsigned default 0;
select next_proposal_id+1 into new_proposal_id from customer
where cust_id = new.cust_id;
update customer set next_proposal_id = new_proposal_id where cust_id = new.cust_id;
set new.proposal_id = new_proposal_id;
set new.proposal_date = now();
end#
delimiter ;
insert into proposal (cust_id) values (1),(2),(1),(3),(2),(1),(1),(2);
select * from proposal;
select * from customer;
hope it helps :)
i've added the myisam version below for good measure:
drop table if exists customer;
create table customer(
cust_id int unsigned not null auto_increment primary key,
name varchar(255) unique not null
)engine = myisam;
insert into customer (name) values ('c1'),('c2'),('c3');
drop table if exists proposal;
create table proposal(
cust_id int unsigned not null,
proposal_id smallint unsigned not null auto_increment,
proposal_date datetime not null,
primary key (cust_id, proposal_id) -- composite non clustered primary key
)engine=myisam;
insert into proposal (cust_id,proposal_date) values
(1,now()),(2,now()),(1,now()),(3,now()),(2,now()),(1,now()),(1,now()),(2,now());
select * from customer;
select * from proposal order by cust_id;
I think that you could design a complicated enough query to take care of this without any non-sql code, but that's not in the spirit of what you're asking. There is not a way to create the type of field-specific increment that you're asking for as a specification of the table itself.