Implementing database data entry with code
T-SQL statements
Query statement divided into several blocks
① Creating a Table
CREATE TABLE Car
(Code varchar () primary key, #primary key primary key to define primary key column
Name varchar (not NULL), #not null non-null
Time Date,
Price float,
Brand varchar references brand #references reference to create foreign keys to build foreign keys from tables
);
CREATE TABLE Brand (
Code varchar (primary key),
Ame varchar (50)
);
The error is due to the use of parentheses in the Chinese state. The correct parentheses are shown in red
② Self-growth table
CREATE TABLE Pinpai (
IDS int Auto_increment primary KEY, #auto_increment self-growth
Name varchar (50)
);
Note: All symbols must be in the English state,
After each table is created, add a semicolon; There is a conflict between multiple tables without semicolons.
Do not add a comma after the last column in the table is finished.
To delete a table:
drop table Pinpai
Database operation: CRUD additions and deletions
1, how to add data to the database
INSERT into Brand values (' b001 ', ' BMW X5 '); #第一种方式
INSERT into Brand values (' b002 '); #第二种方式
Insert into Pinpai (Name) VALUES (' VW '); #自增长时添加name add must have primary key value when not self-growing
INSERT into Pinpai values (' ', ' VW '); #处理自增长列
2, the simplest query
SELECT * FROM Pinpai #查询所有数据 * represents columns
SELECT * from Pinpai where Ids=1; #根据条件查询
3, modify the data
Update + table name set column name = "" Where primary key =value; #主键是不能修改的
Update Pinpai set name= ' Porsche ' where ids=3;
Update car set name= ' Harvard ', Time= ' 2012-3-4 ', price= ' + ', brand= ' b002 ' where code= ' c001 '
4. Delete data
Delete from Brand #删除表内所有数据
Delete from Brand where ids=4; A row of data #删除表内主键IDS to 4
2016/3/13 MySQL additions and deletions to the CRUD with code implementation