Monday, May 9, 2016

How To Create Table In Sql-TCCICOMPUTERCOACHING.COM



How To Create Table In Sql

-TCCICOMPUTERCOACHING.COM


SQL stands for structured Query Language.

SQL is database computer language which allows user to store, access, maintain and remove data from database.

Data which user insert in database is stored in tables. Before inserting data into the table have to create table in database.
Syntax to create table is following:
Create TABLE table name (col1name data type (size of Data),col2name data type (size of data)…);
I.e. if we want to create table for student, which stores data of student rollno and name, then SQL query is like this,
Create Table student (Srnint(10), Snamevarchar (20));
O/P: student table created.
If there is no error then we got this msg “table created”.
Here, Create Table is keyword which tells database to create table using given name in syntax. This syntax includes table name, column name, data type and size of data.
We can use primary key also here. Then syntax is like that
Create Table student (Srnint (10) primary key, Snamevarchar (20));
Primary key gives uniqueness to data in the table student. Because may be there are more than one students of same name, but rollno of students is created as “primary key”, so, it’s unique now. So, it removes duplication from the database of student. It’s good na!
Now if you want to see your created table in database, then write DESC SQL query as per following:
DESC CUSTOMERS;

| Field                         | Type             | Null               | Key               | Default         | Extra |
| ID                                          | int(11)          | NO                 | PRI                |                       |           |
| NAME                       | varchar(20)             | NO                 |                       |                       |           |
| AGE                           | int(11)          | NO                 |                       |                        |          |
| ADDRESS                 | char(25)      | YES               |                       | NULL            |           |
| SALARY                    | decimal(18,2) | YES                        |                       | NULL             |          |

5 rows in set (0.00 sec)

Let us see another example to create customer table.
CREATE TABLE CUSTOMER (
  CID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE INT              NOT NULL,
   ADDRESS CHAR (25),
   SALARY   DECIMAL (18, 2),      
   PRIMARY KEY (ID)
);
Now if you want to insert data in table then have to write another SQL query to insert data into the table.
INSERT INTO TABLE_NAME (col1, col2, col3, colN)] 
VALUES (value1, value2, value3, valueN);
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (1, 'PUJA', 32, 'Ahmedabad', 2000.00);
If you want to see data inserted in table CUSTOMERS, then write SQL query like
Select * from CUSTOMERS;
ID        | NAME                       | AGE               | ADDRESS                 | SALARY    1 | PUJA                         | 32                 | Ahmedabad                         | 2000.00










No comments:

Post a Comment