开发者

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 c开发者_如何学Goreate a table in a specific database?


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, .... )


USE dbname;

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


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
);


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
);


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜