📜  SQL |创造(1)

📅  最后修改于: 2023-12-03 15:20:16.077000             🧑  作者: Mango

Intro to SQL

SQL or 'Structured Query Language' is a language used to manage and manipulate data stored in relational databases. It is an essential skill for any programmer, as it is widely used in web development, data analysis, and data science.

Creating Tables

In SQL, data is organized into tables. Tables consist of columns or fields, which represent the data attributes, and rows or records, which represent the individual data instances.

Here's an example of how to create a table in SQL:

CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(255)
);

In this example, we create a users table with columns for id, name, and email.

Inserting Data

Once we have our table, we can insert data into it using the INSERT statement:

INSERT INTO users
(id, name, email)
VALUES
(1, 'John Smith', 'john@example.com'),
(2, 'Jane Doe', 'jane@example.com'),
(3, 'Bob Johnson', 'bob@example.com');

This adds three rows of data to our users table.

Retrieving Data

To retrieve data from a table, we use the SELECT statement:

SELECT * FROM users;

This returns all the data in the users table:

| id | name | email | | --- | -----------| ----------------| | 1 | John Smith | john@example.com | | 2 | Jane Doe | jane@example.com | | 3 | Bob Johnson| bob@example.com |

We can also query specific data with conditions:

SELECT name FROM users WHERE id = 1;

This returns the name of the user with id equal to 1:

| name | | -----------| | John Smith |

Conclusion

SQL is a powerful tool for managing data, and as a programmer, it's an essential skill to have. With the ability to create tables, insert and retrieve data, and perform complex queries, you can manipulate data in countless ways to achieve your goals.