One:
1. Create a database
Create Database test2;
2. Delete the database;
Drop database test2;
3. Create a table;
CREATE TABLE Test
(
Code varchar (20),
Name varchar #varchar是字符串类型, need to add length (), code and name are the names of the created columns, a column to write a comma, the last column without commas.
); #是数据库的注释语法.
Garther:
CREATE TABLE Test1
(
Code varchar PRIMARY KEY, #primary key for the primary key, so the code column becomes the primary key.
Name varchar (20)
);
Set name to non-null:
CREATE TABLE Test2
(
Code varchar (primary key),
Name varchar is NOT NULL #not NULL is NOT NULL, so the name column is required by the user.
);
FOREIGN key relationships
Create TABLE Zhu
(
Code int PRIMARY KEY,
Name varchar (20)
);
CREATE TABLE Cong
(
Code int PRIMARY KEY,
Name varchar (20),
Zhu Int, # Adds a foreign key relationship to this column, which is the same type as the primary key type in the Zhu table
Foreign key (Zhu) references Zhu (code) #foreign key foreign Key (Zhu) references Zhu (code) (Zhu) refers to the Zhu table Code column
);
Self-growing column: (When the primary key is not found), add a new column, let it be the primary key, this column is the self-growing column, this column of data from the growth, do not need to add data, but it is meaningless.
CREATE TABLE Haoyou
(
IDS int Auto_increment primary KEY, # auto_increment keyword, so this column is self-growing
Name varchar (20),
Friends varchar (20)
);
4. Delete the table:
drop table haoyou;
Note: A. Add a semicolon after each statement
B. Last column cannot be comma-added
C. Write the full format in English.
5. Modify the statement for the table:
Two. Adding and removing data: crud operation, c for Create Add, R for read query, u for update modification, D for delete delete
1.C: Add Data
CREATE TABLE Test5
(
Code varchar (primary key),
Name varchar (NOT NULL)
);
INSERT into test2 values (' n001 ', ' Zhang San ');
Attention:
A insert into table name values (the data added in parentheses, if it is a string, use single quotation marks, if other types, do not add single quotation marks.) )
b INSERT into test2 values (' n001 ', '); When you add only one value, the other adds ', ' and nothing is written inside.
C INSERT into test2 (' n001 '); only add data to the Code column in Test2, you need to append the name of the column to the table name.
D If the table you want to add has a self-growing column, you can add an empty string to the self-growing column, insert into haoyou values (', ' LZ ', ' sz ');
Examples:
Table creation
Create Table intprimarykeyvarchar (double double double) ;
Add Data:
Database section---Add/remove databases, add/delete tables, add data to the database;