Recently in the collation of things about MySQL, to write down some things to record, in order to query and browse later, the following are some of the operational skills.
Add a table field
ALTER TABLE table1 add transactor varchar (ten) not Null;
ALTER TABLE table1 add ID int unsigned not Null auto_increment primary key
Modify the field type of a table and specify empty or non-empty
ALTER TABLE name change field Name field Name field type [whether non-null allowed];
ALTER TABLE name modify field Name field type [allow non-null];
Modify the field name of a table and specify null or non-empty
ALTER TABLE name change field original Name field new Name field type [whether non-null is allowed
Delete a field
ALTER TABLE mytable DROP field name;
Add unique key
ALTER TABLE ' test2 ' ADD UNIQUE (' userid ')
Modify PRIMARY Key
ALTER TABLE ' test2 ' DROP PRIMARY key, ADD PRIMARY key (' ID ')
Add index
ALTER TABLE ' test2 ' ADD INDEX (' id ')
ALTER TABLE ' category ' MODIFY COLUMN ' id ' int (one) not NULL auto_increment First, ADD PRIMARY KEY (' id ');
View field information for a table
DESC table name;
Show columns from table name;
View all information for a table
Show create table table name;
Add a PRIMARY KEY constraint
ALTER TABLE name add constraint primary Key name (Shape: Pk_ table name) primary key table name (primary key field);
ALTER TABLE name add column list type unsigned is null auto_increment primary key;
Add a FOREIGN KEY constraint
ALTER TABLE from TABLE ADD constraint foreign key (shape: Fk_ from Table _ Main Table) foreign key from table (foreign key field) references Main Table (primary key field);
(ALTER TABLE Main Table name Add foreign key (field) references from table name (field) on DELETE cascade)
Add a UNIQUE Constraint
ALTER table name add unique key constraint name (field);
Delete a PRIMARY KEY constraint
ALTER TABLE name drop PRIMARY key;
Delete a FOREIGN KEY constraint
ALTER TABLE name drop FOREIGN key foreign key (case-sensitive);
Modify Table Name
ALTER TABLE T_book Rename to BBB;
The SQL statement block that modifies the primary key is as follows:
DECLARE @defname varchar (100)
DECLARE @cmd varchar (500)
DECLARE @tablename varchar (100)
DECLARE @keyname varchar (100)
Set @tablename = ' TEMP1 '
Set @keyname = ' id '--the key to be placed, delimited
Select @defname = Name
From sysobjects so
JOIN Sysconstraints SC
On so.id = Sc.constid
WHERE object_name (so.parent_obj) = @tablename
and xtype= ' PK '
If @defname is not null
Begin
Select @cmd = ' ALTER TABLE ' + @tablename + ' drop constraint ' + @defname
--print @cmd
EXEC (@cmd)
End
Else
Set @defname = ' pk_ ' [email protected]
Select @cmd = ' ALTER TABLE ' + @tablename + ' ADD constraint ' + @defname + ' PRIMARY KEY CLUSTERED (' [email protected]+ ') '
EXEC (@cmd)
How to take the primary key field name and field type--Get the primary key field name
1:
SELECT Table_name,column_name from INFORMATION_SCHEMA. Key_column_usage
WHERE table_name<> ' dtproperties '
2:
EXEC Sp_pkeys @table_name = ' table name '
3:
Select O.name as table name, c.name as field name, K.colid as field ordinal, K.keyno as index order, t.name as type
From sysindexes i
Join Sysindexkeys k on i.id = k.id and I.indid = K.indid
Join sysobjects o on i.id = o.id
Join syscolumns C on i.id=c.id and k.colid = C.colid
Join Systypes T on C.xusertype=t.xusertype
where O.xtype = ' U ' and o.name= ' table name to query '
and exists (select 1 from sysobjects where xtype = ' PK ' and parent_obj=i.id and name = I.name)
ORDER BY O.name,k.colid
The above is about how to modify the field type of MySQL data table, default values and add new fields.
MySQL add a primary key or change the table's column as the primary key SQL statement