SQL Basics 1 (MICK)

Database and SQL

C:\PostgreSQL\9.5\bin\psql.exe -U postgres -d shop

The basic idea of ​​the database

Database (DB) : it will save large amounts of data through computer processing of data can be efficiently accessed by a set of
database management system (DBMS) : used to manage the database of the computer system
the importance of the DBMS :( text files and spreadsheets can not do)

  • People can not share data
  • We can not provide a large number of operations required data format
  • The ability to read and write automation needs programming
  • Unable to deal with unexpected incidents
    kinds of DBMS
  • Hierarchical databases: Hierarchical DB
  • Relational Database: Relational DB using SQL (Structured Query Language) queries
    • Relational database management system: Relational Database Management System
      • Oracle
      • SQL Server
      • DB2
      • Postgresql
      • MySQL
  • Object-oriented database: Object Oriented DB
  • XML database: XMLDB
  • Key-value storage system: Key_Value Store

    Structure of the database

    Common RDBMS architecture of
    the client, the server, the client by calling SQL data
    tables structure
  • Table: two-dimensional table consisting of rows and columns
  • According to the data contents of the SQL statement returned must be in the form of two-dimensional table
  • Field, recording a relational database reads and writes data in units
  • Column (vertical direction) and a row (horizontal direction) intersecting grid called cell, a cell only one data input

    SQL summary

  • SQL is a language for manipulating relational databases
  • The type of SQL statements
    • DDL (Data Definition Language) to create or delete data stored in databases and database objects such as tables
      • CREATE: to create the database and data tables
      • DROP: delete the database and data tables
      • ALTER: modify the structure of the database and tables
    • DML (Data Manipulation Language) is used to query or change records in the table
      • SELECT: data look-up table
      • INSERT: insert new data into the table
      • UPDATE: update table data
      • DELETE: to delete the data in the table
    • DCL (Data Control Language) is used to confirm or cancel the changes to the data in the database
      • COMMIT: to confirm the changes to the data in the database
      • ROLLBACK: Cancel changes to the data in the database
      • GRANT: gives user rights
      • REVOKE: cancel the user's operating authority
  • The basic rules of writing SQL
    • SQL statements with a semicolon (;) end of a SQL statement may describe the operation of a database
    • SQL statements are not case-sensitive To facilitate understanding of the Uniform Rules
      • Keyword Capitalization
      • The first letter of the table name in uppercase
      • Column name lowercase
      • Inserted into the data table is case-sensitive, the cell Gerry Computer, COMPUTER, computer is not the same
    • Notation is fixed constant
      • Dates to recommend a unified single quotes '2010-01-26' format
      • Numeric constant without filling in single quotes
      • Half-size space word need to separate or break lines

        Create a table

  • Creating a database
CREATE DATABASE <数据库名>;
CREATE DATABASE shop;
  • Create a table
CREATE TABLE <表名>
                    (<列名1><数据类型><该列的约束>
                     <列名2><数据类型><该列的约束>
                     <列名3><数据类型><该列的约束>
                     ...
                     <该表的约束1><该表的约束2>);
CREATE TABLE Product
                     (product_id CHAR(4) NOT NULL,
                      product_name VARCHAR(10) NOT NULL,
                      product_type VARCHAR(32) NOT NULL,
                      sale_price INTEGER ,
                      purchase_price INTEGER ,
                      regist_date DATE ,
                      PRIMARY KEY (product_id)
                     );  --Postgresql通过 \dt来查看数据库里的表 \dt <表名>来查看表的定义
  • Naming Rules
    • Use only half-width letters, numbers, underscores (_) name as the database, table and column
    • The name must begin with a half-width letters
    • You can not create a database with two tables with the same name
  • type of data
    • All columns must specify the data type of
      the data type indicates the type including numeric, character and date data type - INTEGER for integer storage, can not store decimal
      • CHAR string used to specify the storing fixed-length string is case-sensitive
      • VARCHAR variable to store the string used to specify a case-sensitive string - Oracle used VARCHAR2
      • DATE is used to specify the storage date
  • Set constraint: In addition to data type, data stored in a column or limiting conditions added functionality
    • NOT NULL non-empty
    • PRIMARY KEY key primary (non-empty not repeated)

      Delete, and update the table

  • Delete table
     DROP TABLE <表名>;
     DROP TABLE <Product>; --无法恢复           
  • Update table
  1. Increase Column
ALTER TABLE <表名> ADD COLUMN <列名>;
-- Oracle和MS SQL 可以不写COLUMN
-- Oracle添加多列:ALTER TABLE <表名> ADD (<列名1>,<列名2>,...);
  1. Remove Columns
ALTER TABLE <表名> DROP COLUMN <列名>;
-- Oracle不用写COLUMN
-- Oracle删除多列:ALTER TABLE <表名> DROP (<列名1>,<列名2>,...); --无法恢复
  1. Inserting data into the table
-- DML:插入数据
BEGIN TRANSACTION;
INSERT INTO Product VALUES ('0001','T恤衫','衣服',1000,500,'2009-09-20');
INSERT INTO Product VALUES ('0002','打孔器','办公用品',500,320,'2009-09-11');
INSERT INTO Product VALUES ('0003','运动T恤','衣服',4000,2800,NULL);
INSERT INTO Product VALUES ('0004','菜刀','厨房用具',3000,2800,'2009-09-20');
INSERT INTO Product VALUES ('0005','高压锅','厨房用具',6800,5000,'2009-01-15');
INSERT INTO Product VALUES ('0006','叉子','厨房用具',500,NULL,'2009-09-20');
INSERT INTO Product VALUES ('0007','擦菜板','厨房用具',880,790,'2008-04-28');
INSERT INTO Product VALUES ('0008','圆珠笔','办公用品',100,NULL,'2009-11-11');
COMMIT;  
-- MySQL改为:START TRANSACTION -- Oracle和DB2中不用BEGIN TRANSACTION
  1. Modify the table name
ALTER TABLE Poduct RENAME TO Product; -- Oracle/PostgreSQL
RENAME TABLE Poduct TO Product; -- DB2:
sp_rename 'Poduct','Product'; -- SQL SEVER:
RENAME TABLE Poduct TO Product; --MySQL:

Guess you like

Origin www.cnblogs.com/evian-jeff/p/11391924.html