Auto_increment trigger - mysql

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.

Related

Nothing shows when I use select *

I created some tables but when I want to check if my data is inside it doesn't show anything when
I use select * from Project it just appears null and I don't have null values I'm not sure what I'm doing wrong it just appears that the foreign key is restrict
`create table Department
(
`did integer not null auto_increment,
Dname varchar(50) default 'HR',
location varchar(50) default 'Chicago',
primary key(did)`
);
`create table Employee`
(
Eid integer not null auto_increment,
DepartmentID integer default 5,
Ename varchar(50) default 'Josh',
Erank integer default 2,
Salary real default 5000.00,
primary key(Eid),
foreign key(DepartmentID) references Department(did)
);
/*drop table Project;*/
create table Project
(
Pid integer not null auto_increment,
DepartmentID integer default 5,
Pname varchar(50) default 'Sorting',
budget real default 5000.00,
StartYear integer default 2000,
primary key(Pid),
foreign key(DepartmentID) references Department(did)
);
insert
into Project(DepartmentID, Pname, budget, StartYear)
values(1, 'OS', 5000.00, 2018),
(2, 'Net', 6000.00, 2020);
select *
from Project;
Check this fiddle. There are no rows in Department, so the restraint keeps the insert into Projects from happening. Also, I removed the backticks:
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=64f8f0e9cccce2eb2ab77b37fcce54fd
Add COMMIT statements:
create table Project...
commit;
insert into Department values('HR','Chicago');
insert into Department values('Admin','New York');
insert into Project...
commit;
select * from Project;

MySQL: Created Table won't show values?

I'm new to MySQL and I created a table called students with some attributes but when I run Select * From Students it appears as null for each category I'm not sure why
use practice;
create table Students(
sid integer default 1,
sname varchar(50) default 'Joe',
GPA real default 1.7,
dateOfBirth date default (2000-12-12),
primary key(sid)
);
select *
from Students
You can try to insert a value in your table (Insert into Students ('sid', 'sname', 'GPA', 'dateOfBirth') Values ('NULL', 'joe', '1.7', '200-12-12'); this will solve your problem with the SELECT *
I would also add an auto increment on your sid so you can putt the value of sid to null and it will automatically ALTER TABLE Students MODIFY 'sid' INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY;

MySQL INSERT INTO statement giving ERROR CODE 1062

I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below
CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));
CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));
Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams
INSERT INTO Teams (Principal)
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);
In your INSERT statement
INSERT INTO Teams (Principal)
-- ^
you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:
Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL
these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.
You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.
INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...
Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help

Correct SQL Multiply across tables

I am at the end of my rope. I am learning SQL for a class. I have tried to get something akin to the to work, but to no avail. Can someone take a look at it.
Keep in mind that I'm new to this. I am trying to get the code to make subtotal equal the sum of column qty multiplied by the sum of column donutPrice in the donut table. I can't find much except for joins and if I do that, I can't use the join as a value.
The ultimate goal is to make it kinda automated.
CREATE TABLE donut
(
donutID int(50) not null auto_increment primary key,
donutName varchar(50) not null,
donutDesc varchar(200),
donutPrice dec(8,2)
);
CREATE TABLE customer
(
customerID int(50) not null auto_increment primary key,
fname char(50) not null,
lname char(50) not null,
address varchar(50) not null,
apartment varchar(10),
city char(50) not null,
state char(2) not null,
zip dec(5) not null,
homeph varchar(10),
mobileph varchar(10),
otherph varchar(10)
);
CREATE TABLE invoice
(
orderID int(50) not null auto_increment primary key,
notes varchar(50) not null,
orderdate date not null,
customerID int(50) not null default 1,
foreign key (customerID) references customer(customerID)
);
CREATE TABLE invoice_line_item
(
donutID int(50) not null,
orderID int(50) not null,
qty dec not null,
subtotal dec(10,2),
subtotal= sum('qty'*'donutPrice') FROM (invoice_line_item, donut),
primary key (donutID, orderID),
foreign key(donutID) references donut(donutID),
foreign key(orderID) references invoice(orderID)
);
ALTER TABLE donut AUTO_INCREMENT=1;
ALTER TABLE customer AUTO_INCREMENT=1001;
ALTER TABLE invoice AUTO_INCREMENT=500;
I guess you want a result looking like this:
OrderID subtotal
1 12.50
2 15.00
27.50
You get that with a query like this:
SELECT invoice.orderID, SUM(invoice_line_item.qty * donut.donutPrice) subtotal
FROM invoice
JOIN invoice_line_item ON invoice.orderID = invoice_line_item.orderID
JOIN donut ON invoice_line_item.donutID = donut.donutID
GROUP BY invoice.orderID WITH ROLLUP
Did you cover entity-relationship data in your class? Your entities are invoice, invoice_line_item, and donut (and your other tables). The relationships between them appear in the ON clauses of the JOIN operations.
Start with a query, and get it working. Then you can create a view ... which is nothing more or less than an encapsulated query.

Selecting data from other table using a foreign key?

So I'm trying to get the clan tag from ranks table for a specific member_id. But I don't understand how to do that. I used INNER JOIN commands but nothing works.
CREATE TABLE ranks(
rank VARCHAR(8),
tag VARCHAR(18) NOT NULL,
PRIMARY KEY(rank)
);
CREATE TABLE member(
member_id INT UNSIGNED AUTO_INCREMENT,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15),
ign VARCHAR(20) NOT NULL,
rank VARCHAR(8) NOT NULL,
joined_date DATE NOT NULL,
dob DATE,
sex ENUM('M','F') NOT NULL,
country VARCHAR(3) NOT NULL,
PRIMARY KEY(member_id),
FOREIGN KEY(rank) REFERENCES ranks(rank)
);
INSERT INTO ranks VALUES
('Founder','|NoMercy|King'),
('Admin','^1|NoMercy|^7'),
('TmpAdmin','^5|NoMercy|^7'),
('Pro','^1|NoMercy PRO|^7'),
('Member','^4|NoMercy|^7'),
('Banned','BANNED');
INSERT INTO member VALUES
(NULL,'Reznov','NULL','REZ','Member','2017/12/22','1954/02/28','M','RUS'),
(NULL,'Amanda','NULL','S3XXY|G!RL|','Pro','2018/01/05','1992/01/25','F','USA'),
(NULL,'Elmasri','Navathe','INDIAN_Noob _XOX','TmpAdmin','2018/04/02','1960/08/25','M','IND'),
(NULL,'Tony','Silva','Cool KiD','Member','2018/04/26','1988/02/22','M','BR'),
(NULL,'Hashan','NULL','Big Papa','Member','2018/05/08','1996/06/12','M','NZ'),
(NULL,'Emma','Watson','EmmaXXX','Banned','2018/06/10','1985/05/22','F','UK');
I need to output the correct tag for specific member. It should only display the tag.
Please help!
below query should be worked
SELECT tag FROM ranks INNER JOIN member ON ranks.rank=member.rank
where member_id=1;
http://www.sqlfiddle.com/#!9/e90392/1
tag
^4|NoMercy|^7