用SQL语句创表

用SQL语句创表,第1张

create

table

S

(MNO

varchar(10),

PNO

varchar(10),

QTY

number(10)

not

null,

primary

key(MNO));

和上一位区别在于定义字符时用了varchar,

数据库一般提供char,varchar,varchar2几种存储字符的数据结构。如定义char(10),那么当你存放的数据占了5个字节的时候,存储器上开辟的另5个字节容量就是为空,但是仍占着地方;而varchar(10)就可以根据实际数据大小来选择,但是当数据库查找或更新数据时,浏览char的数据较快,一般也没有太大区别,除了有很多数据;varchar2和varchar的区别,前者是用unicode编码,后者是ASCII码,所以前者一个占两个字节,后者是一个。

自己打开企业管理器,随便找个表,导出一个创建表的脚本,就什么都有了。

要语法,SQL的联机帮助里就有:

CREATE TABLE

[ database_name [ schema_name ] | schema_name ] table_name

( { <column_definition> | <computed_column_definition> }

[ <table_constraint> ] [ ,n ] )

[ ON { partition_scheme_name ( partition_column_name ) | filegroup

| "default" } ]

[ { TEXTIMAGE_ON { filegroup | "default" } ]

[ ; ]

<column_definition> ::=

column_name <data_type>

[ COLLATE collation_name ]

[ NULL | NOT NULL ]

[

[ CONSTRAINT constraint_name ] DEFAULT constant_expression ]

| [ IDENTITY [ ( seed ,increment ) ] [ NOT FOR REPLICATION ]

]

[ ROWGUIDCOL ] [ <column_constraint> [ n ] ]

<data type> ::=

[ type_schema_name ] type_name

[ ( precision [ , scale ] | max |

[ { CONTENT | DOCUMENT } ] xml_schema_collection ) ]

<column_constraint> ::=

[ CONSTRAINT constraint_name ]

{ { PRIMARY KEY | UNIQUE }

[ CLUSTERED | NONCLUSTERED ]

[

WITH FILLFACTOR = fillfactor

| WITH ( < index_option > [ , n ] )

]

[ ON { partition_scheme_name ( partition_column_name )

| filegroup | "default" } ]

| [ FOREIGN KEY ]

REFERENCES [ schema_name ] referenced_table_name [ ( ref_column ) ]

[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

[ NOT FOR REPLICATION ]

| CHECK [ NOT FOR REPLICATION ] ( logical_expression )

}

<computed_column_definition> ::=

column_name AS computed_column_expression

[ PERSISTED [ NOT NULL ] ]

[

[ CONSTRAINT constraint_name ]

{ PRIMARY KEY | UNIQUE }

[ CLUSTERED | NONCLUSTERED ]

[

WITH FILLFACTOR = fillfactor

| WITH ( <index_option> [ , n ] )

]

| [ FOREIGN KEY ]

REFERENCES referenced_table_name [ ( ref_column ) ]

[ ON DELETE { NO ACTION | CASCADE } ]

[ ON UPDATE { NO ACTION } ]

[ NOT FOR REPLICATION ]

| CHECK [ NOT FOR REPLICATION ] ( logical_expression )

[ ON { partition_scheme_name ( partition_column_name )

| filegroup | "default" } ]

]

< table_constraint > ::=

[ CONSTRAINT constraint_name ]

{

{ PRIMARY KEY | UNIQUE }

[ CLUSTERED | NONCLUSTERED ]

(column [ ASC | DESC ] [ ,n ] )

[

WITH FILLFACTOR = fillfactor

|WITH ( <index_option> [ , n ] )

]

[ ON { partition_scheme_name (partition_column_name)

| filegroup | "default" } ]

| FOREIGN KEY

( column [ ,n ] )

REFERENCES referenced_table_name [ ( ref_column [ ,n ] ) ]

[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

[ NOT FOR REPLICATION ]

| CHECK [ NOT FOR REPLICATION ] ( logical_expression )

}

<index_option> ::=

{

PAD_INDEX = { ON | OFF }

| FILLFACTOR = fillfactor

| IGNORE_DUP_KEY = { ON | OFF }

| STATISTICS_NORECOMPUTE = { ON | OFF }

| ALLOW_ROW_LOCKS = { ON | OFF}

| ALLOW_PAGE_LOCKS ={ ON | OFF}

}

1

create table class

(

classno char(5) primary key,

classname char(10) not null,

amount int

)

2

create table student

(

sno char(8) primary key, 

sname char(20) not null unique,

age smallint, 

classno char(5) references class(classno)

)

创建表的sql语句是:

use 数据库名称

go

if exists(select from sysobjects where name='表名')

drop table 表名--如果表名是关键字,还需打上单引号(英文的)

go

create table 表名

--这里写字段

以上就是关于用SQL语句创表全部的内容,包括:用SQL语句创表、如何使用SQL语句创建表,要完整的语句。、数据库 SQL语言创建表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/web/10156808.html

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

发表评论

登录后才能评论

评论列表(0条)

保存