如果MySQL中存在表,则插入表,否则实现CREATE TABLE并创建表
这是使用CREATETABLEIFNOTEXISTS检查表是否已经存在的语法-
create table if not exists yourTableName ( yourColumnName1 dataType, . . . . N ); insert into yourTableName values(yourValue1,.......N);
让我们首先创建一个表-
mysql> create table if not exists DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentSubject varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(StudentSubject) values('Java'); mysql> insert into DemoTable(StudentSubject) values('MySQL'); mysql> insert into DemoTable(StudentSubject) values('C');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+----------------+ | StudentId | StudentSubject | +-----------+----------------+ | 1 | Java | | 2 | MySQL | | 3 | C | +-----------+----------------+ 3 rows in set (0.00 sec)