當前位置:首頁 » 編程語言 » 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 '