当前位置:首页 » 编程语言 » sql查询语句表头如何命名
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql查询语句表头如何命名

发布时间: 2023-01-04 08:50:01

㈠ 多条sql语句一起执行,查询结果中每个表的表名怎么自己命名

就像临时表一样 用括号括起来,命个名字,再查询

㈡ SQL查询语句关于表头部分的一些疑问

简单的可以这样写select * from(select F_109 as 'A级品' from F_109 where F_109='A') as a,(select F_109 as 'P级品' from F_109 where F_109='P') as b,(select F_109 as 'T级品' from F_109 where F_109='T') as c,(select Count(*) as 'A级率' from F_109 where F_109='A') as D

㈢ VF中SQL怎样把查询出来的表从命名

你用的是什么数据库,什么版本,要实现什么样的功能,说具体点。
可以通过直接sql语句来修改表名
如: exec sp_rename '表名','修改后的表名'
有什么疑问可以给我留言。

#参考资料---------------------------------------------------
用SQL语句修改表名与表中的列名
ALTER TABLE table
{ [ ALTER COLUMN column_name
{ new_data_type [ ( precision [ , scale ] ) ]
[ COLLATE < collation_name > ]
[ NULL | NOT NULL ]
| {ADD | DROP } ROWGUIDCOL }
]
| ADD
{ [ < column_definition > ]
| column_name AS computed_column_expression
} [ ,...n ]
| [ WITH CHECK | WITH NOCHECK ] ADD
{ < table_constraint > } [ ,...n ]
| DROP
{ [ CONSTRAINT ] constraint_name
| COLUMN column } [ ,...n ]
| { CHECK | NOCHECK } CONSTRAINT
{ ALL | constraint_name [ ,...n ] }
| { ENABLE | DISABLE } TRIGGER
{ ALL | trigger_name [ ,...n ] }
}

< column_definition > ::=
{ column_name data_type }
[ [ DEFAULT constant_expression ] [ WITH VALUES ]
| [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
]
[ ROWGUIDCOL ]
[ COLLATE < collation_name > ]
[ < column_constraint > ] [ ...n ]

< column_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ NULL | NOT NULL ]
| [ { PRIMARY KEY | UNIQUE }
[ CLUSTERED | NONCLUSTERED ]
[ WITH FILLFACTOR = fillfactor ]
[ ON { filegroup | DEFAULT } ]
]
| [ [ FOREIGN KEY ]
REFERENCES ref_table [ ( ref_column ) ]
[ ON DELETE { CASCADE | NO ACTION } ]
[ ON UPDATE { CASCADE | NO ACTION } ]
[ NOT FOR REPLICATION ]
]
| CHECK [ NOT FOR REPLICATION ]
( logical_expression )
}

< table_constraint > ::=
[ CONSTRAINT constraint_name ]
{ [ { PRIMARY KEY | UNIQUE }
[ CLUSTERED | NONCLUSTERED ]
{ ( column [ ,...n ] ) }
[ WITH FILLFACTOR = fillfactor ]
[ ON { filegroup | DEFAULT } ]
]
| FOREIGN KEY
[ ( column [ ,...n ] ) ]
REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
[ ON DELETE { CASCADE | NO ACTION } ]
[ ON UPDATE { CASCADE | NO ACTION } ]
[ NOT FOR REPLICATION ]
| DEFAULT constant_expression
[ FOR column ] [ WITH VALUES ]
| CHECK [ NOT FOR REPLICATION ]
( search_conditions )
}

示例
A. 更改表以添加新列
下例添加一个允许空值的列,而且没有通过 DEFAULT 定义提供值。各行的新列中的值将为 NULL。

CREATE TABLE doc_exa ( column_a INT)
GO
ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL
GO
EXEC sp_help doc_exa
GO
DROP TABLE doc_exa
GO

B. 更改表以除去列
下例修改表以删除一列。

CREATE TABLE doc_exb ( column_a INT, column_b VARCHAR(20) NULL)
GO
ALTER TABLE doc_exb DROP COLUMN column_b
GO
EXEC sp_help doc_exb
GO
DROP TABLE doc_exb
GO

C. 更改表以添加具有约束的列
下例向表中添加具有 UNIQUE 约束的新列。

CREATE TABLE doc_exc ( column_a INT)
GO
ALTER TABLE doc_exc ADD column_b VARCHAR(20) NULL
CONSTRAINT exb_unique UNIQUE
GO
EXEC sp_help doc_exc
GO
DROP TABLE doc_exc
GO

D. 更改表以添加未验证的约束
下例向表中的现有列上添加约束。该列中存在一个违反约束的值;因此,利用 WITH NOCHECK 来防止对现有行验证约束,从而允许该约束的添加。

CREATE TABLE doc_exd ( column_a INT)
GO
INSERT INTO doc_exd VALUES (-1)
GO
ALTER TABLE doc_exd WITH NOCHECK
ADD CONSTRAINT exd_check CHECK (column_a > 1)
GO
EXEC sp_help doc_exd
GO
DROP TABLE doc_exd
GO

E. 更改表以添加多个带有约束的列
下例向表中添加多个带有约束的新列。第一个新列具有 IDENTITY 属性;表中每一行的标识列都将具有递增的新值。

CREATE TABLE doc_exe ( column_a INT CONSTRAINT column_a_un UNIQUE)
GO
ALTER TABLE doc_exe ADD

/* Add a PRIMARY KEY identity column. */
column_b INT IDENTITY
CONSTRAINT column_b_pk PRIMARY KEY,

/* Add a column referencing another column in the same table. */
column_c INT NULL
CONSTRAINT column_c_fk
REFERENCES doc_exe(column_a),

/* Add a column with a constraint to enforce that */
/* nonnull data is in a valid phone number format. */
column_d VARCHAR(16) NULL
CONSTRAINT column_d_chk
CHECK
(column_d IS NULL OR
column_d LIKE "[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]" OR
column_d LIKE
"([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"),

/* Add a nonnull column with a default. */
column_e DECIMAL(3,3)
CONSTRAINT column_e_default
DEFAULT .081
GO
EXEC sp_help doc_exe
GO
DROP TABLE doc_exe
GO

F. 添加具有默认值的可为空的列
下例添加可为空的、具有 DEFAULT 定义的列,并使用 WITH VALUES 为表中的各现有行提供值。如果没有使用 WITH VALUES,那么每一行的新列中都将具有 NULL 值。

ALTER TABLE MyTable
ADD AddDate smalldatetime NULL
CONSTRAINT AddDateDflt
DEFAULT getdate() WITH VALUES

G. 禁用并重新启用一个约束
下例禁用用于限制可接受的薪水数据的约束。WITH NOCHECK CONSTRAINT 与 ALTER TABLE 一起使用,以禁用该约束并使正常情况下会引起约束违规的插入操作得以执行。WITH CHECK CONSTRAINT 重新启用该约束。

CREATE TABLE cnst_example
(id INT NOT NULL,
name VARCHAR(10) NOT NULL,
salary MONEY NOT NULL
CONSTRAINT salary_cap CHECK (salary < 100000)
)

-- Valid inserts
INSERT INTO cnst_example VALUES (1,"Joe Brown",65000)
INSERT INTO cnst_example VALUES (2,"Mary Smith",75000)

-- This insert violates the constraint.
INSERT INTO cnst_example VALUES (3,"Pat Jones",105000)

-- Disable the constraint and try again.
ALTER TABLE cnst_example NOCHECK CONSTRAINT salary_cap
INSERT INTO cnst_example VALUES (3,"Pat Jones",105000)

-- Reenable the constraint and try another insert, will fail.
ALTER TABLE cnst_example CHECK CONSTRAINT salary_cap
INSERT INTO cnst_example VALUES (4,"Eric James",110000)

H. 禁用并重新启用触发器
下例使用 ALTER TABLE 的 DISABLE TRIGGER 选项来禁用触发器,以使正常情况下会违反触发器条件的插入操作得以执行。然后下例使用 ENABLE TRIGGER 重新启用触发器。

CREATE TABLE trig_example
(id INT,
name VARCHAR(10),
salary MONEY)
go
-- Create the trigger.
CREATE TRIGGER trig1 ON trig_example FOR INSERT
as
IF (SELECT COUNT(*) FROM INSERTED
WHERE salary > 100000) > 0
BEGIN
print "TRIG1 Error: you attempted to insert a salary > $100,000"
ROLLBACK TRANSACTION
END
GO
-- Attempt an insert that violates the trigger.
INSERT INTO trig_example VALUES (1,"Pat Smith",100001)
GO
-- Disable the trigger.
ALTER TABLE trig_example DISABLE TRIGGER trig1
GO
-- Attempt an insert that would normally violate the trigger
INSERT INTO trig_example VALUES (2,"Chuck Jones",100001)
GO
-- Re-enable the trigger.
ALTER TABLE trig_example ENABLE TRIGGER trig1
GO
-- Attempt an insert that violates the trigger.
INSERT INTO trig_example VALUES (3,"Mary Booth",100001)
GO

ReMark:
修改表的名: 例如表是:Goods 该成:MyGoods exec sp_rename 'Goods',MyGoods' go
使用系统存储过程sp_rename 例,想把student表中的列name列为student_name exec sp_rename 'student.name',student_name

㈣ SQL中在做查询时怎样给列重命名

SQL 查询中重命名使用as来处理,语法:select 字段名 as 重命名
1、as可理解为:用作、当成,作为;一般式重命名列名或者表名。
2、例如:
有表table, 列 column_1,column_2
可以写成 select column_1 as 列1,column_2 as 列2 from table as 表
上面的语句就可以解释为,选择 column_1 作为 列1,column_2 作为 列2 从 table 当表

㈤ SQL中在做查询时怎样给某一个字段重命名

SQL中在做查询时怎样给某一个字段重命名

MySQL中,如何使用SQL语句来对表中某一个字段进行重命名呢?我们将使用alter table 这一SQL语句。

重命名字段的语法为:alter table <表名> change <字段名> <字段新名称> <字段的类型>。

现在我们来尝试把test表中的.t_name字段重命名为t_name_new字段。

1、首先查看一下当前test表的结构

mysql> describe test;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| t_id | int(11) | YES | | NULL | |

| t_name | var20) | YES | | NULL | |

| t_password | 32) | YES | | NULL | |

| t_birth | date | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

2、使用alter table语句来修改字段名称

mysql> alter table test change t_name t_name_new var20);

Query OK, 0 rows affected (0.11 sec)

Records: 0 Duplicates: 0 Warnings: 0

3、查看修改过后的结果

mysql> describe test;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| t_id | int(11) | YES | | NULL | |

| t_name_new | var20) | YES | | NULL | |

| t_password | 32) | YES | | NULL | |

| t_birth | date | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

至此,我们可以顺利的修改表中字段名称了。

关于MySQL中使用SQL语句对字段进行重命名,本文就介绍这么多,希望对大家有所帮助,谢谢! ;

㈥ SQL中在做查询时怎样给某一个字段重命名

sql 重命名专业术语是 别名,用 AS 语句实现。

例:给sc_table表中的age字段重命名为st_age,则:

selectageasst_age
fromsc_table

类似的还可以给表起别名

例如:查询sc_student表所有行并重命名为student,则:

select*
fromsc_studentasstudent

㈦ sql语句如何重命名表名和列名

一、更改数据库名

sp_renamedb 更改数据库的名称。

语法: sp_renamedb [ @dbname = ] ' old_name ' , [ @newname = ] ' new_name '
参数: [ @dbname = ] ' old_name ' 是数据库的当前名称。old_name 为 sysname 类型,无默认值。
[ @newname = ] ' new_name ' 是数据库的新名称。 new_name 必须遵循标识符规则。new_name 为 sysname 类型,无默认值。
返回代码值: 0 (成功)或非零数字(失败)
权限: 只有 sysadmin 和 dbcreator 固定服务器角色的成员才能执行 sp_renamedb。

示例: 下例将数据库 accounting 改名为 financial。
EXEC sp_renamedb ' accounting ' , ' financial '

二、更改表名或列名

sp_rename [ @objname = ] ' object_name ' ,
[ @newname = ] ' new_name '
[ , [ @objtype = ] ' object_type ' ]

A. 重命名表:
下例将表 customers 重命名为 custs。
EXEC sp_rename ' customers ' , ' custs '

B. 重命名列:
下例将表 customers 中的列 contact title 重命名为 title。
EXEC sp_rename ' customers.[contact title] ' , ' title ' , ' COLUMN '