在MYSQL中创建表的方法有哪些

在MYSQL中创建表的方法有哪些,第1张

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

[(create_definition,)]

[table_options] [select_statement]

或:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

[(] LIKE old_tbl_name [)];

create_definition:

column_definition

| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,)

| KEY [index_name] [index_type] (index_col_name,)

| INDEX [index_name] [index_type] (index_col_name,)

| [CONSTRAINT [symbol]] UNIQUE [INDEX]

[index_name] [index_type] (index_col_name,)

| [FULLTEXT|SPATIAL] [INDEX] [index_name] (index_col_name,)

| [CONSTRAINT [symbol]] FOREIGN KEY

[index_name] (index_col_name,) [reference_definition]

| CHECK (expr)

column_definition:

col_name type [NOT NULL | NULL] [DEFAULT default_value]

[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]

[COMMENT 'string'] [reference_definition]

type:

TINYINT[(length)] [UNSIGNED] [ZEROFILL]

| SMALLINT[(length)] [UNSIGNED] [ZEROFILL]

| MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]

| INT[(length)] [UNSIGNED] [ZEROFILL]

| INTEGER[(length)] [UNSIGNED] [ZEROFILL]

| BIGINT[(length)] [UNSIGNED] [ZEROFILL]

| REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]

| DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]

| FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]

| DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]

| NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]

| DATE

| TIME

| TIMESTAMP

| DATETIME

| CHAR(length) [BINARY | ASCII | UNICODE]

| VARCHAR(length) [BINARY]

| TINYBLOB

| BLOB

| MEDIUMBLOB

| LONGBLOB

| TINYTEXT [BINARY]

| TEXT [BINARY]

| MEDIUMTEXT [BINARY]

| LONGTEXT [BINARY]

| ENUM(value1,value2,value3,)

| SET(value1,value2,value3,)

| spatial_type

index_col_name:

col_name [(length)] [ASC | DESC]

reference_definition:

REFERENCES tbl_name [(index_col_name,)]

[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]

[ON DELETE reference_option]

[ON UPDATE reference_option]

reference_option:

RESTRICT | CASCADE | SET NULL | NO ACTION

table_options: table_option [table_option]

table_option:

{ENGINE|TYPE} = engine_name

| AUTO_INCREMENT = value

| AVG_ROW_LENGTH = value

| [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]

| CHECKSUM = {0 | 1}

| COMMENT = 'string'

| CONNECTION = 'connect_string'

| MAX_ROWS = value

| MIN_ROWS = value

| PACK_KEYS = {0 | 1 | DEFAULT}

| PASSWORD = 'string'

| DELAY_KEY_WRITE = {0 | 1}

| ROW_FORMAT = {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}

| UNION = (tbl_name[,tbl_name])

| INSERT_METHOD = { NO | FIRST | LAST }

| DATA DIRECTORY = 'absolute path to directory'

| INDEX DIRECTORY = 'absolute path to directory'

partition_options:

PARTITION BY

[LINEAR] HASH(expr)

| [LINEAR] KEY(column_list)

| RANGE(expr)

| LIST(column_list)

[PARTITIONS num]

[ SUBPARTITION BY

[LINEAR] HASH(expr)

| [LINEAR] KEY(column_list)

[SUBPARTITIONS(num)]

]

[(partition_definition), [(partition_definition)], ]

partition_definition:

PARTITION partition_name

[VALUES {

LESS THAN (expr) | MAXVALUE

| IN (value_list) }]

[[STORAGE] ENGINE [=] engine-name]

[COMMENT [=] 'comment_text' ]

[DATA DIRECTORY [=] 'data_dir']

[INDEX DIRECTORY [=] 'index_dir']

[MAX_ROWS [=] max_number_of_rows]

[MIN_ROWS [=] min_number_of_rows]

[TABLESPACE [=] (tablespace_name)]

[NODEGROUP [=] node_group_id]

[(subpartition_definition), [(subpartition_definition)], ]

subpartition_definition:

SUBPARTITION logical_name

[[STORAGE] ENGINE [=] engine-name]

[COMMENT [=] 'comment_text' ]

[DATA DIRECTORY [=] 'data_dir']

[INDEX DIRECTORY [=] 'index_dir']

[MAX_ROWS [=] max_number_of_rows]

[MIN_ROWS [=] min_number_of_rows]

[TABLESPACE [=] (tablespace_name)]

[NODEGROUP [=] node_group_id]

select_statement:

[IGNORE | REPLACE] [AS] SELECT (Some legal select statement)

第一种:

create table 表名

(列1 数据类型(数据长度),

列2 数据类型 (数据长度))

第二种:

先用建模工具(visio,powerdigner)做出ER图,再生成数据库脚本,再在查询分析器中运行DLL脚本生成数据库及表结构。

1、下载mysql,安装,然后打开cmd,cd到mysql所在的目录,然后输入mysqld启动mysql服务器

2、查看当前所有的数据库 : show databases;

3、创建数据库 : create database 数据库名称; (这里创建了test_db数据库,然后用1命令查看所有数据库)

4、这里数据库创建完毕了,接下来就是创建数据库表,比如我们在test_db中创建一个为tb_user的表,该表包含字段email ,password

5、切换当前数据库到test_db

命令 use test_db;

6、

dc2ed6aa12228bee31bb96dc0507652png

7、创建表TB_USER ,创建成功后用show tab

简单点: 用户表(用户ID,姓名,密码,性别,年龄,职业,微信,邮箱,手机,专长,用户状态,登录时间,是否管理员)其中也可以单独设置一个管理员表

试题类别:(类别 ID,类别名称,类别描述)

试题表:(试题 ID,试题类别 ID,试题描述,试题难度,试题答案)

答题表:(答题 ID,用户 ID,试题 ID,答题答案,答题评分)

大概就这样

CREATE TABLE [学生] (

[学号] [int] NOT NULL ,

[姓名] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,

[性别] [nvarchar] (1) COLLATE Chinese_PRC_CI_AS NULL ,

[系号] [int] NULL ,

[年龄] [int] NULL

) ON [PRIMARY]

CREATE TABLE [系表] (

[系号] [int] NOT NULL ,

[系名] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,

[系主任] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL

) ON [PRIMARY]

ALTER TABLE [学生] ADD

CONSTRAINT [PK_学生] PRIMARY KEY CLUSTERED

(

[学号]

) ON [PRIMARY]

ALTER TABLE [系表] ADD

CONSTRAINT [PK_系表] PRIMARY KEY CLUSTERED

(

[系号]

) ON [PRIMARY]

ALTER TABLE [学生] ADD

CONSTRAINT [FK_学生_系表] FOREIGN KEY

(

[系号]

) REFERENCES [系表] (

[系号]

) ON DELETE CASCADE ON UPDATE CASCADE

以上就是关于在MYSQL中创建表的方法有哪些全部的内容,包括:在MYSQL中创建表的方法有哪些、数据库如何建表、mysql数据库怎么建表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10123559.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存