当前位置:Gxlcms > 数据库问题 > mysql数据库及表结构操作

mysql数据库及表结构操作

时间:2021-07-01 10:21:17 帮助过:16人阅读

 

创建库操作
http://www.cnblogs.com/linhaifeng/articles/7211690.html
---------------------------------
1、create database homework; 创建数据库)
2、show create database homework; 查询创建过程
3、drop database homework; 删除数据库
4、alter database homework charset utf8; 设置数据库编码格式
5、use homework;(进入数据库)
6、desc homework(查看表结构)


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
对表的操作:http://www.cnblogs.com/linhaifeng/articles/7232894.html#_label1
创建表语法:create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);
#注意:
1. 在同一张表中,字段名是不能相同
2. 宽度和约束条件可选
3. 字段名和类型是必须的

create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum(‘male‘,‘female‘) not null default ‘male‘, #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
);

mysql>

1、 create table t1 查看建表过程
2、desc t1;查看表结构
3、show tables; 查看该数据库下有哪些表
4、show create table t1\G; 查看详细的建表过程

 

 

=================================================================================================================================================================
表结构操作(建表之后的操作)

增加:alter table 表名 add 字段名 字段类型 NOT NULL/first/(after列名);
删:1、drop table 表名 (删大数据用truncate+表名)
2、alter table 表名 drop 字段名:删除字段名
改:1、alter table 表名 modify(改字段类型) 列名 字段类型 /first/(after列名);
2、alter table 表名 change(改字段名) 列名 字段类型 /first/(after列名);
查:详见以下

http://www.cnblogs.com/linhaifeng/articles/7232894.html#_label2
示例:
1. 修改存储引擎
mysql> alter table service
-> engine=innodb;

2. 添加字段
mysql> alter table student10
-> add name varchar(20) not null,
-> add age int(3) not null default 22;

mysql> alter table student10
-> add stu_num varchar(10) not null after name; //添加name字段之后

mysql> alter table student10
-> add sex enum(‘male‘,‘female‘) default ‘male‘ first; //添加到最前面

3. 删除字段
mysql> alter table student10
-> drop sex;

mysql> alter table service
-> drop mac;

4. 修改字段类型modify
mysql> alter table student10
-> modify age int(3);
mysql> alter table student10
-> modify id int(11) not null primary key auto_increment; //修改为主键

5. 增加约束(针对已有的主键增加auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined

mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

6. 对已经存在的表增加复合主键
mysql> alter table service2
-> add primary key(host_ip,port);

7. 增加主键
mysql> alter table student1
-> modify name varchar(10) not null primary key;

8. 增加主键和自动增长
mysql> alter table student1
-> modify id int not null primary key auto_increment;

9. 删除主键
a. 删除自增约束
mysql> alter table student10 modify id int(11) not null;

b. 删除主键
mysql> alter table student10
-> drop primary key;

八 复制表
复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_service select * from service;


只复制表结构
mysql> select * from service where 1=2; //条件为假,查不到任何记录
Empty set (0.00 sec)
mysql> create table new1_service select * from service where 1=2;

 

 截图:

技术分享图片

 

mysql数据库及表结构操作

标签:表名   表结构   ica   建表   mysql数据库   null   编码格式   ice   ase   

人气教程排行