码迷,mamicode.com
首页 > 数据库 > 详细

sql总结4

时间:2016-07-01 06:34:00      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

CREATE TABLE tab2 AS (SELECT * FROM tab1)
这种做法表的存储引擎也会采用服务器默认的存储引擎而不是源表的存储引擎,此种复制方法把表的内容也一起复制过来了。

CREATE TALBE tab2 ENGINE=MYISAM, CHARSET=‘UTF8‘ AS (SELECT * FROM tab1)
可以自己指定存储引擎和字符集,弥补方法一的不足

CREATE TABLE tab2 LIKE tab1
使用和tab1表相同的结构来创建一个新表,列名、数据类型、空指和索引也将复制,但是表的内容不会被复制。外键和专用的权限也没有被复制。

MySQL复制表结构及数据到新表
CREATE TABLE tab_new SELECT * FROM tab_old

复制旧表的数据到新表(假设两个表结构一样)
INSERT INTO tab1 SELECT * FROM tab2

复制旧表的数据到新表(假设两个表结构不一样)
INSERT INTO tab1(字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM tab2

更改表名
ALTER TABLE employee RENAME TO staff

更改列类型
ALTER TABLE employee MODIFY COLUMN truename VARCHAR(10) NOT NULL DEFAULT ‘‘

更改列名
ALTER TABLE employee CHANGE COLUMN truename employeename VARCHAR(10) NOT NULL DEFAULT ‘‘

添加默认值
ALTER TABLE employee ALTER COLUMN truename SET DEFAULT ‘‘

删除默认值
ALTER TABLE employee ALTER COLUMN truename DEOP DEFAULT


mysql修改表
表的结构如下:

mysql> show create table person;
| person | CREATE TABLE `person` (
`number` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`birthday` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
删除列:

ALTER TABLE person DROP COLUMN birthday;
添加列:

ALTER TABLE person ADD COLUMN birthday datetime;
修改列,把number修改为bigint:

ALTER TABLE person MODIFY number BIGINT NOT NULL;
或者是把number修改为id,类型为bigint:

ALTER TABLE person CHANGE number id BIGINT;

添加主键:

ALTER TABLE person ADD PRIMARY KEY (id);
删除主键:

ALTER TABLE person DROP PRIMARY KEY;
添加唯一索引:

ALTER TABLE person ADD UNIQUE name_unique_index (`name`);
为name这一列创建了唯一索引,索引的名字是name_unique_index.

 

添加普通索引:

ALTER TABLE person ADD INDEX birthday_index (`birthday`);

删除索引:

ALTER TABLE person DROP INDEX birthday_index;
ALTER TABLE person DROP INDEX name_unique_index;

禁用非唯一索引

ALTER TABLE person DISABLE KEYS;
ALTER TABLE...DISABLE KEYS让MySQL停止更新MyISAM表中的非唯一索引。

激活非唯一索引

ALTER TABLE person ENABLE KEYS;
ALTER TABLE ... ENABLE KEYS重新创建丢失的索引。

 

把表默认的字符集和所有字符列(CHAR, VARCHAR, TEXT)改为新的字符集:

ALTER TABLE person CONVERT TO CHARACTER SET utf8;
修改表某一列的编码

ALTER TABLE person CHANGE name name varchar(255) CHARACTER SET utf8;
仅仅改变一个表的默认字符集

ALTER TABLE person DEFAULT CHARACTER SET utf8;
修改表名

RENAME TABLE person TO person_other;
移动表到其他数据库

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

 


在mysql中我们对数据表字段的修改命令只要使用alter就可以了,下面我来给大家详细介绍mysql中修改表字段名/字段长度/字段类型等等一些方法介绍,有需要了解的朋友可参考。

先来看看常用的方法
MySql的简单语法,常用,却不容易记住。当然,这些Sql语法在各数据库中基本通用。下面列出:
1.增加一个字段
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;    //增加一个字段,默认不能为空
2.删除一个字段
alter table user DROP COLUMN new2;                //删除一个字段
3.修改一个字段
alter table user MODIFY new1 VARCHAR(10);            //修改一个字段的类型
alter table user CHANGE new1 new4 int;              //修改一个字段的名称,此时一定要重新

//主键
alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
//增加一个新列
alter table t2 add d timestamp;
alter table infos add ex tinyint not null default ‘0′;
//删除列
alter table t2 drop column c;
//重命名列
alter table t1 change a b integer;
//改变列的类型
alter table t1 change b b bigint not null;
alter table infos change list list tinyint not null default ‘0′;
//重命名表
alter table t1 rename t2;
加索引
mysql> alter table tablename change depno depno int(5) not null;
mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);
mysql> alter table tablename add index emp_name (name);
加主关键字的索引
mysql> alter table tablename add primary key(id);
加唯一限制条件的索引
mysql> alter table tablename add unique emp_name2(cardnumber);
删除某个索引
mysql>alter table tablename drop index emp_name;
增加字段:
mysql> ALTER TABLE table_name ADD field_name field_type;
修改原字段名称及类型:
mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
删除字段:
mysql> ALTER TABLE table_name DROP field_name;
mysql修改字段长度
alter table 表名 modify column 字段名 类型;
例如
数据库中user表 name字段是varchar(30)
可以用
alter table user modify column name varchar(50) ;

 

 

 

 


mysql更改表结构:添加、删除、修改字段、调整字段顺序
mysqltablenulluserlist
添加字段:

alter table `user_movement_log`
Add column GatewayId int not null default 0 AFTER `Regionid` (在哪个字段后面添加)

删除字段:

alter table `user_movement_log` drop column Gatewayid

调整字段顺序:

ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID

//主键

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);

//增加一个新列

alter table t2 add d timestamp;
alter table infos add ex tinyint not null default ‘0‘;

//删除列

alter table t2 drop column c;

//重命名列

alter table t1 change a b integer;

//改变列的类型

alter table t1 change b b bigint not null;
alter table infos change list list tinyint not null default ‘0‘;

//重命名表

alter table t1 rename t2;

加索引

mysql> alter table tablename change depno depno int(5) not null;
mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);
mysql> alter table tablename add index emp_name (name);

加主关键字的索引

mysql> alter table tablename add primary key(id);

加唯一限制条件的索引

mysql> alter table tablename add unique emp_name2(cardnumber);

删除某个索引

mysql>alter table tablename drop index emp_name;

修改表:

增加字段:

mysql> ALTER TABLE table_name ADD field_name field_type;

修改原字段名称及类型:

mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

删除字段:

mysql> ALTER TABLE table_name DROP field_name;

 

 

 


 

 


MySql表结构修改详解

修改表的语法
=========================
增加列[add 列名]
=========================
①alter table 表名 add 列名 列类型 列参数【加的列在表的最后面】
例:alter table test add username char(20) not null default ‘‘;
alter table test add birth date not null default ‘0000-00-00‘;

②alter table 表名 add 列名 列类型 列参数 after 某列【把新列加在某列后面】
例:alter table test add gender char(1) not null default ‘‘ after username;

③alter table 表名 add 列名 列类型 列参数 first【把新列加在最前面】
例:alter table test add pid int not null default 0 first;

=========================
删除列[drop 列名]
=========================
①alter table 表名 drop 列名
例:alter table test drop pid;

=========================
修改列[modife 列名]
=========================
①alter table 表名 modify 列名 新类型 新参数【修改列类型】
例:alter table test modify gender char(4) not null default ‘‘;
②alter table 表名 change 旧列名 新列名 新类型 新参数【修改列名和列类型】
例:alter table test change pid uid int unsigned not null default 0;

=========================
查询列
=========================
①desc 表名【查询所有列】
例: desc test;
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId | int(11) | NO | PRI | | |
| dName | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

②show columns from 表名【效果和desc一样】
mysql> show columns from department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId | int(11) | NO | PRI | | |
| dName | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

③show create table 表名【查看表的创建代码】
mysql> show create table department;
CREATE TABLE `department` (
`dId` int(11) NOT NULL,
`dName` varchar(32) DEFAULT NULL,
PRIMARY KEY (`dId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 


总结一下对mysql表结构的修改操作,包括对字段及索引的修改操作: 添加字段: alter table `user_movement_log` Add column GatewayId int not null default 0 AFTER `Regionid` (在哪个字段后面添加) 删除字段: alter table `user_movement_log` drop column Gatewayid 调整字段顺序: ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID //主键 alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id); //增加一个新列 alter table t2 add newfield timestamp; (简单语法,只指定新添字段newfield 类型为timestamp) alter table infos add newfield tinyint not null default ‘0‘; (同时指定字段null属性、默认值) //删除列 alter table t2 drop column c; (删除列c) //重命名列 alter table t1 change a b integer; (把列a改名为b,注意要指定列类型) //改变列的类型 alter table t1 change b b bigint not null; (把列a改名为b,指定列类型及null属性) alter table infos change list list tinyint not null default ‘0‘; //重命名表 alter table t1 rename t2; 加索引 mysql> alter table tablename change depno depno int(5) not null; mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]); mysql> alter table tablename add index emp_name (name); 加主关键字的索引 mysql> alter table tablename add primary key(id); 加唯一限制条件的索引 mysql> alter table tablename add unique emp_name2(cardnumber); 删除某个索引 mysql>alter table tablename drop index emp_name; 修改表: 增加字段: mysql> ALTER TABLE table_name ADD field_name field_type; 修改原字段名称及类型: mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type; 删除字段: mysql> ALTER TABLE table_name DROP field_name;


ORDER BY _column1, _column2; /* _column1升序,_column2升序 */
ORDER BY _column1, _column2 DESC; /* _column1升序,_column2降序 */
ORDER BY _column1 DESC, _column2 ; /* _column1降序,_column2升序 */
ORDER BY _column1 DESC, _column2 DESC; /* _column1降序,_column2降序 */
用 DESC 表示按倒序排序(即:从大到小排序)
用 ACS 表示按正序排序(即:从小到大排序)

 

order应该是以汉字的 ASCII 码排序,下面是按照汉字拼音排序
select * from corp_data where Chengshi like "图木舒克" order by convert(name using gbk);


#select count(*) as count from corp_data where Chengshi like "图木舒克";
select * from corp_data where Chengshi like "图木舒克" order by convert(name using gbk);
#delete from corp_data where Chengshi like "图木舒克";
#desc corp_data;
#alter table corp_data modify column hangye varchar(100) ;
#alter table corp_data modify column jianjie varchar(10000) ;

 

 

 

 

 

mysql如何查询最新的5条数据
select * from tb_content where id=(select msx(id) from tb_content) limit 0,5;

jdbcTemplate.execute("CREATE
建数据库不合适,要手动drop database ,再create database.


查总数:
select count(*) as count from tb_content;
select count(id) as count from tb_content;
navicat->分析表->取得表的总数

select * from corp_data where Chengshi like "上海";
select * from corp_data where Shengzhixiashi like "河南";

select name ,count(distinct(url)) from corp_data group by  name;
select Chengshi ,count(distinct(url)) from corp_data group by  Chengshi;
select Chengshi ,count(*) from corp_data group by  Chengshi;
select count(*) from corp_data where Chengshi like "北京";
select * from corp_data where Chengshi like "北京";
select name ,url  from corp_data limit 1;
select *  from corp_data limit 1;

select *  from corp_data limit 10;

select count(*) as count from corp_data limit 10;
select count(distinct(name)) as count from corp_data;

 varchar(N),  这里的N是指字符数,并不是字节数.占用的字节数与编码有关
utf-8, 一个汉字3字节 英文字母1字节

create user ‘xxxx‘@‘localhost‘ indentified by ‘123456‘;
grant all privileges on *.* to xxxx@localhost identified by ‘123456‘;




sql总结4

标签:

原文地址:http://www.cnblogs.com/destim/p/4761579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!