Create a SQL Server database

Author: Tamara Smith
Date Of Creation: 28 January 2021
Update Date: 1 July 2024
Anonim
SQL Server 2012 - Creating a database
Video: SQL Server 2012 - Creating a database

Content

SQL Server databases are among the most widely used, in part because of the ease with which they can be created and maintained. Along with a free graphical user interface (GUI) like SQL Server Management, you don't have to fiddle with commands from the command prompt. Read on at Step 1 below to create a database in minutes and start entering your data.

To step

  1. Install the SQL Server Management Studio software. This software can be downloaded for free from the Microsoft website, and offers the possibility to connect to and manage an SQL server via a graphical interface, instead of the command prompt.
    • To be able to connect to an external SQL server, you need this or equivalent software.
    • If you have a Mac, you can use open source software such as DbVisualizer or SQuirreL SQL. The interfaces differ, but the principles are the same.
    • See wikiHow for articles on creating databases using command prompt tools.
  2. Start SQL Server Management Studio. When you start the program for the first time, you will be asked which server you want to connect to. If you already have a server running and the rights to connect to it, you can enter the server address and login details. If you want to create a local database, enter a . for the database name and set the login method as "Windows Authentication".
    • Click on Connect to continue.
  3. Look for the database folder. After the connection to the server is established, locally or remotely, the Object Explorer window will open on the left side of the screen. At the top of the Object Explorer tree you will find the server you are connected to. If it is not expanded, click on the "+" located next to it. Now you have found the folder Databases.
  4. Create a new database. Right click on the Databases folder and select "New Database ...". A window will appear allowing you to configure the database before configuring it. Give the database a name to identify it. Most users can leave the rest of the settings as they are.
    • You will notice that when you enter the name of the database, two additional files will be created: the Data and Log file. The data file contains all data from your database and the log file keeps track of changes to the database.
    • Click OK to create the database. You will see the new database appear in the expanded Databases folder. It has a cylinder as an icon.
  5. Create a table. A database can only store data if you first create a structure for that data. A table contains the information you enter into your database, and you will need to create such a table before continuing. Expand the new database in your Databases folder, and right click on the Tables folder and select "New Table ...".
    • Several windows will appear allowing you to edit the new table.
  6. Create the Primary Key. It is strongly recommended that you create a primary key as the first column of your table. This acts like an ID, or record (row) number, with which you can easily recall these inputs later. Create this and enter an "ID" in the Column Name field, type int in the Data Type field and uncheck the "Allow Nulls" box. Click the key icon in the menu bar to set this column as the primary key.
    • You don't want to accept zero values ​​because an input must always be at least "1". If you do allow a zero value, your first entry will be a "0".
    • In the Column Properties window, scroll down until you see the Identity Specification option. Expand it and set "(ls Identity)" to "Yes". This will automatically increase the value of ID for each new row added, which means that each new entry is automatically numbered.
  7. Understand how tables are structured. Tables consist of fields, also called columns. Each column is a representation of a database entry. For example, if you create a database of employees, you will have for example a column "First name", "Last name" and "Address", and a column "Phone number".
  8. Create the rest of your columns. When you have finished filling in the fields for the primary key, you will notice that new fields have appeared underneath. This gives you the option to enter the next row of data. Fill in the fields as you wish and make sure you are using the correct data type for the data you enter in that column:
    • nchar (#) - This is the data type you use for text, such as names, addresses, etc. The numbers in brackets indicate the maximum number of characters allowed in a given field. By setting a limit you can be sure that the size of your database remains manageable. Telephone numbers should be stored in this format, because you do not perform any arithmetic operations on them.
    • int - This is for integers and is usually used for the ID field.
    • decimal (X,y) - Stores numbers in decimal form, and the numbers in parentheses indicate the total number of digits and the number of decimal places respectively. For instance: decimal (6.2) stores numbers as 0000.00.
  9. Save your table. When you have finished creating columns, you will first have to save the table before you can enter data. Click on the Save icon in the toolbar and enter a name for the table. It is best to give your table a name that makes it clear what the content is, especially with larger databases with several tables.
  10. Enter data into your table. Once you have saved the table, you can start entering data. Expand the Tables folder in the Object Explorer window. If the new table is not listed, right click on the Tables folder and select Refresh. Right click on the table and select "Edit Top 200 Rows".
    • The middle window will show fields where you can enter data. The ID field will be filled in automatically, so you can ignore it first. Fill in the information in the rest of the fields. When you click on the next row you will see that ID in the first row is automatically filled.
    • Continue with this until you have entered all the necessary information.
  11. Process the table to store the data. When you have finished entering the data, click Execute SQL in the toolbar to save it. The SQL server continues to run in the background, processing all data contained in the columns. The button looks like a red exclamation mark. You can also press Ctrl+R. to run it.
    • If errors are discovered, you will get an overview of which entries have been entered incorrectly before the table can be processed.
  12. Consult your data. At this point your database has been created. You can create as many tables as needed within each database (there is a limit, but most users will not need to worry about that unless they are working with enterprise level databases). You can now request your data for reports or other administrative purposes. Read articles on wikiHow for more detailed information on running queries.