SQL DB structure

Excellent link concerning the strategy, design and DB forming.
Short and to the point explanation in a youtube tutorial.

Primary Key
Each table should have a column (or a set of columns), called primary key, that uniquely identifies every records of the table. A primary key is called a simple key if it is a single column; it is called a composite key if it is made up of several columns. Most RDBMSs build an index on the primary key to facilitate fast search and retrieval. The primary key is also used to reference other tables. The primary key shall always have a value. In other words, it shall not contain NULL.
A table can have only one primary key.
So that was the theory.
In real life, primary key is defined when making the table. Good link.
A primary key cannot be applied on a column with a null constraint (nullable). When a table is created, by default every column is nullable (link).
So first off all we have to remove the null constraint and apply a NOT NULL constraint on the column :
ALTER TABLE spisak alter column rednibroj int NOT NULL;
And then we define the column “rednibroj” as a primary key for table “spisak” :
ALTER TABLE spisak ADD PRIMARY KEY (rednibroj);

How to list primary keys in table :
SELECT * FROM information_schema.table_constraints
WHERE constraint_type = ‘Primary Key’ and TABLE_NAME = ‘spisak’;
Output from this is quite unusable…..

Views (link)
A view is nothing more than a SQL statement that is stored in the database with an associated name. A view is actually a composition of a table in the form of a predefined SQL query.
Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables or another view.
The INFORMATION_SCHEMA views allow you to retrieve metadata about the objects within a database :
Što za moju testnu tabelicu daje ovo :


You could create index on selected column(s) to facilitate data searching and retrieval. An index is a structured file that speeds up data access for SELECT, but may slow down INSERT, UPDATE, and DELETE

This entry was posted in WIN and tagged , , . Bookmark the permalink.

Comments are closed.