2.mysql_脚本操作

1. MySQL_脚本操作

/*这是sql脚本*/

/*删除库*/
drop database if exists test;

/*删除表*/
drop table if exists test.testTbale;

/*如果不存在则创建一个名为studentInfo的数据库*/
create database if not exists studentInfo;

/*进入创建的数据库*/
use studentInfo;

/*如果不存在则创建一个名为sutdent的表好像可以这样 create table sutdentInfo.`student` ();*/
create table if not exists `student` (
	`studentNo` INT(4) not null comment '学号' primary key,
	`loginPass` varchar(20) not null comment 'password',
	`studentName` varchar(20) not null comment 'sutdent name',
	`sex` char(3) default 'man' not null comment 'sex',
	`gradeId` int(4) unsigned comment 'grade level',
	`phone` int(11) not null comment 'person phone number',
	`address` varchar(255) default 'null address',
	`identityCard` varchar(18) unique key comment 'idCard'
	)engine=InnoDB comment="学生表";

/*
这是创建好之后的结果
mysql> desc student ;
+--------------+--------------+------+-----+--------------+-------+
| Field        | Type         | Null | Key | Default      | Extra |
+--------------+--------------+------+-----+--------------+-------+
| studentNo    | int          | NO   | PRI | NULL         |       |
| loginPass    | varchar(20)  | NO   |     | NULL         |       |
| studentName  | varchar(20)  | NO   |     | NULL         |       |
| sex          | char(3)      | NO   |     | man          |       |
| gradeId      | int unsigned | YES  |     | NULL         |       |
| phone        | int          | NO   |     | NULL         |       |
| address      | varchar(255) | YES  |     | null address |       |
| identityCard | varchar(18)  | YES  | UNI | NULL         |       |
+--------------+--------------+------+-----+--------------+-------+
8 rows in set (0.00 sec)
*/

/*修改表名*/

/*如何test表存在则删除*/
drop table if exists `test`;
/*创建一个表*/
create table if not exists `demo2`(
	`id` int(10) not null ,
	`name` varchar(20) not null
	);

/*修改表名*/
alter table `demo1` rename `newDemoOne`;

/*添加字段
 语法: alter table 表名 add 字段名 数据类型 [属性];
 */
alter table newDemoOne add `password` varchar(32) not null;

/*修改字段*/
ALTER TABLE newDemoOne CHANGE `username` `user` CHAR(10) NOT NULL;

/*删除字段*/
alter table newDemoOne drop `password` ;

/*添加主键约束
 语法: ALTER TABLE table_name ADD CONSTRAINT 主键名 PRIMARY KEY table_name(主键字段)
ALTER TABLE `newDemoOne` ADD CONSTRAINT `id` PRIMARY KEY `newDemoOne`(`id`);
*/
ALTER TABLE `demo2` ADD CONSTRAINT `id` PRIMARY KEY `demo2`(`id`);

/*添加外键约束
https://www.php.cn/mysql-tutorials-486598.html
 这里外键约束两者之间的数据类型必须对应;
 语法: ALTER TABLE <数据表名> ADD CONSTRAINT <外键名> FOREIGN KEY(<列名>) REFERENCES <主表名> (<列名>);
*/
alter table demo1 add constraint name foreign key (`id`) references demo2 (`id`);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值