zoukankan      html  css  js  c++  java
  • mysql之创建数据库,创建数据表

    写在前面

    项目中用到mysql数据库,之前也没用过mysql,今天就学下mysql的常用的语法,发现跟sql server的语法极其相似。用起来还是蛮简单的。

    一个例子

    1、创建一个名为School的数据库。

    1、创建一个学生信息表:学生id(自增,主键),姓名,年龄,性别,电话,籍贯,入学时间,所属班级id(外键)。

    2、创建一个学生成绩表:成绩id(自增,主键),科目,成绩,学生id(外键),创建时间。

    3、创建一个学生班级表:班级id(主键,自增),班级名称。

    创建表和数据库

    复制代码
    #如果存在数据库School,则删除。否则创建数据库
    drop database if exists `School`;
    #创建数据库
    create database `School`;
    use `School`;
    #如果存在数据表,则删除,否则创建
    drop table if exists `tb_class`;
    #创建一个学生班级表:班级id(主键,自增),班级名称。
    create table `tb_class`
    (
    `id` int(11) not null AUTO_INCREMENT primary key ,
    `Name` varchar(32) not null
    
    );
    Drop table if  exists tb_student;
    #创建一个学生信息表:学生id(自增,主键),姓名,年龄,性别,入学时间,所属班级id(外键)。
    create table `tb_student`
    (
     `id` int(11) not null auto_increment primary key,
     `Name` varchar(32) not null,
     `Age` int default 0,check(`Age`>0 and `Age`<=100),
     `gender` boolean default 0,check(`gender`=0 or `gender`=1),
     `date` datetime default now()
    );
    #创建一个学生成绩表:成绩id(自增,主键),科目,成绩,学生id(外键),创建时间。
    drop table if exists `tb_score`;
    create table `tb_score`
    (`id` int(11) not null AUTO_INCREMENT PRIMARY key,
    `course` varchar(32) not null,
    `Score` float(3,1) not null,
    `stuId` int(11) not null , 
    constraint `FK_Stuid` foreign key(`stuId`) references `tb_student`(`id`)
    );
    复制代码

    查询创建的数据库

    show databases;

    查看表结构

    use school;
    desc tb_student;

    结果

    修改学生信息表的字段date为createdate。

    1 use school;
    2 alter table tb_student change `date` `createdate` datetime;

    在学生信息表姓名之后添加学生电话字段。

    use school;
    alter table tb_student add `phone` varchar(15) after `name`;

    为表tb_student添加字段classid,并设置为外键。

    use school;
    alter table tb_student add classId int(11) not null;
    alter table tb_student add constraint `FK_class_student` foreign key(`classId`) references tb_class(`id`);

    总结

    创建数据库和创建数据表的内容就学到这里,如果用过sql server 这个学起来还是容易上手的。之后将学习数据表中的增删改查。

  • 相关阅读:
    使用FolderBrowserDialog组件选择文件夹
    使用OpenFileDialog组件打开多个文
    使用OpenFileDialog组件打开对话框
    获取弹出对话框的相关返回值
    PAT 甲级 1139 First Contact (30 分)
    PAT 甲级 1139 First Contact (30 分)
    PAT 甲级 1138 Postorder Traversal (25 分)
    PAT 甲级 1138 Postorder Traversal (25 分)
    PAT 甲级 1137 Final Grading (25 分)
    PAT 甲级 1137 Final Grading (25 分)
  • 原文地址:https://www.cnblogs.com/bb3q/p/4539920.html
Copyright © 2011-2022 走看看