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