mysql - modify fields

Modify field properties:

copy code
-- modify field properties
-- ALTER TABLE tb_name MODIFY field name field type [integrity constraint]
-- Modify the email field VARCHAR(50) to VARCHAR(200)
-- Note that if there is no integrity constraint when modifying, the original constraint will be lost. If you want to keep the modification, you must bring the integrity constraint
ALTER TABLE user10 MODIFY email VARCHAR(200) NOT NULL DEFAULT '[email protected]';

-- move card to the back of test
ALTER TABLE user10 MODIFY card CHAR(10) AFTER test;

-- Put test first, keeping the original integrity constraints
ALTER TABLE user10 MODIFY test CHAR(32) NOT NULL DEFAULT '123' FIRST;
copy code

Modify field names and properties:

-- Change the test field to test1
-- ALTER TABLE table name CHANGE original field name new field name field type constraints
ALTER TABLE user10 CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123';

 

Add remove defaults:

copy code
-- create new table
CREATE TABLE user11(
id TINYINT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
age TINYINT UNSIGNED
);

-- add default value to age
ALTER TABLE user11 ALTER age SET DEFAUTL 18;
-- add a field
ALTER TABLE user11 ADD email VARCHAR(50);
-- add default value to email
ALTER TABLE user11 ALTER email SET DEFAULT '[email protected]';

-- remove default value
ALTER TABLE user11 ALTER age DROP DEFAULT;
ALTER TABLE user11 ALTER email DROP DEFAULT;
copy code

 

Add primary key:

copy code
-- create a table
CREATE TABLE test12(
id INT
);
-- add primary key
-- ALTER TABLE tb_name ADD [CONSTRAINT [sysmbol]] PRIMARY KEY [index_type] (field name,...)
ALTER TABLE test12 ADD PRIMARY KEY(id);


-- add composite primary key
-- first create a table
CREATE TABLE test13(
id INT,
card CHAR(18),
username VARCHAR(20) NOT NULL
);
-- add composite primary key
ALTER TABLE test13 ADD PRIMARY KEY(id,card);
copy code

Delete the primary key:

-- delete primary key
ALTER TABLE test12 DROP PRIMARY KEY;

-- Add the primary key to test12, the full form
ALTER TABLE test12 ADD CONSTRAINT symbol PRIMARY KEY index_type(id);

When deleting the primary key, there is a situation that needs to be paid attention to. We know that the field with the self-increasing attribute must be the primary key. If the primary key in the table has the self-increasing attribute; then direct deletion will report an error. If you want to delete the primary key, you can first self-increase the property last year, and then delete the primary key

copy code
-- create another table,
CREATE TABLE test14(
id INT UNSIGNED KEY AUTO_INCREMENT
);

-- delete the primary key, this will report an error, because the self-increasing must be the primary key
ALTER TABLE test14 DROP PRIMARY KEY;

-- First use MODIFY to delete the self-increasing attribute, note that MODIFY cannot remove the primary key attribute
ALTER TABLE test14 MODIFY id INT UNSIGNED;
-- delete the primary key again
ALTER TABLE test14 DROP PRIMARY KEY;
copy code

 

 

Unique index:

copy code
-- add a unique constraint
-- ALTER TABLE tb_name ADD [CONSTANT [symbol]] UNIQUE [INDEX | KEY] [index name](field name,...)

-- create test table
CREATE TABLE user12(
id TINYINT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
card CHAR(18) NOT NULL,
test VARCHAR(20) NOT NULL,
test1 CHAR(32) NOT NULL
);

-- username adds a unique constraint, if no index name is specified, the system will create an index with the field name
ALTER TABLE user12 ADD UNIQUE(username);
-- car adds a unique constraint
ALTER TABLE user12 ADD CONSTRAINT symbol UNIQUE KEY uni_card(card);
-- view index
SHOW CREATE TABLE user12;

-- test, test1 add joint unique
ALTER TABLE user12 ADD CONSTRAINT symbol UNIQUE INDEX mulUni_test_test1(test, test1);

-- delete unique
-- ALTER TABLE tb_name DROP {INDEX|KEY} index_name;
-- remove the unique index just added
ALTER TABLE user12 DROP INDEX username;
ALTER TABLE user12 DROP KEY uni_card;
ALTER TABLE user12 DROP KEY mulUni_test_test1;
copy code

 

Modify the storage engine of the table:

-- Modify the storage engine of the table
-- ALTER TABLE tb_name ENGINE=storage engine name
ALTER TABLE user12 ENGINE=MyISAM;
ALTER TABLE user12 ENGINE=INNODB;

 

Modify the auto-increment value:

-- modify the auto-increment value
-- ALTER TABLE tb_name AUTO_INCREMENT=值
ALTER TABLE user12 AUTO_INCREMENT=100;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325595839&siteId=291194637
Recommended