Step 1: Open SQL Server Management Studio (SSMS)
- Launch SQL Server Management Studio.
- Connect to your SQL Server instance.
Step 2: Select the Database
In the Object Explorer, expand the "Databases" node.
Select the database where you want to create the
DailyExpendituretable. If you don't have a database, you can create one using the following SQL command:
Step 3: Create the DailyExpenditure Table
Open a new query window by clicking on "New Query".
Copy and paste the following SQL code into the query window:
Execute the query by clicking on the "Execute" button or pressing
F5.
Step 4: Verify Table Creation
To verify that the table has been created successfully, you can run the following query:
If the table exists, it will return information about the DailyExpenditure table.
Step 5: Insert Data into the Table
You can insert data into the DailyExpenditure table using the INSERT INTO statement. Here’s an example:
Explanation of the Insert Statement
- You do not need to specify the
DailyExpenditureIdandCreatedDatecolumns because:DailyExpenditureIdis anIDENTITYcolumn, which means SQL Server will automatically generate a unique value for it.CreatedDatehas a default value of the current date and time, so it will automatically be populated.
Step 6: Query Data from the Table
To retrieve data from the DailyExpenditure table, you can use the SELECT statement. Here’s an example:
This will return all records from the DailyExpenditure table.
Step 7: Update Data in the Table
If you need to update a record, you can use the UPDATE statement. For example, if you want to update the CounterBalance for the record with DailyExpenditureId of 1:
Step 8: Delete Data from the Table
To delete a record from the DailyExpenditure table, you can use the DELETE statement. For example, to delete the record with DailyExpenditureId of 2:
Step 9: Additional Queries
You can also perform more complex queries. For example, to find all records where the OpeningBalance is greater than 1000:
Step 10: Drop the Table (if needed)
If you ever need to remove the DailyExpenditure table, you can use the DROP TABLE statement:
Table Schema
Explanation of Each Component
Table Name: DailyExpenditure
- This is the name of the table. It indicates that the table is intended to store records related to daily expenditures.
Column Definitions:
DailyExpenditureId Int Identity
- Data Type:
Int- This column will store integer values. It is commonly used for unique identifiers.
- Identity:
- This property indicates that the value for this column will be automatically generated by SQL Server. The first value will start at 1, and it will increment by 1 for each new record. This is useful for ensuring that each record has a unique identifier.
- Constraint:
Constraint Pk_DailyExpenditure_DailyExpendureId Primary Key- This defines the column as the primary key of the table. A primary key uniquely identifies each record in the table and ensures that no two records can have the same value in this column. It also cannot contain null values.
PreviousBalance Numeric(18,2)
- Data Type:
Numeric(18,2)- This column can store numeric values with a total of 18 digits, of which 2 digits can be after the decimal point. This is suitable for storing monetary values, such as the previous balance before the current expenditure.
- Constraints:
- There are no specific constraints applied to this column in the provided schema, but you could add constraints like
NOT NULL if you want to ensure that a value must always be provided.
OpeningBalance Numeric(18,2)
- Data Type:
Numeric(18,2)- Similar to
PreviousBalance, this column will store the opening balance for the day, formatted to handle monetary values.
- Constraints:
- Like
PreviousBalance, there are no specific constraints applied, but you could add constraints as needed.
CounterBalance Numeric(18,2)
- Data Type:
Numeric(18,2)- This column is intended to store the counter balance, which could represent the remaining balance after expenditures have been accounted for.
- Constraints:
- Again, there are no specific constraints applied, but you could add constraints as needed.
CreatedDate DateTime
- Data Type:
DateTime- This column will store the date and time when the record was created. The
DateTime data type is suitable for storing both date and time information.
- Constraint:
Constraint df_DailyExpenditure_CreatedDate Default(getDate())- This sets a default value for the
CreatedDate column to the current date and time when a new record is inserted. The getDate() function retrieves the current date and time from the server. This constraint ensures that if no value is provided for CreatedDate during an insert operation, it will automatically be populated with the current date and time.
CreatedBy Varchar(20)
- Data Type:
Varchar(20)- This column will store variable-length character strings with a maximum length of 20 characters. It is typically used to store names or identifiers of users.
- Constraints:
- There are no specific constraints applied to this column in the provided schema, but you could add constraints like
NOT NULL if you want to ensure that a value must always be provided.
Table Name: DailyExpenditure
- This is the name of the table. It indicates that the table is intended to store records related to daily expenditures.
Column Definitions:
DailyExpenditureId Int Identity- Data Type:
Int- This column will store integer values. It is commonly used for unique identifiers.
- Identity:
- This property indicates that the value for this column will be automatically generated by SQL Server. The first value will start at 1, and it will increment by 1 for each new record. This is useful for ensuring that each record has a unique identifier.
- Constraint:
Constraint Pk_DailyExpenditure_DailyExpendureId Primary Key- This defines the column as the primary key of the table. A primary key uniquely identifies each record in the table and ensures that no two records can have the same value in this column. It also cannot contain null values.
- Data Type:
PreviousBalance Numeric(18,2)- Data Type:
Numeric(18,2)- This column can store numeric values with a total of 18 digits, of which 2 digits can be after the decimal point. This is suitable for storing monetary values, such as the previous balance before the current expenditure.
- Constraints:
- There are no specific constraints applied to this column in the provided schema, but you could add constraints like
NOT NULLif you want to ensure that a value must always be provided.
- There are no specific constraints applied to this column in the provided schema, but you could add constraints like
- Data Type:
OpeningBalance Numeric(18,2)- Data Type:
Numeric(18,2)- Similar to
PreviousBalance, this column will store the opening balance for the day, formatted to handle monetary values.
- Similar to
- Constraints:
- Like
PreviousBalance, there are no specific constraints applied, but you could add constraints as needed.
- Like
- Data Type:
CounterBalance Numeric(18,2)- Data Type:
Numeric(18,2)- This column is intended to store the counter balance, which could represent the remaining balance after expenditures have been accounted for.
- Constraints:
- Again, there are no specific constraints applied, but you could add constraints as needed.
- Data Type:
CreatedDate DateTime- Data Type:
DateTime- This column will store the date and time when the record was created. The
DateTimedata type is suitable for storing both date and time information.
- This column will store the date and time when the record was created. The
- Constraint:
Constraint df_DailyExpenditure_CreatedDate Default(getDate())- This sets a default value for the
CreatedDatecolumn to the current date and time when a new record is inserted. ThegetDate()function retrieves the current date and time from the server. This constraint ensures that if no value is provided forCreatedDateduring an insert operation, it will automatically be populated with the current date and time.
- This sets a default value for the
- Data Type:
CreatedBy Varchar(20)- Data Type:
Varchar(20)- This column will store variable-length character strings with a maximum length of 20 characters. It is typically used to store names or identifiers of users.
- Constraints:
- There are no specific constraints applied to this column in the provided schema, but you could add constraints like
NOT NULLif you want to ensure that a value must always be provided.
- There are no specific constraints applied to this column in the provided schema, but you could add constraints like
- Data Type:
