MySQL Create tables and databases

Source: Internet
Author: User
Tags mysql create

1. Create a Database: Code created: CREATE DATABASE: Database table name: Whatever you want, just remember it. Test

Create DATABASE test;

2. Delete database: Deleted code: Drop DATABASE code: database to delete which database: DB name: Test

drop database test;

3. Create a table: After the database is built, the table is created; example: CREATE TABLE code: Table name: Pick Ceshi

CREATE TABLE Class (    code varchar PRIMARY KEY,    name varchar () not null); CREATE TABLE Ceshi (    IDs int auto_ Increment primary KEY, uid varchar (+), name varchar (+),    class varchar,    foreign key (class)  References Class (code));

Note: Self-growth Code rep: auto_increment

The main building code represents: Primary Key

The code for the foreign key represents the formula: foreign key (column name) references Main Table name (column name)

Fornign key+ (column name) represents the column to which the add foreign key references to reference the table

Blank: Not empty code: NOT NULL

4. Delete: Delete code representative: Drop Delete is table: table to delete the name: Ceshi

drop table Ceshi;

Code write CREATE DATABASE is note:

1. The type contains the length, after the type plus (parentheses), the parentheses write the length

2. Complete the previous column with a comma

3. The last column do not write a comma

4. Add a semicolon after each SQL statement is finished;

5. If there is a foreign key relationship, first create the primary table

Example:

Creating Tables: Create Table Class (    code varchar PRIMARY key, name varchar), CREATE TABLE    student (    code varchar ( Primary key,    name varchar,    sex bit, age    int,    class varchar,    foreign key (class) References Class (code)), CREATE table Kecheng (    code varchar () primary key,    name varchar); CREATE TABLE Teacher (    Code varchar () primary key,    name varchar); CREATE TABLE Chengji (        IDs int auto_increment Primary key,    scode varchar,    kcode varchar,    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,    kcode varchar,    foreign KEY (Kcode) references Kecheng (code),    Foreign KEY (Tcode) references teacher (code));

MySQL Create tables and databases

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.