How do I auto create student id number in mysql? - mysql

I'm very new to MySQL. I created a table for student.
CREATE TABLE student(
studentId int(10) NOT NULL,
firstName VARCHAR(100) NOT NULL,
lastName VARCHAR(100) NOT NULL,
birthday VARCHAR(12) NOT NULL,
gender VARCHAR(7),
course_name VARCHAR(150) NOT NULL,
PRIMARY KEY(studentId )
);
I thought it would be nice studentId auto create by the system. And I don't know how to do this. This may be a simple question to you guys. Please help me with this.
Thanks.

The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows:
Ex -
CREATE TABLE animals (
id INT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
When inserting data into table you don't have input data to id.
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
Which returns -
+----+---------+
| id | name |
+----+---------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
+----+---------+
Answer to your question -
CREATE TABLE student(
studentId int(10) NOT NULL AUTO_INCREMENT,
firstName VARCHAR(100) NOT NULL,
lastName VARCHAR(100) NOT NULL,
birthday VARCHAR(12) NOT NULL,
gender VARCHAR(7),
course_name VARCHAR(150) NOT NULL,
PRIMARY KEY(studentId )
);
For more information refer here.

Related

Adding a FOREIGN KEY but says the column does not exists when it does?

I have created two tables
EMPLOYEE
CREATE TABLE employee (
emp_id INT AUTO_INCREMENT NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
PRIMARY KEY (emp_id)
);
TEAM
CREATE TABLE team (
team_id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(20),
manager_id INT (20),
PRIMARY KEY (team_id)
);
I am trying to add a add a foreign key:
ALTER TABLE employee ADD FOREIGN KEY (manager_id) REFERENCES team(manager_id);
It is giving me an error telling me that the column does not exist
ERROR 1072 (42000): Key column 'manager_id' doesn't exist in table
But it does show when I Describe team
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| team_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| manager_id | int(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
Column manager_id does not exists in table employee.
Something like this would be more meaningful:
ALTER TABLE team ADD FOREIGN KEY (manager_id) REFERENCES employee(emp_id);
Possibly, you also build a table to represent the relationship between employees and teams. As of now, nothing in your schema can be used to relate an employee to one or several teams.

SQL how to create a table with a unique key?

Create a courses table with this definition (try using a multi-line SQL statement):
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| id | int(3) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | UNI | NULL | |
| credits | tinyint(2) unsigned | NO | | 1 | |
I keep getting an error when trying to create the table, this is what I have:
CREATE TABLE courses
(
id int(3) unsigned NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL UNIQUE,
credits tinyint(2) unsigned NOT NULL DEFAULT 1;
Error:
Incorrect table definition; there can be only one auto column and it must be defined as a key
Correct SQL sentence:
CREATE TABLE courses (
id int(3) unsigned primary key NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL UNIQUE,
credits tinyint(2) unsigned NOT NULL DEFAULT 1);
Your sentence loses primary key.
Two mistakes:
The auto_increment column must be a primary key in MySQL.
You need to end the SQL sentence with a ).
This SQL works:
CREATE TABLE courses (
id int(3) unsigned primary key NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL UNIQUE,
credits tinyint(2) unsigned NOT NULL DEFAULT 1
);

Why won't my SQL table creation work in Codio?

I am trying to complete my homework but am getting an error message saying that the table I'm creating doesn't match what is expected to be input. Here is what the table is supposed to contain:
+-----------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------------+------+-----+---------+----------------+
| id | int(3) unsigned | NO | PRI | NULL | auto_increment |
| room_num | int(4) unsigned | NO | UNI | NULL | |
| course_id | int(3) unsigned | YES | MUL | NULL | |
+-----------+-----------------+------+-----+---------+----------------+
The code I am entering to try and achieve this is:
CREATE TABLE college.classrooms (
id INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
room_num INT(4) UNSIGNED NOT NULL UNIQUE KEY,
course_id INT(3) UNSIGNED DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (course_id) REFERENCES college.courses(id)
) AUTO_INCREMENT = 1;
My school uses Codio and it auto checks the table to see if it's correct and that is the error message I'm getting:
[Error]: Create a 'classrooms' table with the requested structure. Reset the database and try again
What am I missing from my code to make this work? I've already created the table it is referencing (courses).
Is it probably failing on the FOREIGN KEY REFERENCES context? You are trying to create a table that is EXPECTING the course_id to be found in a secondary table "college.courses". If the college.courses table is not created yet, this table could never reference it.
Make sure the COURSES table is created FIRST
CREATE TABLE classrooms
(
id INT(3) unsigned primary key NOT NULL AUTO_INCREMENT,
room_num INT(4) unsigned NOT NULL UNIQUE,
course_id INT(3) unsigned DEFAULT NULL,
FOREIGN KEY (course_id) REFERENCES college.courses(id)
)
AUTO_INCREMENT = 1;

Need help in merging three tables foreign keys

I have created three tables like this,
1.
CREATE TABLE person (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
age int,
PRIMARY KEY (id)
);
2.
CREATE TABLE address (
id int NOT NULL AUTO_INCREMENT,
city varchar(50) NOT NULL,
post_code int NOT NULL,
person_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (person_id) REFERENCES person(id)
);
3
CREATE TABLE subjects (
id int NOT NULL AUTO_INCREMENT,
subjects_s varchar(50) NOT NULL,
address_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (address_id) REFERENCES address(id)
);
Now in tables i have some informations like this:
person
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | Sohan | 17 |
| 2 | Farhan | 18 |
+----+--------+------+
address
+----+-------+-----------+-----------+
| id | city | post_code | person_id |
+----+-------+-----------+-----------+
| 1 | Tongi | 1711 | 1 |
| 2 | Dhaka | 1230 | 2 |
+----+-------+-----------+-----------+
subjects
+----+--------------------+------------+
| id | subjects_s | address_id |
+----+--------------------+------------+
| 1 | Accounting Finance | 1 |
| 2 | Physics Math | 2 |
+----+--------------------+------------+
Now I want to show all these data together. How can I do this? Please help!
You should be able to use a SQL join statement to combine these.
The syntax is detailed in the MySQL join documentation.
With your tables, your query should look something like this:
SELECT person.*, address.*, subjects.*
FROM person
JOIN address ON person.id = address.person_id
JOIN subjects ON address.id = subjects.address_id
Keep in mind, this example uses an inner join, which may not be the right type of join depending on the data in your tables. I'd recommend reading the documentation I linked above for further guidance.

Column is not autoincrementing in mysql

When I insert a new row the treeId column is always 1.
The treeId column is not included in the insert statement.
What could be causing it to not increment?
My table code is
CREATE TABLE `users` (
`uuid` varchar(36) NOT NULL,
`parentUuid` varchar(36) DEFAULT NULL,
`treePath` text,
`treeId` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(50) NOT NULL,
`lastName` varchar(50) NOT NULL,
`email` varchar(255) NOT NULL,
`salt` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`state` enum('subscribed','registered','banned') NOT NULL,
`dobMonth` int(11) DEFAULT NULL,
`dobYear` int(11) DEFAULT NULL,
`dateSubscribed` datetime DEFAULT NULL,
`dateRegistered` datetime DEFAULT NULL,
`gender` enum('unspecified','male','female') NOT NULL DEFAULT 'unspecified',
`dd` float DEFAULT '0',
`mainRegion` int(11) DEFAULT NULL,
PRIMARY KEY (`uuid`,`treeId`),
KEY `parentid` (`parentUuid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
if you want it to auto-increment don't specify the treeId field in your insert.
Found the answer (Documentation)
For MyISAM and BDB tables you can specify AUTO_INCREMENT on a
secondary column in a multiple-column index. In this case, the
generated value for the AUTO_INCREMENT column is calculated as
MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is
useful when you want to put data into ordered groups.
CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
) ENGINE=MyISAM;
INSERT INTO animals (grp,name) VALUES
('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale'),
('bird','ostrich');
SELECT * FROM animals ORDER BY grp,id;
Which returns:
+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
So because I had a joint key on uuid (which is always unique) and on treeId (which is the auto-increment) then it was creating a new increment group each time.