user295515
user295515

Reputation: 897

How to create a table in a particular database?

The create database statement is:

CREATE TABLE table_name
(
    column_name1 data_type,
    column_name2 data_type,
    column_name3 data_type,
    ....
)

But I'm wondering how can I create a table in a specific database?

Upvotes: 50

Views: 126090

Answers (6)

Bibek Gupta
Bibek Gupta

Reputation: 31

You can create table inside a particular database as below:

CREATE TABLE database_name.table_name_();

CREATE TABLE library_database.book
(
    book_id int(10) not null,
    book_name varchar(20) not null,
    author_name varchar(20)not null
);

Upvotes: 3

Md Shariful Islam
Md Shariful Islam

Reputation: 157

You can try this query. Suppose the database name schoolmanagementsystem, table name student, and table columns name are student_id, student_first_name, and student_last_name.

So you can create a table (student) in a particular database (schoolmanagementsystem) following this way.

CREATE TABLE schoolmanagementsystem.student
(
    student_id int(10) not null,
    student_first_name varchar(20) not null,
    student_last_name varchar(20)not null
);

Upvotes: 5

mahesh
mahesh

Reputation: 1

SELECT * FROM information_schema.TABLES where table_schema ='database_name' ORDER BY TABLES.CREATE_TIME DESC

Upvotes: -1

Dmitry Yudakov
Dmitry Yudakov

Reputation: 15734

You can specify the DB Name in the same query:

CREATE TABLE database_name.table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )

Upvotes: 82

Peter Kühne
Peter Kühne

Reputation: 3274

Assuming that you have more than one database, in mysql, you can do SHOW DATABASES to view them all and then USE with your db name to make it the current one. Running CREATE TABLE will then create the table in that database.

Upvotes: 0

wRAR
wRAR

Reputation: 25693

USE dbname;

http://dev.mysql.com/doc/refman/5.1/en/use.html

Upvotes: 22

Related Questions