To create a table's SQL statement

Source: Internet
Author: User

This article provides three SQL statement instance code to create a table, one by one example test and explain how to use SQL to create a datasheet and database tutorial Description instance.

Use [master]
Go
if exists (select 1 from sysdatabases where name=n ' hktemp ')
Begin
Drop Database Hktemp--If a database exists, delete it first
End
Go
Create DATABASE Hktemp
On
Primary--Create master database files
(
Name= ' Hktemp ',
Filename= ' e:databaseshktemp.dbf ',
SIZE=5MB,
MAXSIZE=20MB,
Filegrowth=1mb
)
Log on--Creating a journal file
(
Name= ' Hktemplog ',
Filename= ' E:databaseshktemp.ldf ',
SIZE=2MB,
MAXSIZE=20MB,
Filegrowth=1mb
)
Go
--Adding tables
If not EXISTS (SELECT * from sys.objects where object_id = object_id (n ' hksj_user ') and type in (n ' u '))
Begin
CREATE TABLE Hksj_user
(
ID int identity (1,1) not NULL,
Sname nvarchar is not NULL,
Snickname nvarchar (20),
Spassword nvarchar () NOT NULL,
Dcreatedate datetime,
Screator nvarchar (20),
Semail nvarchar (50),
Sphone nvarchar (50),
Sidentifyid nvarchar (30),
Dlasttimelogon datetime
Primary key Clustered
(
ID ASC
) with (Ignore_dup_key = out) on [primary]
) on [primary]
End
Go
--Add comments
exec sys.sp_addextendedproperty @name =n ' ms_description ', @value =n ' login name ', @level0type =n ' schema ', @level0name =n ' dbo ', @ Level1type=n ' table ', @level1name =n ' Hksj_user ', @level2type =n ' column ', @level2name =n ' sname '
Go
exec sys.sp_addextendedproperty @name =n ' ms_description ', @value =n ' mailbox ', @level0type =n ' schema ', @level0name =n ' dbo ', @ Level1type=n ' table ', @level1name =n ' Hksj_user ', @level2type =n ' column ', @level2name =n ' Semail '


Example Two

drop table Cell_tch;
drop table MS;
drop table MSC;
drop table BSc;
drop table BTS;
drop table cell;
drop table antenna;
drop table data;
drop table test;
drop table neighbor;

CREATE TABLE MSC (
Mscid integer NOT NULL,
Mscname character (10),
Msccompany character (10),
Msclongitude Decimal (9,6),
Msclatitude Decimal (8,6),
Mscaltitude Integer,
Primary KEY (MSCID));

CREATE table BSc (
       bscid integer  not NULL,
        Bscname character (a),
       bsccompany character (a),
       Longitude Decimal (9,6),
       latitude Decimal (8,6),
       mscid integer,
       Primary KEY (Bscid),
       foreign Key (MSCID) references MSc (MSCID));

CREATE TABLE BTS (
       btsname character ()   NOT NULL,
  & nbsp;    bscid integer  NOT null,
       longitude Decimal (9,6) ,
       latitude Decimal (8,6),
       altitude Integer,
       btscompany character (Ten),
        Btspower Decimal (2,1),
       primary KEY (Btsname),
   & nbsp;   foreign KEY (bscid) references BSc (Bscid));

Create table cell (
       cellid integer NOT null,
    & nbsp;  Btsname character (a),
       areaname character (a),
        lac Integer,
       longitude Decimal (9,6),
        latitude Decimal (8,6),
       direction Integer,
       radious Integer,
       antnum Integer,
       bcch Integer,
       primary KEY (Cellid) ,
       foreign Key (btsname) references BTS (btsname);

Create TABLE MS (
        imei      bigint  Not NULL,
        msisdn    bigint,
         username  varchar (ten),
        Mscompany varchar,
        gsmmsps tutorial ense integer,
         gsmmsheight Decimal (3,2),
        gsmmspfout decimal (3,2) ,
        Mzone character (a),
         Cellid Integer,
        primary KEY (IMEI),
         foreign KEY (cellid) references cell (cellid);

CREATE TABLE Antenna (
Cellid integer NOT NULL,
Antennahigh Integer,
Halfpangle Integer,
Maxattenuation Integer,
Gain Integer,
Anttilt Integer,
PT Integer,
MSPWR Integer,
Primary KEY (Cellid));

CREATE TABLE Cell_tch (
Cellid Integer,
Freq Integer,
Foreign KEY (Cellid) references cell (cellid));

CREATE TABLE data (
       date integer  not null,
        time integer  NOT null,
       cellid integer  is not NULL,
       ntch Integer,
       traff Decimal (9,7),
       Rate Decimal (10,9),
       Thtraff Decimal (9,7),
       callnum integer,
       Congsnum integer,
       callcongs Decimal (10,9),
        primary KEY (Date,time,cellid);

CREATE TABLE Test (
       keynum integer  not NULL,
   & nbsp;   cellid Integer,
       latitude Decimal (9,6),
        Longitude Decimal (8,6),
       Rxlev Decimal (9,6),
& nbsp;      primary KEY (Keynum),
       foreign Key ( Cellid) References cell (cellid));

CREATE TABLE neighbor (
Cellid Integer,
Adjcellid Integer,
Celllac Integer,
Adjcelllac Integer,
Foreign KEY (Cellid) references cell (cellid));
Import from "C:msc.csv" to Del method p (1, 2, 3, 4, 5, 6) Messages "1" inserts into Db2admin.msc (Mscid, Mscname, Msccompa NY, Msclongitude, Msclatitude, mscaltitude);

Import from "C:bsc.csv" to Del method p (1, 2, 3, 4, 5, 6) Messages "1" inserts into DB2ADMIN.BSC (Bscid, Bscname, Bsccompa NY, longitude, Latitude, mscid);

Import from "C:bts.csv" to Del method p (1, 2, 3, 4, 5, 6, 7) messages "1" inserts into Db2admin.bts (Btsname, Bscid, Longi Tude, latitude, altitude, btscompany, btspower);

Import from "C:cell.csv" to Del method p (1, 2, 3, 4, 5, 6, 7, 8, 9, ten) messages "1" INSERT into Db2admin.cell (Cellid, b Tsname, AreaName, Lac, longitude, latitude, direction, radious, Antnum, BCCH);

Import from "C:data.csv" The Del method p (1, 2, 3, 4, 5, 6, 7, 8, 9, ten) messages "1" INSERT INTO "data" (date, time, cell IDs, Ntch, Traff, rate, Thtraff, Callnum, Congsnum, callcongs);

Import from "C:ms.csv" to Del method p (1, 2, 3, 4, 5, 6, 7, 8, 9) Messages "1" inserts into db2admin.ms (IMEI, MSISDN, use Rname, Mscompany, Gsmmspsense, Gsmmsheight, Gsmmspfout, Mzone, cellid);

Import from "C:cell_tch.csv" to Del method p (1, 2) messages "1" inserts into Db2admin.cell_tch (Cellid, freq);

Import from "C:test.csv" to Del method p (1, 2, 3, 4, 5) Messages "1" inserts into Db2admin.test (Keynum, Cellid, Latitude, longitude, Rxlev);

Import from "C:antenna.csv" to Del method p (1, 2, 3, 4, 5, 6, 7, 8) Messages "1" inserts into Db2admin.antenna (Cellid, a Tennahigh, Halfpangle, Maxattenuation, Gain, Anttilt, PT, MSPWR);

Import from "C:neighbor.csv" to Del method p (1, 2, 3, 4) messages "1" inserts into Db2admin.neighbor (Cellid, Adjcellid, C Elllac, Adjcelllac);


Method Three

For
Example:
CREATE TABLE [student]
(
School Number char (8) NOT null PrimaryKey,
Name varchar (8) NOT NULL
)

There are two columns, the rest is similar, as to not allow duplicate names, plus "constraints" on the line.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.