create a table employer in sql in javascript

To create a table employer in SQL using JavaScript, you can use the following code:

index.tsx
// require the mysql package
const mysql = require('mysql');

// create a connection to the database
const connection = mysql.createConnection({
  host: 'localhost', // change this to your MySQL server IP or hostname
  user: 'root',      // change this to your MySQL username
  password: 'secret', // change this to your MySQL password
  database: 'mydb', // change this to your MySQL database name
});

// define the SQL query to create the employer table
const createEmployerTableQuery = `
CREATE TABLE employer (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  phone VARCHAR(20),
  address VARCHAR(255),
  salary DECIMAL(10, 2)
)
`;

// execute the query to create the employer table
connection.query(createEmployerTableQuery, (err, results, fields) => {
  if (err) {
    console.error(err);
  } else {
    console.log('Employer table created successfully.');
  }
});

// close the database connection
connection.end();
975 chars
35 lines

This code uses the mysql package to connect to a MySQL database and create a table called employer. The table has columns for id, name, email, phone, address, and salary. The id column is the primary key with AUTO_INCREMENT property, which automatically assigns a unique integer value to each new row. The name and email columns are not nullable, meaning they must contain a value for every row.

Once you execute the script, it will create the employer table in the specified database. You can now use this table to store and retrieve data related to employers.

gistlibby LogSnag