zoukankan      html  css  js  c++  java
  • 创建数据库,创建数据库表,例子。MySQL语句

    1.创建数据库:    创建的代码:create  数据库的代码:database   数据库表名:随便起,只要自己记住就行。test

    create database test;

    2.删除数据库: 删除的代码:drop  数据库代码:database  要删除哪一个数据库:数据库名:test

    drop database test;

    3.创建表:   数据库建好后该往里创建表了;例下  创建: create   表的代码: table   表名:随便取   ceshi   

    create table class
    (
        code varchar(20) primary key,
        name varchar(20) not null
    );
    create table ceshi
    (
        ids int auto_increment primary key,
        uid varchar(20),
        name varchar(20),
        class varchar(20),
        foreign key (class)  references class(code) 
    );

     

    注:自增长代码代表:auto_increment

      主建的代码代表:primary key

      外键的代码代表公式:foreign key (列名)  references  主表名 (列名)

          fornign key+(列名)  代表给哪一个加外键 references 要引用哪个表里的列

          是否为空: 不为空的代码:not null

     

    4.删除:      删除代码的代表:drop  删除的是表: table  要删的那个表名:ceshi

    drop table ceshi;

     

    代码写创建数据库是注意:

    1.类型包含长度的,在类型后面加(括号),括号里面写长度

    2.上一列写完加逗号

    3.最后一列不要写逗号

    4.在每一条SQL语句写完之后要加分号;

    5.如果有外键关系,先创建主表

     

    例子:

    创建表:
    create table class
    (
        code varchar(20) primary key,
        name varchar(20)
    );
    create table student
    (
        code varchar(20) primary key,
        name varchar(20),
        sex bit,
        age int,
        class varchar(20),
        foreign key (class) references class(code)
    );
    create table kecheng
    (
        code varchar(20) primary key,
        name varchar(20)
    );
    create table teacher 
    (
        code varchar(20) primary key,
        name varchar(20)
    );
    create table chengji
    (    
        ids int auto_increment primary key,
        scode varchar(20),
        kcode varchar(20),
        degree float,
        foreign key (scode) references student(code),
        foreign key (kcode) references kecheng(code)
    );
    create table tkecheng
    (
        ids int auto_increment primary key,
        tcode varchar(20),
        kcode varchar(20),
        foreign key (kcode) references kecheng(code),
        foreign key (tcode) references teacher(code)
    );

     

     

     

     

     

  • 相关阅读:
    TcpUDP通讯
    hashlib模块和logging模块
    Windows之系统自带截屏快捷键
    Windows之文件夹中打开PowerShell
    scrapy之管道
    爬虫之通讯协议
    爬虫
    jupyter使用
    MongoDB之$
    MongoDB之修改器
  • 原文地址:https://www.cnblogs.com/zc290987034/p/5966192.html
Copyright © 2011-2022 走看看