×
Clear all filters including search bar
Valeri Tandilashvili's MySQL Notes
AUTO_INCREMENT to any varchar column first requires the column to be converted into int CREATE TABLE note (
title varchar(50),
description varchar(50),
PRIMARY KEY(title)
);
INSERT INTO `note` (`title`, `description`) VALUES ('first', 'value'), ('second', 'value');
Let's add AUTO_INCREMENT to title column.
We are not able to add AUTO_INCREMENT without changing it's type to int ALTER TABLE note MODIFY title int NOT NULL AUTO_INCREMENT;
Note: In such cases If the table contains rows with string values, (for AUTO INCREMENT field) the string values will be replaced with numbers starting with 1note table without any column to have AUTO_INCREMENT CREATE TABLE note (
id int,
title varchar(50),
description varchar(50),
PRIMARY KEY(id, title)
);After running the query above, we can add AUTO_INCREMENT feature to the existing column id ALTER TABLE note MODIFY id int NOT NULL AUTO_INCREMENT;
If the column is not primary key initially, we can modify the query, not to cause any error when adding AUTO_INCREMENT to itCREATE TABLE note (
id int,
title varchar(50),
description varchar(50)
);Adding AUTO_INCREMENT feature to the column, that is not selected as a primary key ALTER TABLE note MODIFY id int NOT NULL PRIMARY KEY AUTO_INCREMENTAUTO_INCREMENT featureCREATE TABLE note (
id int AUTO_INCREMENT,
note_id int AUTO_INCREMENT,
title varchar(50),
description varchar(50),
PRIMARY KEY(id, note_id)
);The query generates the following error:#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a keyCREATE TABLE note (
id int AUTO_INCREMENT,
note_id int,
title varchar(50),
description varchar(50),
PRIMARY KEY(id, note_id, title)
);AUTO_INCREMENT feature to a column that is not selected as a primary key.
If we want to add the feature to a column, we must make it primary keyCREATE TABLE note (
id int AUTO_INCREMENT,
title varchar(50),
description varchar(50)
);After running the above query, the following error will be generated:#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
The correct syntax of the query after adding primary key to the column id:CREATE TABLE note (
id int AUTO_INCREMENT,
title varchar(50),
description varchar(50),
PRIMARY KEY (id)
);notes if exists.
It will not occur an error if it does not exist, because we are using IF EXISTS keywordDROP TABLE IF EXISTS notes
The same goes when creating a new table.
It will not cause an error if a table with the same name already exists.
If the table does not exist, it will create one with the specified fields:CREATE TABLE IF NOT EXISTS students (
`id` int(11) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL
) ENGINE=InnoDB;st and copies only several fields from students tableCREATE TABLE st AS
SELECT
id,
first_name AS name,
last_name AS surname
FROM studentsUSE university; -- we are looking at the database called: university
SET @grade = (SELECT MAX(points) FROM students);
SELECT @grade AS 'The highest grade'grt and assigns it Hello SET @grt = 'Hello';
SELECT @grt AS greetingSHOW VARIABLES