創表
CREATE TABLE IF NOT EXISTS tableName (
`FieldName1` varchar(64) NOT NULL Default ' ', `FieldName2` double NOT NULL Default 0,
PRIMARY KEY (`FieldName2`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
查看某個數據庫表的DDL
show create table ‘tableName’
會有兩列屬性出現,第二列即爲創表語句
插入數據
REPLACE INTO tableName (`FieldName1`,`FieldName2`) VALUES ('111',222)
INSERT INTO tableName (`FieldName1`,`FieldName2`) VALUES ('111',222)
- replace into 會有兩步操作,先檢查是否key值存在,存在就先刪除,然後插入
- insert into,如果key值存在,插入會失敗
插入多條數據
REPLACE INTO tableName
(`FieldName1`,`FieldName2`)
VALUES
('111',222),
('222',333),
('333',444)
-----------------------------------------------------
INSERT INTO tableName
(`FieldName1`,`FieldName2`)
VALUES
('111',222),
('222',333),
('333',444)
判斷某個數據庫表中,是否存在某列
Describe tableName `FieldName`
判斷某個數據庫表是否存在
SELECT table_name FROM information_schema.TABLES WHERE table_name ='tableName'
AND TABLE_SCHEMA= 'tableSchema';
- 注意,這裏的tableSchema指的是MySQL中的schema,可以理解爲數據庫服務器上的某個數據庫,一個數據庫服務器可以有多個數據庫
- 注意,這個schema一定要給,不然判斷時會查找整個數據庫服務器,而不是某個特定的數據庫
獲取某個數據庫表中的所有列
select COLUMN_NAME from information_schema.COLUMNS where table_name = 'tableName'
and TABLE_SCHEMA ='tableSchema';
- 同理,這裏的schema也要給出,否則獲取的列名會是整個數據庫服務器上所有同名表的列名的集合,並且這個集合沒有去重