<span id="Label3"></p><p><p><strong id="Header1_HeaderTitle" class="headermaintitle"></strong></p></p><p><p><strong>one, SQL statements</strong></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">If you want to manipulate the data in the database while the program is running, you must first learn to use SQL statements</p></p><p><p>1. What is SQL</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">SQL (structured Query language): Structured Query Language</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">SQL is a language for defining and manipulating data in a relational database</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">SQL language concise, simple syntax, studious and useful</p></p><p><p></p></p><p><p>2. What is a SQL statement</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The sentence \ code written in the SQL language is the SQL statement</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">In the process of running the program, you must use SQL statements to manipulate (add and remove, CRUD) data in the database</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p><p>Features of the 3.SQL statement</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Case insensitive (E.G. The database thinks the user and user are the Same)</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Each statement must have a semicolon; End</p></p><p><p></p></p><p><p>The commonly used keywords in 4.SQL are</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">select, insert, update, delete, from, create, where, desc, order, by, group, table, alter, view, index, and so on</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">You can not use keywords to name tables, fields in a database</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p><p><strong>II. Types of SQL statements</strong></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">1. Data definition statements (ddl:data definition Language)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Includes operations such as Create and drop</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Create a new table or delete a table in the Database (create table or drop tables)</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">2. Data manipulation Statements (dml:data manipulation Language)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Include insert, update, delete, and more</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The above 3 actions are used to add, modify, and delete data in a table, respectively</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">3. Data query Statement (dql:data query Language)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">can be used to query for data in a table</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Keyword SELECT is the most used operation for DQL (and all sql)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Other dql commonly used keywords are where,order by,group by and having</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p><strong>three, the basic operation</strong></p></p><p><p>1. Create a table</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">CREATE TABLE table name (field name 1 field type 1, field Name 2 field type 2, ...);</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The CREATE table if not EXISTS table name (field Name 1 field type 1, field Name 2 field type 2, ...);</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">CREATE TABLE t_student (id integer, name text, age integer, score real);</p></p><p><p>2. Field type</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">SQLite divides the data into the following types of storage:</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Integer: integer value</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Real: floating-point value</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Text: literal string</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Blob: binary Data (such as Files)</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span>Note: in fact, SQLite is untyped and can store string literals (except primary keys), even if declared as an integer type</span></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">It is possible to declare what type or not to declare a type when building a table, which means that the statement can be written like this:</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">CREATE TABLE T_student (name, age);</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Tip: in order to maintain good programming specifications and facilitate communication between programmers, it is best to add the specific type of each field when writing a table statement</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p><p>3. By deleting the table</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">drop table name;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">drop table if exists name;</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">drop table t_student;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>4. Inserting Data (insert)</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Insert into table name (field 1, field 2, ...) values (value of field 1, value of field 2, ...);</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Insert INTO t_student (name, age) values (' MJ ', 10);</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Attention</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The string contents in the database should be enclosed in single quotes</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>5. Updating Data (update)</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Update table name set field 1 = Value of field 1, Field 2 = value of field 2, ...;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Update t_student Set name = ' Jack ', age = 20;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Attention</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The previous example changes the name of all records in the T_student table to Jack,age to 20</p></p><p><p>6. Deleting Data (delete)</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Delete from table name;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Delete from t_student;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Attention</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The above example deletes all the records in the T_student table</p></p><p><p>7. Conditional Statements</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">If you only want to update or delete some fixed records, you must add some conditions after the DML statement</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Common formats for conditional statements</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">where field = a value; cannot be used for two x =</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The Where field is a value; is equals =</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">where field! = a value;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The Where field is not a value; is isn't equivalent to! =</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">where field > a value;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">where field 1 = a value and field 2 > a value; and equivalent to the && in C language</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">where field 1 = a value or field 2 = a value; or equivalent to the C language | |</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Change the age of the T_student table to more than 10 and the name does not equal Jack's record to 5</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Update t_student Set age = 5 where age > Ten and name! = ' Jack ';</p></p><p><p></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Delete records with age less than or equal to 10 or older than 30 in the T_student table</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Delete from T_student where is age <= or age > 30;</p></p><p><p></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Guess the effect of the following statement</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Update T_student Set score = age WHERE name = ' Jack ';</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Change the name in the T_student table to the Jack record, and the value of the score field to the value of the age field</p></p><p><p>8.DQL statements</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select field 1, field 2, ... from table name;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * FROM Table name; Query all fields</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select name, age from t_student;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * FROM t_student;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from t_student where age > 10; Conditional query</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>9. Aliases</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format (fields and tables can be aliases)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select field 1 alias, Field 2 alias, ... from table name alias;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select field 1 alias, field 2 as alias, ... from table name as alias;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select Alias. field 1, alias. field 2, ... from table name alias;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select name myname, age myage from t_student;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Give name a nickname called myname, an alias called Myage for Age.</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select s.name, s.age from T_student s;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Give T_student an individual name called S and use s to refer to the fields in the table</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>10. Calculate the number of records</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select Count (field) from table name;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select Count (*) from table name;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select Count (age) from t_student;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select Count (*) from T_student where score >= 60;</p></p><p><p>11. Sorting</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The results of the query can be sorted with order by</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from T_student the ORDER by field;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from T_student ORDER by age;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The default is to sort in ascending order (from small to large), or to descending (from large to Small)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from T_student ORDER by age desc; Descending</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from T_student ORDER by age asc; Ascending (default)</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">You can also sort by multiple fields</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from T_student order by age asc, height desc;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Sort by age (ascending), and age equal by height (descending)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>12.limit</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Use limit to precisely control the number of query results, such as querying only 10 data at a time</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Format</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from table name limit value 1, value 2;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">SELECT * from t_student limit 4, 8;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Can be understood as: skip the first 4 statements, then fetch 8 records</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The limit is often used for paging queries, such as 5 data per page, so you should take the data</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">1th page: Limit 0, 5</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">2nd page: Limit 5, 5</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">3rd page: Limit 10, 5</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">...</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Page n: Limit 5* (n-1), 5</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">SELECT * FROM t_student limit 7; This statement is equivalent to select * from T_student limit 0, 7; to take the first 7 records</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p><p><strong>Iv. constraints</strong></p></p><p><p>1. Simple Constraints</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">When you build a table, you can set some constraints on specific fields, and the common constraints are</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Not null: the value of the specified field cannot be null</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Unique: the value of the specified field must be unique</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Default: specify a value for the field</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">( <span>recommendation: as far as possible to set strict constraints on the field to ensure the normative data)</span></p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">CREATE TABLE t_student (id integer, Name text not null-unique, age integer NOT null default 1);</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The name field cannot be null, and the unique</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The age field cannot be null, and the default is 1</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>2. PRIMARY KEY constraints</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(1) Brief description</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">If the T_student table is in the name and age two fields, and some records have the same name and the value of the date field, then the data cannot be distinguished, resulting in a database that is not unique, which makes it inconvenient to manage the data</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">A good database programming specification should ensure uniqueness of each record, adding a PRIMARY KEY constraint</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">In other words, each table must have a primary key to identify the uniqueness of the record</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(2) What is a primary key?</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Primary key (Primary key, referred to as Pk) uniquely identifies a record</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">For example, t_student can add an ID field as the primary key, which is equivalent to a Person's ID</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The primary key can be a field or multiple fields</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(3) design principle of primary key</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The primary key should not be meaningful to the user</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Never update the primary key</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The primary key should not contain dynamically changing data</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The primary key should be automatically generated by the computer</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(4) declaration of the primary key</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Declare a primary key with primary key when creating a table</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">CREATE TABLE T_student (id integer primary key, name text, age integer);</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The ID of the integer type as the primary key for the T_student table</p></p><p><p></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Primary key field</p></p><p style="margin-left: 90px;"><p style="margin-left: 90px;">As long as primary key is declared, it is a primary key field</p></p><p style="margin-left: 90px;"><p style="margin-left: 90px;">The primary key field contains not NULL and a unique two constraint by default</p></p><p><p></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"><span>Description: If you want the primary key to grow automatically (must be an integer type), you should increase the AutoIncrement</span></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">CREATE TABLE T_student (id integer PRIMARY key autoincrement, name text, age integer);</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>3. FOREIGN KEY constraints</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Using FOREIGN KEY constraints can be used to establish a table-to-table connection</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The general case of a foreign key is a field in a table that refers to the primary key field of another table</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Create a new foreign key</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">CREATE TABLE T_student (id integer PRIMARY key autoincrement, name text, age integer, class_id integer, constraint Fk_stud Ent_class foreign key (class_id) references T_class (id));</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">There is a foreign key called fk_t_student_class_id_t_class_id in the T_student table.</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The role of this foreign key is to refer to the ID field of the T_class table using the class_id field in the T_student table</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>4. Table Connection Query</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Table Join query: You need to federate multiple tables to find the data you want</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Types of table joins</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Inner joins: inner JOIN or join (displays records with a full field value in the left and right tables)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Left outer connection: left OUTER join (guarantees the integrity of data on the Table)</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Example</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Find all students in class 0316iOS</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Select S.name,s.age from T_student s, t_class c where s.class_id = c.id and c.name = ' 0316iOS ';</p></p><p><p>iOS Development Database-sql</p></p></span>
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