Program to create a table with Mysql.

Here, we have a basic program example to create table using Mysq in Python and Java.

Code to create table using Mysql in Python

Example
1
2
3
4
5
import mysql.connector
con=mysql.connector.connect(host="localhost", user="root", passwd="root", database="coder")
cur=con.cursor()
cur.execute("create table Book( BookName varchar(30), Author varchar(30) )")
con.close()

Code to create table using Mysql in Java

Example
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
public class CreateTable {
   static final String DB_URL = "jdbc:mysql://localhost/TestDb";
   static final String USER = "root";
   static final String PASS = "vsit@123";
 
   public static void main(String[] args) {
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         Statement stmt = conn.createStatement();
      ) {            
          String sql = "CREATE TABLE DemoTable " +
                   "(Id INTEGER not NULL, " +
                   " Name VARCHAR(255), " +
                   " CountryName VARCHAR(255), " +
                   " Age INTEGER, " +
                   " PRIMARY KEY ( Id ))";
 
         stmt.executeUpdate(sql);
         System.out.println("Created table in given database...");       
      } catch (SQLException e) {
         e.printStackTrace();
      }
   }
}