sqlite基本sql语句使用 一,SQLite常见的数据类型 SQLite是无类型的。 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中,无论这列声明的数据类型是什么(只有自动递增Integer Primary Key才有用)。对于SQLite来说对字段不指定类型是完全有
sqlite基本sql语句使用
一,SQLite常见的数据类型
SQLite是无类型的。 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中,无论这列声明的数据类型是什么(只有自动递增Integer Primary Key才有用)。对于SQLite来说对字段不指定类型是完全有效的。 即使SQLite允许忽略数据类型,但是仍然建议在你的Create Table语句中指定数据类型。 因为数据类型对于你和其他的程序员交
流,或者你准备换掉你的数据库引擎是非常有用的。SQLite只支持常见的5种存储类,
NULL
INTEGER --整型
REAL --浮点数
TEXT --文本
BLOB --大二进制对象
以下定义的数据类型都会转到相应的存储类中。
create table tab( --注意其中的注释方式
a VARCHAR(10), --长度不固定且其最大长度为n的字符串
b NVARCHAR(15),
c TEXT, --二进制对象
d INTEGER, --带符号的整型,具体取决于存入数字的范围大小
e FLOAT,
f BOOLEAN,
g CLOB, --使用CHAR来保存数据
h BLOB, --使用二进制对象保存数据,如保存位图
i TIMESTAMP,
j NUMBERIC(10,5),
k VARYING CHARACTER(24),
l NATIONAL VARYING CHARACTER(16), //
j REAL --浮点数字,存储为8-byte IEEE浮点数
);
二, 基本的数据操作
1,建立表
Create table admin(
username text,
age integer);
2,插入数据
insert into 表名(字段列表) values(值列表);
例如:insert into admin values(‘song’,25);
3,查询
select 字段名 from 表名;
select * from admin;
select distinct field from table_name;(distinct去掉重复项,将列中各字段值单个列出)
4,删除数据
Delete from 表名 where 条件子句。
delete from admin form where username=’song’;
5,修改
update 表名 set 字段名=值 where 条件子句。
update admin set username=’zhang’,age=24 where username=’song’ and age=25;
6,按条件分组
select * from 表名 where 条件子句 group by 分组子句 having …order by排子句
例如:
select * from admin;
select * from admin order by id desc(降序) | asc(升序);
select username from admin group by username having count(*)>1;
7,多条件查询语句
select 字段名 from 表名 where 子句1 按 子句二
select * from admin where username=’song’ and age=24;
select * from table_name where field in (‘val1’ , ’val2’ , ‘val3’ );
select * from table_name where field between val1 and val2;
select * from admin limit 5; --限制输出数据记录数量
8,多条件排序
select 字段名 from 表名 order by 字段1 (desc),字段2(desc);
select * from admin order by t1 ,t2 desc;
9,索引
例如 建立复合索引:create index idxT1 on admin(username,age);
各自建立索引:create index idxUsername on admin(username);
create index idxAge on admin(age);
10,外键FOREIGN KEY(UNIQUE | PRIMARY KEY | NOT NULL)的用法()
create table a(
a1 INTEGER PRIMARY KEY | UNIQUE | NOT NULL,
a2 TEXT,
a3 INTEGER );
create table b()(
b1 INTEGER ,
b2 TEXT,
b3 INTEGER,
foreign key(b3) references a(a1));
11,分页
select * from account limit 5 offset 3;
或者 select * from account limit 5,3;
12,模糊查询
SELECT 字段 FROM 表 WHERE 某字段 LIKE 条件
(1)%:表示任意0个或多个字符
(2)_ :表示任意单个字符,匹配单个任意字符,常用来限制表达式的字符长度语句。
(3)[ ]:表示括号内所列字符中的一个(类似正则表达式)
select * from admin where username like ‘[张李王]三’;
表示搜索的是“张三”,“李三”或“王三”
[4]:[^]表示不在括号所列之类的单个字符。
[5]:查询内容包含通配符时,用“[ ]”括起来。
13,删除表 | 索引
drop table [ IF EXISTS] admin;
drop index index_name
14,查询记录数目
select count(*) from table_name;

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)
