设为首页 收藏本站
查看: 447|回复: 0

[经验分享] MySQL之表的创建、删除、修改、删除、查看及索引

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-2-13 14:02:26 | 显示全部楼层 |阅读模式
创建数据库
create database|schema [if not exists] db_name [character set= ] [collate= ]
注:schema可以理解为方案或者数据库,与database一个意义

例创建一个students数据库,且默认字符集为‘gbk’、默认排序为'gbk_chinese_ci';
create schema if not exists students character set 'gbk' collate 'gbk_chinese_ci';

删除数据库
drop {database|schema} [if exists] db_name;

创建表的方式
  • 直接创建一个空表
  • 从其他表中查询出数据,并以此创建一个有数据的表
  • 以其他表为模板创建一个空表




create table [if not exists] tb.name (字段名称1 字段定义 约束 索引,字段名称2 字段定义 约束 索引)
例:
mysql> create table tb1 (id int unsigned not null auto_increment primary key,name char(20) not null,age tinyint not null) engine='engine_name';

主键还可以如下单独定义
mysql> create table tb1 (id int unsigned not null auto_increment ,name char(20) not null,age tinyint not null,primary key(id));

也可以将多个字段一起作为主键
mysql> create table tb1 (id int unsigned not null auto_increment ,name char(20) not null,age tinyint not null,primary key(id,name));

注:任何一个auto_increment字段都必须定义为主键

将id定义为主键,name定义为唯一键,age定义为索引
mysql> create table tb1 (id int unsigned not null auto_increment ,name char(20) not null,age tinyint not null,primary key(id),unique key (name) index(age));

mysql> show tables ;
+--------------------+
| Tables_in_students |
+--------------------+
| tb1                |
+--------------------+
1 row in set (0.00 sec)
mysql> select id,name,age from tb1;


键是一种特殊的索引,其数值不能相同,而索引允许出现相同值,称键为约束,属于B+树索引结构
索引类型有两种
  • BTREE索引
  • HASH索引

每个字段都支持B树索引,但不一定支持HASH索引


mysql> create table corses (cid tinyint unsigned not null auto_increment ,course varchar(50) not null,primary key(cid));
Query OK, 0 rows affected (0.15 sec)
mysql> show table status like 'corses'\G
*************************** 1. row ***************************
           Name: corses
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
Auto_increment: 1
    Create_time: 2017-02-12 10:45:11
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
Create_options:
        Comment:
1 row in set (0.00 sec)

注:创建表时如果未指定字符集,排序规则等,则从数据库继承;而数据库创建时指定或者采取默认


向表中插入及查看数据 insert into 和select

例:
mysql> insert into corses (course) values ('kuihuabaodian'),('jiuyingzhenjing'),('rulaishezhang');   ##向corses表的courses字段插入数据
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from corses; #查看
+-----+-----------------+
| cid | course          |
+-----+-----------------+
|   1 | kuihuabaodian   |
|   2 | jiuyingzhenjing |
|   3 | rulaishezhang   |
+-----+-----------------+
3 rows in set (0.00 sec)

show index from tb_name; 显示表的索引
例:
mysql> show index from corses\G
*************************** 1. row ***************************
   Table: corses  表名
   Non_unique: 0  是否为唯一主键;0表示是唯一键,1表示不是唯一键
   Key_name: PRIMARY 键名
   Seq_in_index: 1 这个表的第1个索引,一个表中可以第一多个索引
   Column_name: cid 索引在哪个字段上(cid)
   Collation: A   排序规则
   Cardinality: 3
   Sub_part: NULL 索引长度
   Packed: NULL
   Null:
   Index_type: BTREE 索引类型
   Comment:
Index_comment:
1 row in set (0.00 sec)


从以张表中查找出数据并建立一个新表
mysql> create table testcourses select * from corses where cid<2;
Query OK, 1 row affected (0.12 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from testcourses;
+-----+---------------+
| cid | course        |
+-----+---------------+
|   1 | kuihuabaodian |
+-----+---------------+
1 row in set (0.00 sec)

mysql> create table testcourses select * from corses where cid<3;
ERROR 1050 (42S01): Table 'testcourses' already exists
mysql> desc courses;
ERROR 1146 (42S02): Table 'students.courses' doesn't exist
mysql> desc testcourses;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| cid    | tinyint(3) unsigned | NO   |     | 0       |     
| course | varchar(50)         | NO   |     | NULL    |     
+--------+---------------------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql> desc corses;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra         
+--------+---------------------+------+-----+---------+----------------+
| cid    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| course | varchar(50)         | NO   |     | NULL    |         
+--------+---------------------+------+-----+---------+----------------

用dessc查看两张表的结构,可以看出不一样
当从一张表中复制数据时某些字段会消失

以一个表为模板创建一个结构一样的空表
mysql> create table test like corses;
Query OK, 0 rows affected (0.16 sec)

mysql> desc test;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| cid    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| course | varchar(50)         | NO   |     | NULL    |            
+--------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> desc corses;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| cid    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| course | varchar(50)         | NO   |     | NULL    |              
+--------+---------------------+------+-----+---------+----------------+


desc显示两个表的格式相同
注:
所以要想复制一张表中的数据,最好先依据原表创建一个完全相同的空表,在用insert命令导入原表的数据


修改表
alter table tb_name(添加,删除,修改字段,修改索引,改表名,修改表属性)


mysql> alter table test add unique key(course);
为test表添加一个唯一键(course)

mysql> alter table test change course Course varchar(50) not null;

将course字段(属性)改为Course 且为varchar(50) 不能为空


新增一个日期字段

例:
mysql> alter table test  add starttime date default '2017-2-12';

mysql> desc test;
+-----------+---------------------+------+-----+------------+----------------+
| Field     | Type                | Null | Key | Default    | Extra          |
+-----------+---------------------+------+-----+------------+----------------+
| cid       | tinyint(3) unsigned | NO   | PRI | NULL       | auto_increment |
| Course    | varchar(50)         | NO   | UNI | NULL       |                |
| starttime | date                | YES  |     | 2017-02-12 |                |
+-----------+---------------------+------+-----+------------+----------------

修改表名test为mytest
mysql> alter table test rename mytest;

删除表
mysql> create table hehe (sid int unsigned not null auto_increment primary key,name varchar(30),cid int not null);
Query OK, 0 rows affected (0.09 sec)

mysql> insert into hehe (name,cid) values ('jiamian',2),('zxl',1);

mysql> select * from hehe;
+-----+---------+-----+
| sid | name    | cid |
+-----+---------+-----+
|   1 | jiamian |   2 |
|   2 | zxl     |   1 |
+-----+---------+-----+
2 rows in set (0.00 sec)

mysql> select * from Courses;
ERROR 1146 (42S02): Table 'students.Courses' doesn't exist
mysql> select * from corses;
+-----+-----------------+
| cid | course          |
+-----+-----------------+
|   1 | kuihuabaodian   |
|   2 | jiuyingzhenjing |
|   3 | rulaishezhang   |
+-----+-----------------+
3 rows in set (0.00 sec)

做两表的条件显示
mysql> select name,course from hehe,corses where hehe.cid=corses.cid;
+---------+-----------------+
| name    | course          |
+---------+-----------------+
| zxl     | kuihuabaodian   |
| jiamian | jiuyingzhenjing |
+---------+-----------------+
2 rows in set (0.01 sec)


添加外键约束
  • 外键约束只能添加在支持事物的存储引擎上,且存储引擎要一样
  • 外键约束的关联的两个字段类型要一样


mysql> alter table corses engine=innodb; 修改引擎
mysql> alter table hehe modify cid tinyint unsigned not null; 修改字段类型一样
mysql>alter table hehe add foreign key foreign_cid (cid) references corses (cid);

将hehe表的cid字段与corses表的cid字段关联一起建立一个外键约束,外键名称为foreign_cid

mysql> create table test1 (cid int unsigned not null auto_increment primary key,name varchar(50) not null,sid char not null);
mysql> insert into test1 (cid,name,sid) values (1,'zxl','A'),(2,'jiamian','B'),(3,'fade','C');
mysql> create table test2 (cid int unsigned not null auto_increment primary key,name varchar(50));

mysql> insert into test2 (cid,name) values (1,'hehe'),(2,'haha');
mysql> alter table test1 add foreign key foreign_cid (cid) references courses(cid);



索引:可以创建,查看,删除,不可以修改
create index index_name on tb_name(字段)using BTREE/HASH;
且可以 (字段(length)desc|asc )
length表示索引长度、占的字符数;
asc表示创建索引后按照升序排,desc表示按照降序排

mysql> create index name_on_student on test1(name) using BTREE;
在test1表的name字段创建索引,并且为BTREE索引
mysql> show index from test1\G
*************************** 1. row ***************************
   Table: test1
   Non_unique: 0
   Key_name: PRIMARY
   Seq_in_index: 1
   Column_name: cid
   Collation: A
   Cardinality: 3
   Sub_part: NULL
   Packed: NULL
   Null:
   Index_type: BTREE
   Comment:
Index_comment:
*************************** 2. row ***************************
   Table: test1
   Non_uniqu
   Key_name: name_on_student
   Seq_in_index: 1
   Column_name: name
   Collation: A
   Cardinality: 3
   Sub_part: NULL
   Packed: NULL
   Null:
   Index_type: BTREE
   Comment:
Index_comment

mysql> drop index name_on_student ontest1;
在test1表的name字段创建一个长度为5,且降序排列的BTREE索引
mysql> create index name_on_student on test1(name(5) desc) using BTREE;


mysql> show index from test1\G
*************************** 1. row ***************************
        Table: test1
   Non_unique: 0
   Key_name: PRIMARY
   Seq_in_index: 1
   Column_name: cid
   Collation: A
   Cardinality:
   Sub_part: NULL
   Packed: NULL
   Null:
   Index_type: BTREE
   Comment:
Index_comment:
*************************** 2. row ***************************
        Table: test1
  Non_unique: 1
  Key_name: name_on_student  索引名称
  Seq_in_index: 1         这个表的第一个索引
  Column_name: name       索引所在的字段(name)
  Collation: A
  Cardinality: 3
  Sub_part: 5           索引长度为5
  Packed: NULL
  Null:
  Index_type: BTREE
  Comment:
  Index_comment:








运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-341629-1-1.html 上篇帖子: mysql双主配合keepalived可用简析 下篇帖子: MySQL使用存储过程创建用户
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表