• 第二章 创建和使用数据库


    1.基本描述

        本章内容主要包括如何创建数据库及如何建表。

    2.基本样例

    CREATE TABLE person
    (    
        person_id SMALLINT UNSIGNED,
        fname VARCHAR(20),
        lname VARCHAR(20),
        gender ENUM('M', 'F'),
        birth_date DATE,
        street VARCHAR(20),
        city VARCHAR(20),
        state VARCHAR(20),
        country VARCHAR(20),
        postal_code VARCHAR(20),
        CONSTRAINT pk_person PRIMARY KEY (person_id)
    );
    
    DESC person;
    
    CREATE TABLE favorite_food
    (
        person_id SMALLINT UNSIGNED,
        food VARCHAR(20),
        CONSTRAINT pk_favorite_food PRIMARY KEY (person_id,food),
        CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id) REFERENCES person (person_id)
    );
    
    DESC favorite_food;
    
    ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
    
    DESC person;
    
    INSERT INTO person (person_id, fname, lname, gender, birth_date)
    VALUES (null, "William", 'Turner', 'M', '1972-05-27');
    
    SELECT person_id, fname, lname, birth_date FROM person; 
    
    SELECT person_id, fname, lname, birth_date FROM person WHERE person_id = 1;
    
    SELECT person_id, fname, lname, birth_date FROM person WHERE lname = 'Turner';
    
    INSERT INTO favorite_food (person_id, food) VALUES (1, 'pizza');
    
    INSERT INTO favorite_food (person_id, food) VALUES (1, 'cookies');
    
    INSERT INTO favorite_food (person_id, food) VALUES (1, 'nachos');
    
    SELECT food FROM favorite_food WHERE person_id = 1 ORDER BY food;
    
    INSERT INTO person (person_id, fname, lname, gender, birth_date, street, city, state, country, postal_code)
    VALUES (null, 'Susan', 'Smith', 'F', '1975-11-02', '23 Maple St.', 'Arlington', 'VA', 'USA', '20220');
    
    SELECT person_id, fname, lname, birth_date FROM person;
    
    UPDATE  person
    SET street = '1225 Tremont St.',
        city = 'Boston',
        state = 'MA',
        country = 'USA',
        postal_code = '02138',
    WHERE person_id = 1;
    
    DELETE FROM person WHERE person_id = 2;
    
    INSERT INTO person (person_id, fname, lname, gender, birth_date) 
    VALUES (1, 'Charles', 'Fulton', 'M', '1968-01-15');
    
    INSERT INTO favorite_food (person_id, food)
    VALUES (999, 'lasagna');
    
    UPDATE person 
    SET gender = 'z'
    WHERE person_id = 1;
    
    UPDATE person
    SET birth_date = 'DEC-21-1980'
    WHERE person_id = 1;
    
    UPDATE person
    SET birth_date = str_to_date('DEC-21-1980', '%b-%d-%Y')
    WHERE person_id = 1;
    
    SHOW TABLES;
    
    DROP TABLE;
    
    DROP TABLE favorite_food;
    DROP TABLE person;
    DESC customer;
    

    3.参考

        SQL学习指南

    4.运行环境

        Linux:mysql -uroot -p;show database;use bank

  • 相关阅读:
    使用 BinToHex() 把 TBytes 转换为十六进制字符串 回复 "梧桐栖凤" 的问题
    ASP.NET中UrlEncode应该用Uri.EscapeDataString()
    抛弃WebService,在.NET4中用 jQuery 调用 WCF
    事实证明Ajax的世界更需要ASP.NET MVC
    tinyMCEPopup.close轻松让IE 9 RC崩溃
    不走寻常路:在WebForm中使用MVC
    关于ASP.NET预编译
    不错的VS2010扩展——JSEnhancements,让js和css也折叠
    VS2010 SP1 Beta与VisualSVN的冲突引起VS2010关闭时重启
    Web应用架构探索笔记 —— 查询
  • 原文地址:https://www.cnblogs.com/LuckPsyduck/p/12608157.html
Copyright © 2020-2023  润新知