In our study of data sets and in our introduction to tables, we saw that a list could be organized in categories called columns. Here is an example:
As you can see from this arrangement, a column is used to particularly classify one type of data. For example, one column can be used to list some names. Another column can be used to list numbers. Yet another column can be used for a select list of items that keep repeating. To organize the information that a column holds, a table needs a series of details about each column. If you are visually creating a table, you can create the columns in the top section or write SQL code in the bottom section. Two aspects are particularly important: a name and the type of data that a column should/must/can hold. We saw that the primary formula to create a table was: CREATE TABLE TableName After specifying the name of the table, you must list the columns of the table. The list of columns starts with an opening parenthesis "(". The list ends with a closing parenthesis ")". Each column must be separated from the next with a comma, except for the last column. You can include all columns on the same line if possible as follows: CREATE TABLE [SchemaName.]Country(Column1, Column2, Column3) Alternatively, to make your statement easier to read, you should create each column on its own line as follows: CREATE TABLE [SchemaName.]Country( Column1, Column2, Column3); There are two primary pieces of information you must specify for each column: its name and its type. Therefore, the syntax of creating a column is: ColumnName DataType Options To be able to recognize the categories of information that a column holds, the column must have a name. The name of a column:
After respecting these rules, you can add your own rules. In our lessons, here are the rules we will use to name our columns:
After deciding on the name of a column, the database needs to know what kind of information the column would hold. This means that you must specify the data type that is necessary for a particular column. We saw that you can create user-defined data types for existing Transact-SQL data types. If you are working on a database, you can create and store your user-defined types in it. After creating the UDT(s), you can use it(them) for your column(s).
A database deals with various types of data, appropriate or not for certain fields. This means that you should take care of jobs behind the scenes as much as you can. One way you can do this is by controlling the amount of information that can be stored in a particular field. As various columns can hold different types of data, so can the same data type control its own mechanism of internal data entry. The length of data means different things to different fields. Columns that carry the same data type can have different lengths. Bit Fields: We saw already that a bit column type is meant for one of two answers. The user is supposed to simply let the database know that the answer is yes or no, true or false, on or off, 1 or 0. Therefore, the only length of this field is 1. Integers: The length of an integer is the number of bytes its field can hold. For an int type, that would be 4 bytes. Decimal and Floating-Point Numbers: The Length specifies how many bytes the field can store. Strings: The length of a character or string column specifies the maximum number of characters that the field can hold. Some of the data types need to have a length. This is certainly true for all string or text-based columns (char, text, varchar, etc). In the case of text-based columns, when using SQL to create your columns, because it is less visual than the table design, you cannot rely on the default length of strings suggested by SQL (in fact, in MySQL, you must specify a length for varchar). Here are examples: private void btnDatabase_Click(object sender, EventArgs e)
{
using (SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand command =
new SqlCommand("CREATE TABLE Customers (" +
"DrvLicNbr NVarChar(50), " +
"DateIssued Date," +
"DateExpired Date," +
"FullName nvarchar(120)," +
"Address NVARCHAR(120)," +
"City nvarchar(50)," +
"State nvarchar(100)," +
"PostalCode nvarchar(20)," +
"HomePhone nvarchar(20)," +
"OrganDonor bit);",
connection);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("A new table named \"Customers\" has been created.");
}
}
If a column was already created, to get its length, you can call the COL_LENGTH() function. Its syntax is: COL_LENGTH('table' , 'column') RETURNS smallint;
This function takes two required arguments. The first argument is the name of the table that owns the column. The second argument is the name of the column. The result is as follows:
Here is an example: SELECT COL_LENGTH(N'Employees', N'HourlySalary') AS [Size of Salary];
We will write many expressions that include the names of columns. In such expressions, you will need to indicate the particular column you are referring to. There are various ways you can do this. To refer to, or to indicate, a table:
You can create an alias name of a table to use in an expression that involves a column. To do this, type a letter or a word that will represent the table to which the column belongs. The letter or the word is followed by a period, and followed by the name of the column. An example would be empl.LastName. At the end of the statement, you must type the name of the table, followed by space, and followed by the letter or the word. An example would be Employee empl. You can also type AS between the name of the table and its alias. An example is Employee AS empl (we will see various examples in future lessons.
Column maintenance consists of reviewing or changing any of its aspects. This includes reviewing the structure of columns of a table, renaming a column, deleting a column, changing the data type or the nullity of a column, etc.
Because different languages use different mechanisms in their alphabetical characters, this can affect the way some sort algorithms or queries are performed on data, you can ask the database engine to apply a certain language mechanism to the field by changing the Collation property. To specify the collation option, when programmatically creating the table, type COLLATE, followed by the desired collation code. Here is an example: CREATE TABLE Customers(
FullName varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS
);
When making a change on a column, you are also said to alter the table. To programmatically support this operation, the SQL starts with the following formula: ALTER TABLE TableName When using this statement, the ALTER TABLE expression is required and it is followed by the name of the table.
After a table has already been created, you can still add a new column to it. In SQL, the basic formula to add a new column to an existing table is: ALTER TABLE TableName ADD ColumnName Properties The ColumnName factor is required. In fact, on the right side of the ADD operator, define the column by its name and use all the options we reviewed for columns. Here is an example: private void btnDatabase_Click(object sender, EventArgs e)
{
using (SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand command =
new SqlCommand("ALTER TABLE Customers " +
"ADD EmaillAddress varchar(100);",
connection);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show(
"A new column named \"EmailAddress\" has been added.");
}
}
When this code is executed, a new column named Address, of type varchar, with a limit of 100 characters, that allows empty entries, will be added to a table named StaffMembers in the current database.
If you find out that the name of a column is not appropriate, you can change it. To do this, execute sp_rename using the following formula: sp_rename/font> 'TableName.ColumnName', 'NewColumnName', 'COLUMN' The sp_rename factor and the 'COLUMN' string are required. The TableName factor is the name of the table that the column belongs to. The ColumnName is the current name of the column. The NewColumnName is the desired name you want to give to the column. Here is an example: private void btnDatabase_Click(object sender, EventArgs e)
{
string strConnection = "sp_rename 'StaffMembers.FullName', 'EmployeeName', 'COLUMN';";
SqlConnection conDatabase = new
SqlConnection("Data Source=(local);" +
"Database='Countries2';" +
"Integrated Security=yes");
SqlCommand cmdDatabase = new SqlCommand(strConnection, conDatabase);
conDatabase.Open();
cmdDatabase.ExecuteNonQuery();
conDatabase.Close();
}
When this code is executed, the interpreter will look for a column named FullName in the StaffMembers table of the current or selected database. If it finds that column in the table, then it renames it EmployeeName.
If you have an undesired column that you don't want anymore in a table, you can remove it. To programmatically delete a column, use the following formula: ALTER TABLE TableName DROP COLUMN ColumnName On the right side of the ALTER TABLE expression, type the name of the table. On the right side of the DROP COLUMN expression, enter the name of the undesired column. Here is an example: private void btnDatabase_Click(object sender, EventArgs e)
{
using (SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand command =
new SqlCommand("ALTER TABLE Customers " +
"DROP Column DateIssued;",
connection);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show(
"The column named \"DateIssued\" has been deleted.");
}
}
WWhen this code is executed, the interpreter will look for a column named CurrentResidence in a table named StaffMembers of the current. If it finds that column, it will remove it from the table. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|