您好,欢迎访问一九零五行业门户网

MySQL常用的建表等SQL语句写法总结

本文主要介绍了mysql常用的建表、添加字段、修改字段、添加索引sql语句写法,总结分析了mysql建表、编码设置、字段添加、索引操作所涉及的sql语句,需要的朋友可以参考下,希望能帮助到大家。
建表:
drop table if exists bulletin; create table bulletin( id int not null primary key auto_increment, # 主键 uid int(11) not null default 0, # 创建者id context varchar(600) not null default '', # 公告详细内容(300字) begintime dec(20) not null default 0, # 公告开始时间 endtime dec(20) not null default 0, # 公告结束时间 createtime dec(20) not null default 0, # 创建时间 modifytime dec(20) not null default 0 # 修改时间 primary key (`id`), )default charset=utf8 type=innodb;
修改原有字段名称及类型:
alter table bulletin change uid username varchar(50) not null default '';
添加新字段:
alter table bulletin add citycode varchar(6) not null default 0; # 城市代码
1.创建数据库时设置编码
create database test character set utf8;
2.创建表时设置编码
create table test(id int primary key)default charset=utf8;
3.修改数据库编码
alter database test character set utf8;
4.修改表默认编码
alter table test character set utf8;
5.修改字段编码
alter table test modify col_name varchar(50) character set utf8;
添加索引方法
1.添加primary key(主键索引)
mysql>alter table `table_name` add primary key ( `column` )
2.添加unique(唯一索引)
mysql>alter table `table_name` add unique ( `column` )
3.添加index(普通索引)
mysql>alter table `table_name` add index index_name ( `column` )
4.添加fulltext(全文索引)
mysql>alter table `table_name` add fulltext ( `column` )
5.添加多列索引
mysql>alter table `table_name` add index index_name ( `column1`, `column2`, `column3` )
相关推荐:
mysql sql语句注释大全实例分享
laravel如何记录sql语句
自动生成sql语句的方法
以上就是mysql常用的建表等sql语句写法总结的详细内容。
其它类似信息

推荐信息