mysql的表和约束操作

2018-06-23 13:28:46来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

在创建表是默认为加上数据引擎和字符集,如创建一个student表,代码如下:

create table students(id int unsigned zerofill auto_increment primary key,
		      name varchar(20) not null,  --不允许字段为null
		      sex char(1)
		      );


-----------------------------自动加上数据引擎和字符集------------------------
create table students(id int unsigned zerofill auto_increment primary key,
            name varchar(20) not null,
            sex char(1)
)engines=innodb default charset=utf8; 
  • 插入数据(insert和replace)

1.insert命令直接在表的插入一条记录。

insert into students(sname,sex) values('orna','男');
insert into students(sname,sex) values('lisi','男');
insert into students(sname,sex) values('wangwu','男');

mysql> select  * from students;
+------------+--------+------+------+
| id         | sname  | sex  | tid  |
+------------+--------+------+------+
| 0000000001 | orna   | 男   | NULL |
| 0000000002 | lisi   | 男   | NULL |
| 0000000003 | wangwu | 男   | NULL |
+------------+--------+------+------+
3 rows in set (0.00 sec)

  2.replace命令在插入记录时判断主键是否相同,相同就修改,否则插入一条新记录。

replace into students(id,sname,sex) values(2,'luscy','女');
replace into students(id,sname,sex) values(20,'zhangsan','男');


mysql> select  * from students;
+------------+----------+------+------+
| id         | sname    | sex  | tid  |
+------------+----------+------+------+
| 0000000001 | orna     | 男   | NULL |
| 0000000002 | luscy    | 女   | NULL |
| 0000000003 | wangwu   | 男   | NULL |
| 0000000020 | zhangsan | 男   | NULL |
+------------+----------+------+------+
4 rows in set (0.00 sec)

  

  • 用select 结果集创建一个表
mysql> select * from students;
+------------+-------+------+
| id         | name  | sex  |
+------------+-------+------+
| 0000000001 | luscy | 女   |
| 0000000002 | lisi  | 男   |
| 0000000003 | orna  | 男   |
+------------+-------+------+
3 rows in set (0.00 sec)

mysql> create table stu select * from students;
Query OK, 3 rows affected (0.33 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from stu;
+------------+-------+------+
| id         | name  | sex  |
+------------+-------+------+
| 0000000001 | luscy | 女   |
| 0000000002 | lisi  | 男   |
| 0000000003 | orna  | 男   |
+------------+-------+------+
3 rows in set (0.00 sec)
  • 修改表名

有两种方法可以修改表名。

mysql> rename table stu to st;
Query OK, 0 rows affected (0.17 sec)

mysql> alter table st rename to s;
Query OK, 0 rows affected (0.23 sec)
  • 删除表

drop table 表名;

drop table 表1,表2,表3.............. 表示可以同时删除多少个表。

  • 修改表的列名称

在修改字段需要注意几点:

  1. 修改字段宽度只能改大,不能改小;
  2. 修改字段类型时,要兼容;
mysql> alter table s change name sname varchar(30);
Query OK, 3 rows affected (1.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from s;
+------------+-------+------+
| id         | sname | sex  |
+------------+-------+------+
| 0000000001 | luscy | 女   |
| 0000000002 | lisi  | 男   |
| 0000000003 | orna  | 男   |
+------------+-------+------+
3 rows in set (0.01 sec)

  

  • 限定字段的取值范围
create table students(id int unsigned zerofill auto_increment primary key,
		      name varchar(20) not null,
		      sex enum('男','女')   --指定取值范围。enum和set关键字同意
		      );   
insert into students(name,sex) values('orna','男');  --插入数据成功


------------------------------------插入数据时报错---------------------------------------
mysql> insert into students(name,sex) values('orna','未知');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql> select * from students;
+------------+------+------+
| id         | name | sex  |
+------------+------+------+
| 0000000001 | orna | 男   |
+------------+------+------+
1 row in set (0.00 sec)
  • 主建、外建和唯一

一个表只能有一个主键,但可以用多个字段建立一个组合主键。可以有多个外键和唯一约束。

  1. 增加主键
创建主键的两种方法:
--指定主键名 alter table s add constraint pk primary key(id); --不指定主键名 alter table s add constraint primary key(id);

  2.删除主键

mysql> desc s;
+-------+---------------------------+------+-----+------------+-------+
| Field | Type                      | Null | Key | Default    | Extra |
+-------+---------------------------+------+-----+------------+-------+
| id    | int(10) unsigned zerofill | NO   | PRI | 0000000000 |       |
| sname | varchar(30)               | YES  |     | NULL       |       |
| sex   | set('男','女')            | YES  |     | NULL       |       |
+-------+---------------------------+------+-----+------------+-------+
3 rows in set (0.00 sec)

mysql> alter table s drop primary key;
Query OK, 3 rows affected (0.82 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> desc s;
+-------+---------------------------+------+-----+------------+-------+
| Field | Type                      | Null | Key | Default    | Extra |
+-------+---------------------------+------+-----+------------+-------+
| id    | int(10) unsigned zerofill | NO   |     | 0000000000 |       |
| sname | varchar(30)               | YES  |     | NULL       |       |
| sex   | set('男','女')            | YES  |     | NULL       |       |
+-------+---------------------------+------+-----+------------+-------+
3 rows in set (0.00 sec)

  3.增加字段

在增加字段时,可以用first和after关键字,但没有before关键字。

mysql> alter table students add address varchar(50) not null after sex;
Query OK, 0 rows affected (0.49 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc students;
+---------+---------------------------+------+-----+---------+----------------+
| Field   | Type                      | Null | Key | Default | Extra          |
+---------+---------------------------+------+-----+---------+----------------+
| id      | int(10) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20)               | NO   |     | NULL    |                |
| sex     | set('男','女')            | YES  |     | NULL    |                |
| address | varchar(50)               | NO   |     | NULL    |                |
+---------+---------------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

  4.删除字段

mysql> select * from s;
+------------+-------+------+
| id         | sname | sex  |
+------------+-------+------+
| 0000000001 | luscy | 女   |
| 0000000002 | lisi  | 男   |
| 0000000003 | orna  | 男   |
+------------+-------+------+
3 rows in set (0.00 sec)

mysql> alter table s drop column sex;
Query OK, 0 rows affected (0.63 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc s;
+-------+---------------------------+------+-----+------------+-------+
| Field | Type                      | Null | Key | Default    | Extra |
+-------+---------------------------+------+-----+------------+-------+
| id    | int(10) unsigned zerofill | NO   |     | 0000000000 |       |
| sname | varchar(30)               | YES  |     | NULL       |       |
+-------+---------------------------+------+-----+------------+-------+
2 rows in set (0.00 sec)

  2.增加唯一约束

--创建表时增加唯一约束。
create table students(id int unsigned zerofill auto_increment primary key,
		      sname varchar(20) not null unique,
		      sex set('男','女'),
		      tid int unsigned
		      )engine=innodb default charset=utf8;


--创建表后,再增加唯一约束。
alter table students add constraint uk unique(sname);	


mysql> desc students;
+-------+---------------------------+------+-----+---------+----------------+
| Field | Type                      | Null | Key | Default | Extra          |
+-------+---------------------------+------+-----+---------+----------------+
| id    | int(10) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| sname | varchar(20)               | NO   | UNI | NULL    |                |
| sex   | set('男','女')            | YES  |     | NULL    |                |
| tid   | int(10) unsigned          | YES  |     | NULL    |                |
+-------+---------------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

--删除唯一约束。

   mysql> alter table students drop index uk;
   Query OK, 0 rows affected (0.26 sec)
   Records: 0 Duplicates: 0 Warnings: 0

  3.增加外键约束

alter table students add constraint fk foreign key(tid) references teachers(id);
--alter table students add constraint fk foreign key(tid) references teachers(id) on delete set null;表示删除外键引用的记录时把当前记录的外键值修改成空。
--alter table students add constraint fk foreign key(tid) references teachers(id) on delete cascade;表示删除外键引用的记录时把当前记录同时删除。
--alter table students add constraint fk foreign key(tid) references teachers(id) on delete set null on update cascade;表示删除外键引用的记录时把当前记录的外键值修
--改成空,或者修改引用记录同时修改外键引用字段。 mysql> desc students; +-------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+---------+----------------+ | id | int(10) unsigned zerofill | NO | PRI | NULL | auto_increment | | sname | varchar(20) | NO | | NULL | | | sex | set('男','女') | YES | | NULL | | | tid | int(10) unsigned | YES | MUL | NULL | | +-------+---------------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)

  4.删除外键约束

alter table students drop foreign key fk;
alter table students drop index fk;

  5.创建索引

mysql> create index sname on students(sname desc);
Query OK, 0 rows affected (0.27 sec)
Records: 0  Duplicates: 0  Warnings: 0
--alter table students add index(sname desc);

  6.删除索引

alter table students drop index sname;
--drop index sname on students;

  

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:CentOS7安装mysql后无法启动服务,提示Unit not found

下一篇:pt-align