【数据库-MySql】MYSQL 语句

来源:互联网 发布:黑魂34g内存优化 编辑:程序博客网 时间:2024/06/06 13:06

表格创建示例

查询语句:

CREATE TABLE `CherishTable` (

 `id` int(10) NOT NULL auto_increment,

 `stinyint` tinyint(10) NOT NULL,

 `ssmallint` smallint(10) NOT NULL,

 `smediumint` mediumint(10) NOT NULL,

 `sinteger` integer(10) NOT NULL,

 `sbigint` bigint(10) NOT NULL,

 `sbit` bit(10) NOT NULL,

 `sreal` real NOT NULL,

 `sdouble` double(10,2) default NULL,

 `sfloat` float(10,2) default NULL,

 `sdecimal` decimal(10) NOT NULL,

 `snumeric` numeric(10) NOT NULL,

 `schar` char(10) NOT NULL,

 `svarchar` varchar(10) NOT NULL,

 `sdate` date NOT NULL,

 `stime` time(6) NOT NULL,

 `syear` year(10) NOT NULL, 

 `stimestamp` timestamp(6) NOT NULL,

 `sdatetime` datetime(6) NOT NULL,

 `stinyblob` tinyblob NOT NULL,

 `sblob` blob(10) NOT NULL,

 `smediumblob` mediumblob NOT NULL,

 `slongblob` longblob NOT NULL,

 `stinytext` tinytext NOT NULL,

 `stext` text NOT NULL,

 `smediumtext` mediumtext NOT NULL,

 `slongtext` longtext NOT NULL,

 `senum` enum('cherish', 'hos','lucy') not NULL,

 `sset` set('index','best','hot') NOT NULL,

 `sbinary` binary(10) NOT NULL,

 `svarbinary` varbinary(10) NOT NULL,

 `spoint` point NOT NULL,

 `slinestring` linestring NOT NULL,

 `spolygon` polygon NOT NULL,

 `sgeometry` geometry NOT NULL,

 `smultipoint` multipoint NOT NULL,

 `smultilinestring` multilinestring NOT NULL,

 `smultipolygon` multipolygon NOT NULL,

 `sgeometrycollection` geometrycollection NOT NULL,

 `date` date NOT NULL,

 `type` varchar(100) NOT NULL,

 `sub_type` varchar(100) NOT NULL,

 `domain_name` varchar(128) NOT NULL,

 `url` text NOT NULL,

 `parameters` text NOT NULL,

 `hash` varchar(100) NOT NULL,

 `cherish` tinyint(100) NOT NULL,

 `deal` int(1) NOT NULL,

 `deal_date` date default NULL,

 `remark` text,

 `last_push_time` datetime default NULL,

 `push_times` int(11) default '1',

 `first_set_ok_time` datetime default NULL,

 `last_set_ok_time` datetime default NULL,

 PRIMARY KEY  (`id`),

 UNIQUE KEY `date` (`date`,`hash`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8

查询表的创建信息

 

表达式

show create table表名

例子

show create table cherishtable;

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable1'doesn't exist

 

查询结果

CREATE TABLE `cherishtable` (

 `id` int(10) NOT NULL AUTO_INCREMENT,

 `last_push_time` double DEFAULT NULL,

 `hh` double DEFAULT NULL,

 `nickname` char(20) DEFAULT NULL,

 `dollar` double(20,2) DEFAULT '1.00',

 `hash` varchar(20) NOT NULL,

 PRIMARY KEY (`id`),

 UNIQUE KEY `hash` (`last_push_time`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULTCHARSET=utf8

 

修改表名

表达式

alter table源表名rename to 目标表名

例子

alter table cherishtable rename tocherishtable1;

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable'doesn't exist

创建表

复制表结构

表达式

完全复制

create table 目标表名 like 源表名

无主键

create table目标表名select * from源表名limit 0;  

例子

create table cherishtable1223 likecherishtable;

create table cherishtable12233 select *from cherishtable limit 0;

提示

复制表数据

表达式

无目标表、无主键

create table目标表名select * from源表名;  

无目标表、选择列复制

create table 目标表名 select 列1,列2 ,列3….. from 源表名

有目标表、全部复制复制

INSERT INTO目标表名SELECT * FROM源表名

有目标表、选择列复制

INSERT INTO目标表名(列1, 列2,…….) SELECT列1, 列2,…… FROM源表名

例子

create table cherishtable1223 likecherishtable;

create table cherishtable122333 select *from cherishtable

create table cherishtable12233333 selectid,last_push_time,hh from cherishtable

INSERT INTO cherishtable123 SELECT * FROMcherishtable

提示

 

 

清空表

表达式

TRUNCATE TABLE 表名

例子

TRUNCATE TABLE cherishtable

提示

 

删除表

表达式

连接断开删除

DROP TABLE表名

删除

DROP TABLE IF EXISTS `表名`

例子

DROP TABLE cherishtable

DROP TABLE IF EXISTS `cherishtable`;

提示

 

注释

表达式

alter table表名COMMENT ='注释'

例子

alter table cherishtable ='这就是注释'

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable'doesn't exist

引擎

表达式

alter table表名ENGINE=引擎

例子

alter table cherishtable ENGINE=INNODB;

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable'doesn't exist

 

 

字符集

表达式

alter table表名defaultcharacter set 字符集

例子

alter table cherishtable default characterset utf8;

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable'doesn't exist

 

主键

添加

表达式

Alter table 表名add primary key(列名);

设置主键为自动递增

 

Alter table 表名 change列名 列名 属性(长度) not nullauto_increment;

例子

添加

Alter table cherishtable add primarykey(id);

设置主键属性

Alter table cherishtable change id idint(10) not null auto_increment;

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable'doesn't exist

主键已经存在

[Err] 1068 - Multiple primary key defined

 

删除

表达式

设置主键属性为非自动递增

Alter table 表名change 列名 列名 类型(长度);

alter table表名drop primarykey

例子

设置主键属性为非自动递增

Alter table cherishtable change id idint(10);

删除主键

Alter table cherishtable drop primary key;

提示

表不存在

[Err] 1146 - Table 'anydrill.cherishtable'doesn't exist

主键属性为自动递增无法删除

[Err] 1075 - Incorrect table definition;there can be only one auto column and it must be defined as a key

查询列属性

表达式

desc 表名

例子

desc cherishtable

 

提示

该表不存在

[Err] 1146 - Table 'anydrill.cherishtabl1'doesn't exist

添加列

表达式

alter table 表名 add 列名 类型[1](长度[2],浮点数[3])允许空值[4] 默认值[5]

 

[1]类型决定[2][3]的存在

[2]决定值的长度

[3]决定浮点型数据保留的小数位数

[4]当为default时,允许空值,[5]可以填写默认值;当为not时,不允许空值,[5]不能填写默认值只能为null

[5]默认值

例子

alter table cherishtable add dollarDOUBLE(20,2) DEFAULT '1.0'

alter table cherishtable add dollar1int(20) NOT NULL

提示

该列名已经存在

[Err] 1060 - Duplicate column name 'hash'

删除列

表达式

alter table 表名 drop column 列名

 

列子

alter table cherishtable drop columndollar1

 

 

提示

该列名不存在

[Err] 1091 - Can't DROP 'hash'; check thatcolumn/key exists

 

修改列名

表达式

alter table表名change源列名 目标列名 类型[1]

[1]类型更换时要保证表中数据的长度和格式符合该类型,不然会导致类型转换失败,最好与原列名的属性一致

列子

alter table cherishtable change push_timeshh int;

desc cherishtable

提示

列名不存在

[Err] 1054 - Unknown column 'push_times' in'cherishtable'

数据格式不匹配

[Err] 1064 - You have an error in your SQLsyntax; check the manual that corresponds to your MySQL server version for theright syntax to use near '' at line 1

[Err] 1292 - Incorrect datetime value:'94344' for column 'hhh' at row 1

源数据内容长度超过修改后的数据类型最大长度

[Err] 1264 - Out of range value for column'hhh' at row 1

修改列属性

表达式

alter table表名modify列名 类型(长度,浮点数) 允许空值 默认值 自动递增

列子

alter table cherishtable modify push_timesvarchar(22);

desc cherishtable

 

 

提示

0 0
原创粉丝点击